简体   繁体   中英

mysql_insert_id return 0 even after a successful insert

I have made sure the id in table1 is primary and auto-incrementing. Even the insert is happening successfully. However, in table2 id is 0. Also, mysqli_real_escape_string is not working with $_POST['xyz1'] in this case.

<?php
include ('db.php');
session_start();

$q1="insert into xyz (email, content) values          ('".$_SESSION['Email']."','".$_POST['xyz1']."')";
$query1=mysqli_query($pqr,$q1);
$a=mysqli_insert_id();

$q2="insert into xyz2 (email,content2,id) values ('".$_SESSION['Email']."','".$_POST['xyz3']."', '".$a."')";
$query2=mysqli_query($pqr,$q2);



echo "done";
?>

You obviously need the mysqli_insert_id() function of mysqli, you missed the i .

Also: Switch to Prepared Statements, instead of using mysqli_real_escape_string . They are far superior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM