allexx_d Posted December 3, 2008 Share Posted December 3, 2008 Hello, I have a problem inserting some data in database. I have the folowing code: echo' <table border="0" cellspacing="0" cellpadding="0" id="shoppingCart" width=""> <tr> <th>Denumire produs</th> <th>Cantitate</th> <th>Pret</th> </tr><tr><td> '; foreach ($_POST["product_name"] as $nume_produs) { echo ' '. $nume_produs . '<br>'; } echo"</td><td>"; foreach ($_POST["cantitate_produs"] as $cantitate) { echo ' ' . $cantitate . '<br>'; } echo"</td><td>"; foreach ($_POST["pret"] as $pret) { echo '' . $pret . ' RON<br>'; } echo'</td></tr></table>'; $data = date("Y/m/d"); $timp = date('h:i:s A'); echo'<br><br><br>'; $conn = mysql_connect(HOST, DBUSER, PASS) or die('Conectarea nu a fost efectuata !<br />Contacteaza administratorul.'); $sql = "insert into comenzi values ('', '$nume', '$telefon', '$email', '$nume_facturare', '$cnp', '$buletin', '$firma', '$regcom', '$cui', '$banca', '$cont', '$adresa', '$nume_produs', '$data', '$timp', '0')"; //executa instructiunea if (mysql_query($sql, $conn) or die(mysql_error())) { $items == 0; echo '<p>Comanda a fost trimisa! </p>'; } else { die(mysql_error()) ; // echo "A intervenit o eroare. Va rugam reincercati!"; } $nume_produs can have one or more values. If $nume_produs has 2 or more values, running the script will insert inthe database only the last value. I want all the values of $nume_produs to be inserted in the database. Can you help me please? Thank you in advance! Link to comment https://forums.phpfreaks.com/topic/135360-problem-inserting-in-database/ Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 Your insert query is invalid. Inserts must be one of these forms: INSERT INTO table SET column1 = value1, column2 = value2 OR INSERT INTO table (column1, column2) VALUES (value1, value2) Link to comment https://forums.phpfreaks.com/topic/135360-problem-inserting-in-database/#findComment-705028 Share on other sites More sharing options...
allexx_d Posted December 3, 2008 Author Share Posted December 3, 2008 This insert is working, is inserting data in database. The problem is when $nume_produs has more than 2 values. In that case only the last value is inserted in database. I have used this too: $sql = "INSERT INTO tb (id_client, id_comanda, nume_produs, cantitate, pret) VALUE ($id_client, $id_comanda, '$nume_produs', $cantitate, $pret);"; mysql_query($sql); Link to comment https://forums.phpfreaks.com/topic/135360-problem-inserting-in-database/#findComment-705034 Share on other sites More sharing options...
creativeimpact Posted December 3, 2008 Share Posted December 3, 2008 can you give an example of the what $nume_produs would be? Is it like: $nume_pruds = "somepruds1, someotherpruds2" ?? if you have a comma or a quote in the value to separate the values of $nume_pruds you may want to try something like: ...VALUE ($id_client, $id_comanda, ' ".mysql_escape_string($nume_produs)." ', $cantitate, ... hope it helps Link to comment https://forums.phpfreaks.com/topic/135360-problem-inserting-in-database/#findComment-705076 Share on other sites More sharing options...
allexx_d Posted December 3, 2008 Author Share Posted December 3, 2008 I have solved the problem with concatenation. $nume_produs = 'something'; $nume_produs = $nume_produs. "some echo"; ...and so on. Thank you for you help! Link to comment https://forums.phpfreaks.com/topic/135360-problem-inserting-in-database/#findComment-705190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.