简体   繁体   English

将多个值插入数据库,php,sql

[英]Inserting multiple values into a database, php , sql

Im having a problem trying to insert multiple values at the same time into the same column in a table, 我在尝试将多个值同时插入表中的同一列时遇到问题,

This code shows a table: Table Ex: 此代码显示了一个表:表格Ex:

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

The useradmin ca insert points for each user, but when I click summint to insert all of those values into the database I gt a message(error) PDO::prepare() expects parameter 1 to be string, array given in and another one Fatal error: Call to a member function execute() on a non-objec useradmin ca为每个用户插入点,但是当我单击summint将所有这些值插入数据库时​​我发出消息(错误)PDO :: prepare()期望参数1为字符串,数组给定,另一个为Fatal错误:在非目标上调用成员函数execute()

Any ideas why or how to fixed? 任何想法为什么或如何修复?

<?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>

You're re-assigning $query here: 你在这里重新分配$query

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

So, after the second line, $query becomes an array with one element. 因此,在第二行之后, $query变为一个包含一个元素的数组。

I think you meant to do this: 我想你打算这样做:

$query = "INSERT INTO points (sid, ais, spoints) values (1, 2, :spoints)";

try
  {
    $stmt = $db->prepare($query);
    $stmt->bindParam(':spoints', $_POST['spoints']);
    $stmt->execute();
  }

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

Also, to get multiple points, change your html element from: 此外,要获得多个点,请更改您的html元素:

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

to

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

Notice the name with an array spoints[] . 注意带有数组spoints[]的名称。 When posted, the $_POST['spoints'] will be an array you can loop through and add records with. 发布时, $_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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM