0

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

1 Answer 1

2

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: https://www.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());
}
Sign up to request clarification or add additional context in comments.

8 Comments

@CarlosPerez sure thing. also, I edited the answer a bit, fyi
Did you how can I insert multiple values, If you see my table I can insert point for multiple users, with what I have now it only read the first value and is the only one that appears in the database
@CarlosPerez cool. made one minor adjustment. that should get you going
and to insert multiple id, i only need to follow the same?
I'm having so much troubles to do that, sorry to ask you again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.