Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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());
}
share|improve this answer
    
I didn't see that, it was a typo, thank you. –  Carlos Perez Apr 19 '14 at 18:31
    
@CarlosPerez sure thing. also, I edited the answer a bit, fyi –  joseph4tw Apr 19 '14 at 18:32
    
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 –  Carlos Perez Apr 19 '14 at 18:35
    
@CarlosPerez re-edited :) –  joseph4tw Apr 19 '14 at 18:42
    
@CarlosPerez also, check out: us3.php.net/pdo.prepared-statements –  joseph4tw Apr 19 '14 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.