0

Can someone explain me why this is not working, Im trying to insert multiple values into a database, first I was inserting carpirces only and was working, but now Im trying to insert also de Ids but now the code don't work

if(!empty($_POST))
{ 
  $query = "INSERT INTO prices (carid, vendorid, carprice) values (:carid, 2, :carprice)";
  $query_params = array(':carprice' => $_POST['carprice']);
  $price = null;
  $carids = null;
  try
  {
    $stmt = $db->prepare($query); 
    $stmt->bindParam(':carprice', $price);  
    foreach($_POST['carprice'] as $value) { 
            $price = $value;
            $stmt->execute();
    }
      $stmt->bindParam(':carid', $carids);  
    foreach($_POST['carid'] as $value) { 
            $carids = $value;
            $stmt->execute();
    }

  }
  catch(PDOException $ex)
  {
    die("Error 1 " . $ex->getMessage());
  } 
  header("Location: update.php");
  die("Rendirecting to update.php");
}
?>
<form action="prices.php" method="post">
<table border=1>
  <tr>
    <th>Id</th>
    <th>car</th>
    <th>model</th>
    <th>Price</th>
  </tr>
<?php foreach($rowscars as $row): ?>
  <tr>
    <th><input type="hidden" name="carid[]" value="<?php echo ' ' . htmlentities($row['carid'], ENT_QUOTES, 'UTF-8') . ' ';?>" /><?php echo '' . htmlentities($row['carid'], ENT_QUOTES, 'UTF-8') . '';?></th>
    <th><?php echo '' . htmlentities($row['car'], ENT_QUOTES, 'UTF-8') . '';?></th>
    <th><?php echo '' . htmlentities($row['model'], ENT_QUOTES, 'UTF-8') . '';?></th>

    <th><input type="text" name="carprice[]" value=""></th>
  </tr>
<?php endforeach; ?>
</table>
<input type="submit" value="Submit">
</form> 
3
  • I was just curious, what is the error it is throwing? Commented Apr 26, 2014 at 22:28
  • well more that an error is that is not inserting anything into the databse Commented Apr 26, 2014 at 22:29
  • @saurabh it was working fine when I was only inserting the carPrice, but now that Im tying to insert the id, is when nothing is happenning Commented Apr 26, 2014 at 22:37

1 Answer 1

1

I suppose you want to insert carPrice for specified carId. First you need to map each car id to car price, do smth like this:

if (!empty($_POST['carid'] && $_POST['carprice']) {
   $carPrices = array_combine($_POST['carid'], $_POST['carprice']);
   foreach ($carPrices as $carId => $carPrice) {
      $stmt = $db->prepare($query); 
      $stmt->bindParam(':carprice', $carPrice);
      $stmt->bindParam(':carid', $carId);
      $stmt->execute();
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for my late response, but thank you so much, I spend like 3 hours try to this what you just did
@CarlosPerez You are welcome;) Please, close the question by accepting my answer as correct.

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.