简体   繁体   中英

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:

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

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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