Nayem Hossain Nayem Hossain - 1 year ago 58
PHP Question

MYSQL inserting data

i am facing problem during inserting data in mysql database.
the error is showed like this:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1')' at line 1"


$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$fathername = $_POST['fathername'];
$mothername = $_POST['Firstname'];
$fatherjob = $_POST['fatherjob'];
$motherjob = $_POST['motherjob'];
$phone = $_POST['phone'];
$mobile1 = $_POST['mobile1'];
$mobile2 = $_POST['mobile2'];
$address = $_POST['address'];

$sex = $_POST['sex'];
$bloodgroup = $_POST['bloodgroup'];
$class = $_POST['class'];
$section = $_POST['section'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

$hostname_localhost ="localhost";
$database_localhost ="school";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)

mysql_select_db($database_localhost, $localhost);

$sql = "INSERT INTO student_info VALUES ('".$id."',".$firstname."','".$lastname."','".$sex."','".$bloodgroup."','".$fathername."','".$mothername ."','".$fatherjob."','".$motherjob."','".$address."','".$birthday."','".$phone."','".$mobile1."','".$mobile2."','".$class."','".$section."','".$roll."')";

mysql_query($sql) or die(mysql_error());
echo "updated succesfully";


BTW, i ahve 2 columns in my table named id and roll which should be generated that case, is it the right sql ????

Answer Source

Best way for your case is to use the query that mention the columns like below :

$sql = "INSERT INTO student_info (id, firstname, ..... ) VALUES ('".$id."',".$firstname."','".$lastname."','".$sex."','".$bloodgroup."','".$fathername."','".$mothername ."','".$fatherjob."','".$motherjob."','".$address."','".$birthday."','".$phone."','".$mobile1."','".$mobile2."','".$class."','".$section."','".$roll."')";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download