尝试准备要存储在数据库中的语句

时间:2015-09-28 01:15:29

标签: javascript php mysqli

确定。我有一个带有两个输入字段的表单。一个是捕捉日期,另一个是"目标标题"的文本字段。我有按钮点击添加另一个"目标标题"按下时的字段。在表单提交时,将有2到21个条目。

(将有一个单独的日期条目适用于所有"目标标题"以及多达20"目标标题"。

表单的html如下所示:

<form action="handlers/add-goals.php" method="POST">
    <h1>Add Goals</h1>
    <input type="hidden" name="assigned_by_user_id" value="<?php echo $user_id; ?>">
    <input type="hidden" name="assign_to_user_id" value="<?php echo $user_id; ?>">

    <div class="form-group">
        <label>What Date Are You Adding Goals For?</label>
        <input class="form-control datepicker" type="text" name="due_date" required>
    </div>
    <div class="form-group">
        <label>Goal #1:</label>
        <input class="form-control" type="text" name="goal_title" required>
    </div>
    <div id="dynamicInput"></div>
    <div class="form-group">
        <input class="form-control btn btn-default" type="button" value="Add Another Goal Area" onClick="addInput('dynamicInput');">
    </div>
    <input type="submit" class="btn btn-primary" name="submit" value="Add Goal(s)">
</form>

然后我们有这样的javascript:

var counter = 1;
var limit = 20;
function addInput(divName){
 if (counter == limit)  {
      alert("You have reached the limit of adding " + counter + " inputs");
 }
 else {
      var newdiv = document.createElement('div');
      newdiv.innerHTML = "<div class='form-group'><label>Goal # " + (counter + 1) + "</label><input class='form-control' type='text' name='goal_title' required></div>";
      document.getElementById(divName).appendChild(newdiv);
      counter++;
 }
}

最后,它提交的PHP表单是:

<?php

include('../../includes/db_con.php');
$due_date = $_POST['due_date'];
$date_assigned = date("Y-m-d");

$goal_titles = $_POST['goal_title'];


if($due_date<$date_assigned) {
    echo '<script> alert("Sorry. You can not make a goal due before today\'s date!"); </script>';
    echo '<script> window.location.href="../add-goal.php"; </script>';
}
else {
    $stmt = $con->prepare("INSERT INTO goals (title, instructions, belongs_to, assigned_by, due_date, date_assigned, completed, completed_on, deleted, notes) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die("Error: " . $con);

    for ($i=0; $i<count($goal_titles); $i++) {
        $title = $goal_titles[$i];
        $instructions = '';
        $assigned_by_user_id = $_POST['assigned_by_user_id'];
        $assigned_to_user_id = $_POST['assign_to_user_id'];
        $due_date = $_POST['due_date'];
        $date_assigned = date("Y-m-d");
        $completed = 'NO';
        $completed_on = '';
        $deleted = '0';
        $notes = '';
        $stmt->bind_param('ssssssssss', $title, $instructions, $assigned_to_user_id, $assigned_by_user_id, $due_date, $date_assigned, $completed, $completed_on, $deleted, $notes);

        $stmt->execute();
    }

    $stmt->close();
    echo '<script> alert("Added to your goals!"); </script>';
    echo '<script> window.location.href="../add-goal.php"; </script>';
}


?>

我所遇到的是php脚本正在存储最后一个&#34; goal_title&#34;行到数据库而不是所有数据库,它只捕获&#34; goal_title&#34;的第一个字母。

我假设我的错误出现在php脚本中,但包含所有内容,以便您可以看到正在发生的一切。

1 个答案:

答案 0 :(得分:3)

要详细说明我的评论,您需要发送一组goal_title数据。通过在表单元素name属性中添加方括号,告诉PHP数据是一个数组。例如

<div class="form-group">
    <label>Goal #1:</label>
    <input class="form-control" type="text" name="goal_title[]" required>
</div>

并在你的JS中

...<input class='form-control' type='text' name='goal_title[]' required>...

需要注意的另一件事是,语句只需要准备并绑定一次。 bind_param中使用的变量通过引用传递,因此它们的值可能会更改。

$stmt = $con->prepare("INSERT INTO goals (title, instructions, belongs_to, assigned_by, due_date, date_assigned, completed, completed_on, deleted, notes) values (?, ?, ?, ?, ?, NOW(), ?, ?, ?, ?)");

$instructions = '';
$assigned_by_user_id = $_POST['assigned_by_user_id'];
$assigned_to_user_id = $_POST['assign_to_user_id'];
$due_date = $_POST['due_date'];
$completed = 'NO';
$completed_on = '';
$deleted = '0';
$notes = '';

// note that $title may be unassigned at this point
$stmt->bind_param('sssssssss', $title, $instructions, $assigned_to_user_id, $assigned_by_user_id, $due_date, $completed, $completed_on, $deleted, $notes);

foreach($goal_titles as $title) {
    $stmt->execute();
}