37088

error inserting multiple values database php

Question:

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>

Answer1:

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

Recommend

  • Splitting model instance for serializer into 3 different fields
  • How do I get the Vendor and Product strings in case of a HID device on Windows?
  • Classification of Blackberry devices by using useragent
  • C# WebBrowserDocumentCompletedEventHandler inside of foreach loop
  • Getting the id of the last inserted record from an MSSQL table using PDO and PHP
  • ASP.NET C# - Dropdown list by using User Control
  • MySQLi using an IN in a where
  • c++ create an array with string::size
  • Using Select and where statement in Criteria
  • HSQLDB Statement and Java NaN doubles
  • VB.NET: How do I use coalesce with db column values and nullable types? Or is there a better solutio
  • Show records ordered with maximum price first in PHP & MySQL
  • Importing Excel files with a large number of columns header into mysql with c#
  • How can I select the most recent and distinct records using LINQ?
  • Laravel lmutator $this->attributes return 'Undefined index: id'
  • MongoDB GeoJSON “Can't extract geo keys from object, malformed geometry?” when inserting type P
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • Count from each distinct date, fill in missing dates with zero
  • MVC - @Html.CheckBoxFor
  • Get used tables from sql query [duplicate]
  • pymongo replication secondary readreference not work
  • sweetalert2 inputoptions from file in select example
  • Find JSON nested nodes using multiple string values
  • Hector: how to query parts of a Composite Type
  • Open Existing DB in MySQL WorkBench
  • converting text file into xml using php?
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?