Page 1 of 1

Double Insert with PHP

Posted: Tue Sep 09, 2008 10:02 pm
by rooky
Hi guys,
I need your help please with the following code. I'm trying to insert into two tables at the same time, in one table I'm only inserting one line and in the other one I need to insert many rows.
This is the code:

Code: Select all

<?php $con=mysql_connect("host","username","password");
                if (!$con)
                {
                die('La conexion no fue posible: ' .mysql_error());
                }
                mysql_select_db("databasename",$con);
                $sql="INSERT INTO table1 (id_tour, descrip_tour, fecha, guia) VALUES ('$_POST[id_tour]','$_POST[descrip_tour]','$_POST[fecha]','$_POST[guia]')";
                echo "<pre>";
 print_r($_POST['tour']);
echo "</pre>";
 
for($i = 0; $i < count($_POST['id_tour']); $i++)
{
 $sql = "INSERT INTO table2 (id_tour, idioma, habitacion, cliente, voucher) VALUES ('$_POST[id_tour[$i]]','$_POST[idioma[$i]]','$_POST[habitacion[$i]]','$_POST[cliente[$i]],'$_POST[voucher[$i]])";
 echo $sql;
}
                if (!mysql_query($sql,$con))
                {
                die('Error: ' . mysql_error());
                }
                echo "1 record grabado";
                mysql_close($con)
                ?>

Re: Double Insert with PHP

Posted: Tue Sep 09, 2008 10:30 pm
by josh
So what's your question?

Re: Double Insert with PHP

Posted: Wed Sep 10, 2008 10:19 am
by rooky
Hi jshpro2,
That code that I sent is not working and is giving me an error, I just would like to know if anyone can help me to find the problem on that code.

Re: Double Insert with PHP

Posted: Thu Sep 11, 2008 7:21 am
by josh
Oh well you didn't say that. In my editor I show a parse error on your string usage. Perhaps you'd see the error better if you used the concat operator or perhaps spritnf() to build your query. Or read up on how PHP handles strings

Hint:

$var = ' some static test ' . $_POST['val'].

Also you should pass all user input through mysql_real_escape_string()