doz95923 2014-04-19 18:24
浏览 41
已采纳

将多个值插入数据库,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:

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

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>
  • 写回答

1条回答 默认 最新

  • dsaf32131 2014-04-19 18:27
    关注

    You're re-assigning $query here:

    $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.

    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

    Also, to get multiple points, change your html element from:

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

    to

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

    Notice the name with an array spoints[]. When posted, the $_POST['spoints'] will be an array you can loop through and add records with.

    $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());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)