无法使用准备语句插入mySQLi数据库

时间:2014-02-26 18:35:46

标签: php mysqli prepared-statement

我修改了我的mysqli db,使用预处理语句将表单中的值插入到db中。 感谢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();

1 个答案:

答案 0 :(得分:0)

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

    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);
        }