3

Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\cc_real\3HLR14CMORCGZ0IY8AE7H4JL409RV9\insert.php on line 15.

Showing me this error, and Below is my code that I am trying to use for inserting data into a database.

<?php
echo "Invoked!!!";

$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
    die('could not connect:'.mysql_error());    
}
mysqli_select_db('job1');


Error: $sql = "INSERT INTO `cc_job2`(cc_Answer_filename,cc_time,cc_workerID)  VALUES
       ('$_post["Answer_filename"]','$_post["track_data"]','$_post["workerID"]')";

$result = mysqli_query($sql)
if ($result){
    echo ("<br> Input data is succeed") 
} else{
    echo("<br> Input data is failed");
}
mysqli_close($con);
?>
Tolios
  • 219
  • 3
  • 12
Rocky
  • 67
  • 1
  • 1
  • 6

4 Answers4

2

There are several issues with this piece of code.

  1. Missing ; at the end of several lines
  2. Missing the $con parameter for most mysqli_ functions
  3. Mixing mysql_ and mysqli_
  4. Using $_post instead of $_POST (capital letters)

See the comments around the code for what's been changed.

<?php
echo "Invoked!!!";

$con = mysqli_connect('localhost', 'root', '');
/*
You can also do this, and drop mysqli_select_db() later in the code
$con = mysqli_connect('localhost', 'root', '', 'job1');
*/

if (!$con) {
    // Cannot mix mysql with mysqli (changed out mysql_error())
    // Also, mysqli has "mysqli_connect_error()" for connecting errors
    die('could not connect: '.mysqli_connect_error());    
}

// This function require the $con parameter
mysqli_select_db($con, 'job1');

// Quotes being messed up - this is your current error
// Concatenate the POST values instead, like this
// Also using $_post instead of $_POST
$sql = "INSERT INTO `cc_job2` (cc_Answer_filename, cc_time, cc_workerID) VALUES ('".$_POST["Answer_filename"]."', '".$_POST["track_data"]."', '".$_POST["workerID"]."')";

// Missing $con as the first parameter and ; at the end
$result = mysqli_query($con, $sql);
if ($result) {
    // Missing ; at the end 
    echo "<br> Input data is succeed";
} else{
    echo "<br> Input data is failed";
    // You should add echo mysqli_error($con); here to troubleshoot queries
}
mysqli_close($con);
?>

Note that the query will fail if any of the POST-values contains singlequotes ', so you should either escape them, or better yet, use prepared statements (see the paragraph below and the link "How can I prevent SQL injection in PHP?" at the bottom.

This code is also vulnerable to SQL injection, and you should use prepared statements with placeholders to guard yourself against this.

See these links

Community
  • 1
  • 1
Tolios
  • 219
  • 3
  • 12
1

Just Add ; end of the line echo ("<br> Input data is succeed") and $result = mysqli_query($sql).

Try to something like this.

<?php
echo "Invoked!!!";

$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
die('could not connect:'.mysql_error());    
}
mysqli_select_db('job1');


Error: $sql = "INSERT INTO cc_job2(cc_Answer_filename,cc_time,cc_workerID) VALUES ('".$_post['Answer_filename']."','".$_post['track_data']."','".$_post['workerID']."')";

$result = mysqli_query($sql);
if ($result){
echo ("<br> Input data is succeed");

}else{
echo("<br> Input data is failed");
}
mysqli_close($con);
?>
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34
0
$sql = "INSERT INTO `cc_job2`(cc_Answer_filename,cc_time,cc_workerID)  VALUES ('".$_post['Answer_filename']."','".$_post['track_data']."','".$_post['workerID']."')";
Federkun
  • 36,084
  • 8
  • 78
  • 90
0

Concate in mysql query:

<?php
echo "Invoked!!!";

$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
die('could not connect:'.mysql_error());    
}
mysqli_select_db('job1');


Error: $sql = "INSERT INTO cc_job2 (cc_Answer_filename,cc_time,cc_workerID)  VALUES
       ('".$_post["Answer_filename"]."','".$_post["track_data"]."','".$_post["workerID"]."')";

$result = mysqli_query($sql)
if ($result){
echo ("<br> Input data is succeed");

}else{
echo("<br> Input data is failed");
}
mysqli_close($con);
?>
Aniket B
  • 438
  • 1
  • 5
  • 14