【问题标题】:Can't Insert into mySQLi DB using Prepared Statements无法使用准备好的语句插入 mySQLi DB
【发布时间】:2014-03-29 19:13:53
【问题描述】:

我改进了我的 mysqli 数据库,以使用准备好的语句将值从我的表单插入到数据库中。 感谢 stackoverflow - 我通过阅读本网站了解到这是要走的路。

将我的语句更改为如下所示,但由于某种原因它们没有插入到数据库中。我可能做错了什么?我在字符串周围添加了 '' 但这不起作用......不确定我可能会丢失什么。我从一个教程开始,一直在研究这组代码。

任何帮助我的插入工作将不胜感激,我是 mysqli 准备语句的新手。这是我的代码:

  <?php
  $con= new mysqli("localhost","admin1","default","sourcesDB");
  // Check connection

  if (mysqli_connect_erno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
  }

  $biotext = $_POST[$bio];

        $keywords= json_encode($_POST['Keyword_ID']);
        $assocs= json_encode($_POST['Associations']);
        $fname = $_Post['FName'];
        $mname = $_POST['MName'];
        $lname = $_POST['LName'];
        $suffix = $_POST['Suffix'];
        $dept = $_POST['Dept'];
        $title = $_POST['Title'];
        $title2 = $_POST['Title2'];
        $title3 = $_POST['Title3'];
        $edu = $_POST['Education'];
        $edu2 = $_POST['Education2'];
        $edu3 = $_POST['Education3'];
        $ph1 = $_POST['PH1'];
        $ph2 = $_POST['PH2'];
        $email = $_POST['Email'];
        $pic = $_POST['Pic'];
        $linkname = $_POST['LinkName'];
        $website = $_POST['Website'];
        $tags = $_POST['Tags'];
        $bio = $_POST['bioLinks'];


  $stmt = $con->prepare('INSERT IGNORE INTO profileTable
   (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ');

  $stmt->bind_param('sssssssssssssssssssss', $fname, $mname, $lname, $suffix, $dept,
  $title, $title2, $titl3, $edu, $edu2, $edu3, $ph1, $ph2, $email,  $pic, $linkname,
  $bio, $website, $keywords, $tags, $assocs);

  if($stmt->execute()) {
echo '<div style="color: green;">Profile Added!</div>';
echo "<meta http-equiv='Refresh' content='4; URL=addProfile.php'>";
  }
  else {
echo "Failed to insert";
  }

  $stmt->close();

【问题讨论】:

  • $title3$titl3 不匹配。如果/当一个失败,那么他们都倒下了。
  • good catch @Fred-ii- 我确实改变了这一点,但仍然没有插入。我有一个问题......如果我的数据库中的第一列是“id”,这会设置插入的顺序吗?
  • 无法确定。太多可能的原因在起作用。 @MizAkita
  • 谢谢@YourCommonSense 这是mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);在 $stmt->close() 之前插入;功能?如果是这样,它对我没有任何显示...

标签: php mysqli prepared-statement


【解决方案1】:

我能够自己回答这个问题...我添加了要插入的列的名称,然后将我的参数绑定到我的值。最后,这就是最终完美无缺的工作......

    if ($stmt = $con->prepare("INSERT INTO profileTable (FirName, MName, LaName,
         Suffix, Dept, Title, Title2, Title3, Education, Education2, Education3,
         PH1, PH2, Email, Photo, BioLK, Website, Keyword_ID, Tags, Associations) 
          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));

         {
      $stmt->bind_param("ssssssssssssssssssss", $fname, $mname, $lname, $suffix, 
         $dept, $title, $title2, $title3, $edu, $edu2, $edu3, $ph1, $ph2, $email,
         $linkname, $bio, $website, $keywords, $tags, $assocs);

      $stmt->execute();
         echo '<div style="color: green;">Profile $fname $lname Added!</div>';
         echo "<meta http-equiv='Refresh' content='4; URL=addProfile.php'>";

       $stmt->close();

       mysqli_close($con);
        }

【讨论】:

    猜你喜欢
    • 2014-03-16
    • 2012-05-15
    • 2016-04-05
    • 1970-01-01
    • 2014-05-26
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多