有人可以向我解释为什么这不起作用。试图按照每个人的建议转换为准备好的语句,但一开始就卡住了……连接很好,没有返回任何消息,但我的表中没有条目(称为名称表)

<?php
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "fidelio";
 $dbname = "test";
 $con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
 if(mysqli_connect_errno()) {
 die("Database connection failed: " .
     mysqli_connect_error() .
     " (" . mysqli_connect_errno() . ")");
   }

$query = "INSERT INTO nametable (fname, lname) values (?,?)";
$stmt = mysqli_prepare($con, $query);
$firstName = "simon";
$lastName = "morris";
mysqli_stmt_bind_param($stmt,"ss",$firstname, $lastname);
mysqli_stmt_execute($stmt);
printf("Error: %s.\n", $stmt->error);
$stmt->close();
?>

我添加了最后两行,返回的错误是



这工作得很好,但准备好的语句没有......有人知道为什么吗?
  $sql="INSERT INTO nametable (fname, lname)
  VALUES ('$firstName', '$lastName')";
   if (!mysqli_query($con,$sql))
{
   die('Error: ' . mysqli_error($con));
 }

最佳答案

试试这个

    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "fidelio";
    $dbname = "test";

    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

     $firstName = "simon";
     $lastName = "morris";
          if ($stmtb = $mysqli->prepare("INSERT INTO nametable (fname, lname) values (?,?)")) {
              $stmtb->bind_param('ss',$firstName, $lastName);
              $stmtb->execute();
           }else {printf("Prepared Statement Error: %s\n", $mysqli->error);}

关于mysql - mysqli 中准备好的语句不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17816222/

10-10 16:21