Jump to content

Problem inserting in database


allexx_d

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.