【问题标题】:Inserting multiple values into a database, php , sql将多个值插入数据库,php,sql
【发布时间】:2014-04-19 18:24:23
【问题描述】:

我在尝试将多个值同时插入表的同一列时遇到问题,

这段代码显示了一个表格: 表例:

-----------------------------
Name   | Last Name | Points |
-----------------------------
Test   | 185       |        |
-----------------------------
Test1  | 185       |        |
-----------------------------
Test2  | 185       |        |
-----------------------------

useradmin 可以为每个用户插入点,但是当我单击 summint 将所有这些值插入数据库时​​,我会收到一条消息(错误) PDO::prepare() 期望参数 1 是字符串,数组在 还有一个 致命错误:在非对象上调用成员函数 execute()

任何想法为什么或如何修复?

<?php
require("coneccion.php"); 

if(!empty($_POST))
{ 

  $query = "INSERT INTO points (sid, ais, spoints) values (1, 2, :spoints)";
  $query = array(':spoints' => $_POST['spoints']);

  try
  {
    $stmt = $db->prepare($query);
    $stmt = $stmt->execute($query_params);
  }
  catch(PDOException $ex)
  {
    die("Error 1 " . $ex->getMessage());
  }
  $cid = $_SESSION['cid'];
  header("Location: index.php?id=$cid");
  die("Rendirecting to index.php?id=$cid");
}
else
{

  $id = $_SESSION['cid'];
  echo 'Course id: ' .$id ;
  $sid = $_GET['id'];

  $query = "SELECT DISTINCT s.studentid, s.fname, s.lname, a.assignmentpoints, s.courseid, a.courseid, a.duedate FROM students as s, assignments as a WHERE s.courseid = '$id' and s.courseid = a.courseid and a.assignmentid = '$sid' ";
  try
  {
    $stmt = $db->prepare($query);
    $stmt->execute();
  }
  catch(PDOException $ex)
  {
    die("Error 2" . $ex->getMessage());
  }
  $rowstudents = $stmt->fetchAll();
}
?>


<form action="index.php" method="post">
<table border=1>
  <tr>
    <th>Id</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Assignment Points</th> 
    <th>Student Points</th>
  </tr>
<?php foreach($rowstudents as $row): ?>
  <tr>
    <th><?php echo '' . htmlentities($row['studentid'], ENT_QUOTES, 'UTF-8') . '';?></th>
    <th><?php echo '' . htmlentities($row['fname'], ENT_QUOTES, 'UTF-8') . '';?></th>
    <th><?php echo '' . htmlentities($row['lname'], ENT_QUOTES, 'UTF-8') . '';?></th>
    <th><?php echo '' . htmlentities($row['assignmentpoints'], ENT_QUOTES, 'UTF-8') . '';?></th> 
    <th><input type="text" name="spoints" value=""></th>
  </tr>
<?php endforeach; ?>
</table>
<input type="submit" value="Submit">
</form>

【问题讨论】:

    标签: php mysql sql pdo


    【解决方案1】:

    你在这里重新分配$query

    $query = "INSERT INTO points (sid, ais, spoints) values (1, 2, :spoints)";
    $query = array(':spoints' => $_POST['spoints']);
    

    所以,在第二行之后,$query 变成了一个包含一个元素的数组。

    我认为你的本意是:

    $query = "INSERT INTO points (sid, ais, spoints) values (1, 2, :spoints)";
    
    try
      {
        $stmt = $db->prepare($query);
        $stmt->bindParam(':spoints', $_POST['spoints']);
        $stmt->execute();
      }
    

    参考:http://us3.php.net/pdo.prepared-statements

    另外,要获得多个点,请将您的 html 元素更改为:

    <input type="text" name="spoints" value="">
    

    <input type="text" name="spoints[]" value="">
    

    注意带有数组spoints[] 的名称。发布后,$_POST['spoints'] 将是一个数组,您可以循环并添加记录。

    $points = null;
    
    $query = "INSERT INTO points (sid, ais, spoints) values (1, 2, :spoints)";
    
    try
    {
        $stmt = $db->prepare($query);
        $stmt->bindParam(':spoints', $points);
        foreach($_POST['spoints'] as $value) {
            $points = $value;
            $stmt->execute();
        }
    }
    catch(PDOException $ex)
    {
        die("Error 1 " . $ex->getMessage());
    }
    

    【讨论】:

    • @CarlosPerez 确定。另外,我对答案进行了一些编辑,仅供参考
    • 你怎么能插入多个值,如果你看到我的表我可以为多个用户插入点,我现在只读取第一个值并且是唯一出现在数据库
    • 谢谢你的帮助,会试试这个:)
    • @CarlosPerez 很酷。做了一个的调整。这应该会让你继续前进
    • 要插入多个id,我只需要跟着一样?
    猜你喜欢
    • 2018-08-31
    • 2013-04-13
    • 2013-01-05
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多