0

I am having a problem when I try to insert data into mysql. Doing post request and having the data inside php file. No problem at that part. I can iterate the data inside php file and see the results... But when I try to insert data into mysql. then it's giving the 500 (Internal Server Error)

$.ajax({
    type: "POST",
    url: "insert.php",
    data: {
        fetch: tasksJson
    },
    success: function (data) {
        console.log("The ajax request succeeded!");
        console.log(data);
    },
    error: function (err) {
        console.log(err + "The request failed");
    }
});

insert.php file:

$servername = "localhost";
$database = "databaseName";
$username = "example";
$password = "example";

$conn = new mysqli_connect($servername, $username, $password, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST)){
    if(isset($_POST['fetch'])){
        $array = json_decode($_POST['fetch'],true);
        foreach ($array as $key => $val) {
            // prepare and bind
            $prep = $conn->prepare("INSERT INTO tasks (gid, project, title) VALUES (?, ?, ?)");
            $prep->bind_param("sss", $gid, $project, $title);

            $gid = $val["gid"];
            $project = $val["project_name"];
            $title = $val["title"];

            $prep->execute();       

            echo "New records created successfully";

        }
    }
}

$prep->close();
$conn->close();

Do I missing something here?

yvl
  • 620
  • 7
  • 25
  • 1
    1. You are open to SQL injection, please use prepared statements. – PHPology Jan 08 '20 at 05:44
  • 1
    2. Looking at your query, it looks like you are wanting to update 3 columns but where you have the value's part, you are only saving to 1 column. You need to add the single quotes around each variable. – PHPology Jan 08 '20 at 05:46
  • take a look at this post : https://stackoverflow.com/a/60496/3387094 – Ramyz Jan 08 '20 at 05:52
  • I updated the question. still having same error. @Ramyz – yvl Jan 08 '20 at 06:00
  • Capture the error of mysqli prepared statements if any. Refer a link: https://www.php.net/manual/en/mysqli-stmt.error.php . If issue did not get resolved then try to have a look at url: https://www.php.net/manual/en/function.register-shutdown-function.php#85727 – Prasad Wargad Jan 08 '20 at 06:08
  • you should always try to send and receive data in json , so you can use JSON.stringify(inputdata) in ajax and in php json_decode() that json request – shashikant kuswaha Jan 08 '20 at 06:08
  • you can try by doing error reporting ON by ini_set('display_errors', 1); error_reporting(E_ALL); – shashikant kuswaha Jan 08 '20 at 06:09
  • Can you add json in your question? – Jinesh Jan 08 '20 at 06:24
  • $prep->bind_param("sss", $gid, $project, $title); add this line after assigning variables before execute() – Ramyz Jan 08 '20 at 06:27

1 Answers1

0

I am on my mobile but try this: $sql = "INSERT INTO tasks (gid, project_name, title) VALUES ('$gid', '$project', '$title')";

Like I said before, please use prepared statements too to prevent SQL injection. https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

PHPology
  • 1,017
  • 6
  • 12