mysql_insert_id();继续返回'0'

时间:2013-08-28 02:58:19

标签: php mysqli

我试图在使用mysql_insert_id();的查询后获取id,但是我仍然在0,尽管

  1. 我把它放在查询本身后面
  2. 我确保id(称为P_Id)具有AUTO_INCREMENT。
  3. 以下代码:

    $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 - 您的解决方案有效:)

3 个答案:

答案 0 :(得分:4)

在第二个例子中使用mysqli_insert_id($con)代替mysql_insert_id()进行了尝试?

答案 1 :(得分: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;

答案 2 :(得分:-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);
}