【问题标题】:Trying to Prepare a Statement To Store In Database尝试准备要存储在数据库中的语句
【发布时间】:2015-09-28 01:15:28
【问题描述】:

好的。我有一个带有两个输入字段的表单。一个是捕获日期,另一个是“目标标题”的文本字段。按下时,我有按钮可以单击以添加另一个“目标标题”字段。提交表单时,将有 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 脚本将最后一个“goal_title”行存储到数据库中,而不是全部存储到数据库中,它只捕获该“goal_title”的第一个字母。

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

【问题讨论】:

  • 您需要使用带有方括号的表单名称发送数据数组,例如name="goal_title[]"
  • 好吧,我会的。 ;) 谢谢老兄!

标签: javascript php mysqli


【解决方案1】:

要详细说明我的评论,您需要发送一组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();
}

【讨论】:

  • 谢谢兄弟。绝对是这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
相关资源
最近更新 更多