0
votes

Edit : I Forgot To Require WP-Load.php Which Contains The $wpdb() Function.

I have made a page in wordpress which contains a form. now that forms sends data to a PHP file throught this javascript code

<script>
$(document).ready(function() {
$("#sbmit").click(function() {
var episode = $("#text1").val().split("\n");
var server1 = $("#text2").val().split("\n");
var server2 = $("#text3").val().split("\n");
var server3 = $("#text4").val().split("\n");
if (server1 != undefined || server2 != undefined || server3 != undefined ) { $.post("/submit.php", {
episode1: JSON.stringify(episode),
server11: JSON.stringify(server1),
server21: JSON.stringify(server2),
server31: JSON.stringify(server3),}, function(data) {alert(data);}
);
}});
});
</script>

and Submit.php file uploads the data to a seperate MYSQL Database since, i'm using wordpress i know $wpdb is the recommended way to insert data in database's but when i use $wpdb the console log gives 500 Error So I changed the wpdb to new sqli method and it worked.

After few minutes when i tried to update any of my post it said update failed but updated my post anyway's then when i opened PHPmyAdmin there came hundreds of Error's and my whole data was and when i opened my wp-admin it started wordpress from the installation page like i just installed wordpress and also said wordpress database Error

So I'm asking "Does mysqli method affects Wordpress in anyway?"

Below Is The Submit.PHP Code But When I Click Submit I Get 500 Error.

<?php
// Create connection
$conn = new wpdb($username, $password, $dbname, $servername);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$Episode2=json_decode($_POST['episode1'], true); // Fetching Values from URL
$Server12=json_decode($_POST['server11'], true);
$Server22=json_decode($_POST['server21'], true);
$Server32=json_decode($_POST['server31'], true);


for ($x = 0; $x < count($Episode2); $x++) {

if ($Episode2[$x] != "") {

$sql = "SELECT Episode FROM DBZ WHERE Episode='".$Episode2[$x]."'";
$result = $conn->get_results($sql);

if (mysqli_num_rows($result) > 0) {

    echo "Episode $Episode2[$x] Server ";
if($Server12[$x] != "") {$sql = "UPDATE DBZ SET Server1 = '".$Server12[$x]."' WHERE Episode = '".$Episode2[$x]."' ";    $result = $conn->get_results($sql); echo "1, ";}

if($Server22[$x] != "") {$sql = "UPDATE DBZ SET Server2 = '".$Server22[$x]."' WHERE Episode = '".$Episode2[$x]."' ";    $result = $conn->get_results($sql);   echo "2, ";}

if($Server32[$x] != "") {$sql = "UPDATE DBZ SET Server3 = '".$Server32[$x]."' WHERE Episode = '".$Episode2[$x]."' ";    $result = $conn->get_results($sql); echo "3 ";}

echo "Updated \n";

} else {

$sql = "INSERT INTO DB VALUES ('".$Episode2[$x]."','".$Server12[$x]."', '".$Server22[$x]."','".$Server32[$x]."')";

$result = $conn->get_results($sql);

echo "Episode $Episode2[$x] is Added in Database \n";

}

} else { echo "Episode Number is Blank";}
}
$conn->close(); // Connection Closed.
?>
1
This is very insecure. Study SQL injections, you should not template like this. For the 500 Error check your server's error logs. - user3783243
The actual cause of the error is from the poor indentation. You have three unclosed control groups. - user3783243

1 Answers

0
votes

Mysqli does not "effect" wordpress. However, you're doing it wrong.

You don't need to create a new connection to use $wpdb. Simply declare the global, the use it directly.

GLOBAL $wpdb;
$sql = "SELECT Episode FROM DBZ WHERE Episode='".$Episode2[$x]."'";

$results = $wpdb->get_results($sql);

Also, try to use prepare statements to prevent sql injections.