-1

我试图在使用 查询后获取 id mysql_insert_id();,但我仍然得到 0 尽管事实上

  1. 我把它放在查询本身之后
  2. 我已确保 id(称为 P_Id)具有 AUTO_INCREMENT。

下面的代码:

$con=mysqli_connect(-connectiondetails-);
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO clients (Name, Email, Address, Phone, Date, Service, ExtraOne, ExtraTwo, ExtraThree, ExtraFour, ExtraFive) VALUES ('$_POST[name]','$_POST[email]','$_POST[address]','$_POST[phone]','$_POST[date]','$_POST[service]','$_POST[extra1]','$_POST[extra2]','$_POST[extra3]','$_POST[extra4]','$_POST[extra5]')";
$idit = mysql_insert_id();
echo $idit;



if (mysqli_query($con,$sql))
{

}
else
{
    echo "Error message goes here: " . mysqli_error($con);
}

“哦,好吧,也许我必须先实际查询它”,我做了以下但得出了相同的结果:

$con=mysqli_connect(-connectiondetails-);
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO clients (Name, Email, Address, Phone, Date, Service, ExtraOne, ExtraTwo, ExtraThree, ExtraFour, ExtraFive) VALUES ('$_POST[name]','$_POST[email]','$_POST[address]','$_POST[phone]','$_POST[date]','$_POST[service]','$_POST[extra1]','$_POST[extra2]','$_POST[extra3]','$_POST[extra4]','$_POST[extra5]')";



if (mysqli_query($con,$sql))
{
    $idit = mysql_insert_id();
    echo $idit;

}
else
{
    echo "Error message goes here: " . mysqli_error($con);
}

知道我哪里出错了吗?我已经经历了其他线程,但似乎没有什么对我有用。先谢谢了。

编辑:我改变了 mysql_insert_id(); 到 mysqli_insert_id(); 这一次它甚至没有返回 0,只是空白。

编辑2:谢谢mbouzahir-您的解决方案有效:)

4

3 回答 3

4

在第二个示例中尝试使用mysqli_insert_id($con)而不是mysql_insert_id()

于 2013-08-28T03:01:51.210 回答
0

您的查询容易受到 SQL 注入的攻击。您应该使用带有参数绑定的准备好的语句。试试这个(是的,我正在使用 OOP API,因为它是一个该死的网站清洁器)

$con = new mysqli(-connectiondetails-);
if ($con->connect_errno) {
    throw new Exception($con->connect_error, $con->connect_errno);
}

$stmt = $con->prepare(
    'INSERT INTO clients (Name, Email, Address, Phone, Date, Service, ExtraOne, ExtraTwo, ExtraThree, ExtraFour, ExtraFive)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');

if ($stmt === false) {
    throw new Exception($con->error);
}

// you should probably check here that all required POST params are present

$stmt->bindParam('sssssssssss',
    $_POST['name'],
    $_POST['email'],
    $_POST['address'],
    $_POST['phone'],
    $_POST['date'],
    $_POST['service'],
    $_POST['extra1'],
    $_POST['extra2'],
    $_POST['extra3'],
    $_POST['extra4'],
    $_POST['extra5']);

if (!$stmt->execute()) {
    throw new Exception($stmt->error);
}

echo $con->insert_id;
于 2013-08-28T03:24:45.993 回答
-1

试试这个

$con=mysqli_connect(-connectiondetails-);
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO clients (Name, Email, Address, Phone, Date, Service, ExtraOne, ExtraTwo, ExtraThree, ExtraFour, ExtraFive) VALUES ('$_POST[name]','$_POST[email]','$_POST[address]','$_POST[phone]','$_POST[date]','$_POST[service]','$_POST[extra1]','$_POST[extra2]','$_POST[extra3]','$_POST[extra4]','$_POST[extra5]')";



if (mysqli_query($sql,$con))
{
    $idit = mysqli_insert_id();
    echo $idit;

}
else
{
    echo "Error message goes here: " . mysql_error($con);
}
于 2013-08-28T03:27:43.790 回答