如何避免Query将INSERT加入数据库?

时间:2015-12-13 18:52:35

标签: php mysql database

问题:
我做了一个简单的表单,使用PHP通过INSERT查询将信息传递给我的数据库。但是,每次运行它时,它都会尝试将信息放入两次。我怎么能避免这个?

解释
我首先将答案插入我的答案表,将AnswerID保存为变量。然后使用我的问题表进行保存,最后我将包含ID的两个已保存变量用于我的question_answers表。

我的代码:

if (isset($_POST['textinput1']) && !empty($_POST['textinput1'])) {

      $text1 = mysqli_real_escape_string($conn, $_POST['textinput1']);
      $text2 = mysqli_real_escape_string($conn, $_POST['textinput2']);
      $q_text = mysqli_real_escape_string($conn, $_POST['textarea']);

      $stmt = $conn->prepare("INSERT INTO answers (Answer1Text, Answer2Text) VALUES (?, ?)");
      $stmt->bind_param('ss', $text1, $text2);
      $stmt->execute();
      $answerid = $stmt->insert_id;
      $stmt = $conn->prepare("INSERT INTO question (QuestionText) VALUES (?)");
      $stmt->bind_param('s', $q_text);
      $stmt->execute();
      $questionid = $stmt->insert_id;
      if ($stmt->execute()) {
        $stmt = $conn->prepare("INSERT INTO question_answers (AnswerID, QuestionID) VALUES (?, ?)");
        $stmt->bind_param('ss', $answerid, $questionid);
        $stmt->execute();
        echo "<h2>Dit spørgsmål er nu lagt op på siden!</h2>";
        echo "<h3>Tusinde tak for din interesse for SMIL - Skodfri Århus.</h3>";
      }
      else
      {
        echo "ERROR: Could not able to execute . " . mysqli_error($conn);
      }
    }

    // close connection
    mysqli_close($conn);
    ?>

我的重要表:
问题: QuestionID(PK),QuestionText
答案: AnswerID(PK),Answer1Text,Answer2Text
question_answers: QuestionAnswerID(PK),QuestionID(FK),AnswerID(FK)

聚苯乙烯。我不想使用复合唯一约束作为解决方案。

另外还有一个问题,$stmt->insert_id个变量应该是mysqli_real_escape_string吗?

2 个答案:

答案 0 :(得分:3)

您的问题是您已执行第二次查询TWICE

if (isset($_POST['textinput1']) && !empty($_POST['textinput1'])) {

      $text1 = mysqli_real_escape_string($conn, $_POST['textinput1']);
      $text2 = mysqli_real_escape_string($conn, $_POST['textinput2']);
      $q_text = mysqli_real_escape_string($conn, $_POST['textarea']);

      $stmt = $conn->prepare("INSERT INTO answers (Answer1Text, Answer2Text) VALUES (?, ?)");
      $stmt->bind_param('ss', $text1, $text2);
      $stmt->execute();
      $answerid = $stmt->insert_id;
      $stmt = $conn->prepare("INSERT INTO question (QuestionText) VALUES (?)");
      $stmt->bind_param('s', $q_text);
      $stmt->execute();
      $questionid = $stmt->insert_id;

      // THIS IS THE SECOND EXECUTION OF QUERY 2
      if ($stmt->execute()) {
        $stmt = $conn->prepare("INSERT INTO question_answers (AnswerID, QuestionID) VALUES (?, ?)");
        $stmt->bind_param('ss', $answerid, $questionid);
        $stmt->execute();
        echo "<h2>Dit spørgsmål er nu lagt op på siden!</h2>";
        echo "<h3>Tusinde tak for din interesse for SMIL - Skodfri Århus.</h3>";
      }
      else
      {
        echo "ERROR: Could not able to execute . " . mysqli_error($conn);
      }
    }

    // close connection
    mysqli_close($conn);
    ?>

而是将其作为IF测试

    //if ($stmt->execute()) {
    if ( isset($answerid,$questionid) ) {

答案 1 :(得分:1)

if ($stmt->execute()) {

这会再次运行您的一个陈述。如果稍后需要将返回值分配给变量,则应将其返回值。