簡體   English   中英

將多個值插入數據庫,php,sql

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

我在嘗試將多個值同時插入表中的同一列時遇到問題,

此代碼顯示了一個表:表格Ex:

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

useradmin ca為每個用戶插入點,但是當我單擊summint將所有這些值插入數據庫時​​我發出消息(錯誤)PDO :: prepare()期望參數1為字符串,數組給定,另一個為Fatal錯誤:在非目標上調用成員函數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>

你在這里重新分配$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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM