There are several issues with this piece of code.
- Missing
;
at the end of several lines
- Missing the
$con
parameter for most mysqli_
functions
- Mixing
mysql_
and mysqli_
- 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
Input data is succeed")` – Beginner Dec 23 '16 at 09:32
Input data is succeed")** add semicolon `;` – Soni Vimalkumar Dec 23 '16 at 09:33