Double Insert with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Double Insert with PHP

Post 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)
                ?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Double Insert with PHP

Post by josh »

So what's your question?
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: Double Insert with PHP

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Double Insert with PHP

Post 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()
Post Reply