0

Ok...this is my "HELP ME! HELP ME!" thread.:D
I finally got my code to work...almost. The form posts to my database but only the first query data inserts and the last value of the third query. All the form fields are correct and the problem appears to be with my posting script so I'll include just that:

<?php
session_start();

 $dbhost = "localhost";  
 $dbname = "WB_db";   
 $dbuser = "xxxxx"; 
 $dbpass = "xxxxx";
 
  mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
 mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
 
$guest_1 = $_POST['guest_1'];
$guest_2 = $_POST['guest_2'];
$age_1 = $_POST['age_1'];
$age_2 = $_POST['age_2'];
$country = $_POST['country'];
$region = $_POST['region'];
$email = $_POST['email'];

$hedo2_trips = $_POST['hedo2_trips'];
$hedo3_trips = $_POST['hedo3_trips'];
$jamaica_trips = $_POST['jamaica_trips'];
$hedo2_visits = $_POST['hedo2_visits'];
$hedo3_visits = $_POST['hedo3_visits'];
$jamaica_visits = $_POST['jamaica_visits'];
$return_buncher = $_POST['return_buncher'];
$return_trip = $_POST['return_trip'];
$airline = $_POST['airline'];
$flight_number = $_POST['flight_number'];
$arrival = $_POST['arrival'];
$departure = $_POST['departure'];

$login_1 = $_POST['login_1'];
$login_2 = $_POST['login_2'];
$pass_1 = $_POST['pass_1'];
$pass_2 = $_POST['pass_2'];
$comments = $_POST['comments'];

if(!$_POST['Submit']) {
	echo "Please fill out all areas";
	header('Location: addme.php');
}else{
mysql_query("INSERT INTO personal_info (`UserID`,`guest_1`, `age_1`, `guest_2`, `age_2`, `country`, `region`, `email`) 
VALUES 
('NULL','$guest_1','$age_1','$guest_2','$age_2','$country','$region','$email')");


//Second query to execute
mysql_query("INSERT INTO travel_info (``UserID`,`hedo2_trips`,`hedo2_visits`,`hedo3_trips`,`hedo3_visits`,`jamaica_trips`,   `jamaica_visits`, `return_buncher`, `return_trip`, `airline`, `flight_number`, `arrival`, `departure`)
VALUES
('NULL','$hedo2_trips','$hedo2_visits','$hedo3_trips','$hedo3_visits','$jamaica_trips','$jamaica_visits','$return_buncher','$return_trip','$airline','$flight_number','$arrival','$departure')");


//Third query needed to execute
mysql_query("INSERT INTO login_info (`UserID`,`login_1`,`pass_1`,`login_2`,`pass_2`,`comments`)
VALUES
('NULL','$login_1','$pass_1','$login_2','$pass_2','$comments')") or die (mysql_error()); 

     echo "Guests have been added!";
	 header('Location: addme.php');
}

?>

It took me a bit but I finally wrapped my brain around the code I needed to insert my form data. Problem is, due to the constraints of the mySQL database, I was forced to split my database into 3 separate tables (no big deal). Now I have to execute 3 separate queries from the same line of code. From what I've been able to glom off help sites (most notably this one) and tutorials, this is a very difficult task and goes beyond the scope of my skill.
I have tried numerous attacks at this with solutions others have tried and none seems to work for me. Maybe its my syntax or I'm missing something I've probably gone over several hundred times but every time I attempt a solution I am met with errors.
If someone can come up with a solution that works I will put you on my Christmas list.
Remember this (and my previous) thread because one of these days I will be a PHP guru and people will flock from miles around to witness my sage advice...or maybe I'll be able to competently write a few lines of code whenever I need to build a site. ;)

2
Contributors
6
Replies
9
Views
6 Years
Discussion Span
Last Post by HedoBum
0
  • You mustn't send anything to output before the "header()" function. Not even white-spaces.
  • Your second query had this: ``UserID`. That's not good, is it?
  • Don't ever insert plain user input. You're opening yourself to a good lil' chapter named "SQL Injection". Use mysql_real_escape_string() and clean with trim().
  • Use isset().

Try this code and see what if it works for you.

<?php

session_start();

$dbhost = "localhost";
$dbname = "WB_db";
$dbuser = "xxxxx";
$dbpass = "xxxxx";

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());


if (!isset($_POST['Submit'])) {

    header('Location: addme.php');

} else {

    foreach ($_POST as $i => $c) {
        $$i = trim(mysql_real_escape_string($_POST[$i]));
    }

    $sql = "INSERT INTO personal_info
               (`UserID`,`guest_1`, `age_1`, `guest_2`, `age_2`, `country`, `region`, `email`)
               VALUES ('NULL','$guest_1','$age_1','$guest_2','$age_2','$country','$region','$email'); ";

    //Second query to execute

    $sql .= "INSERT INTO travel_info
            (`UserID`, `hedo2_trips`, `hedo2_visits`, `hedo3_trips`, `hedo3_visits`, `jamaica_trips`, `jamaica_visits`, `return_buncher`, `return_trip`, `airline`, `flight_number`, `arrival`, `departure`)
            VALUES
            ('NULL', '$hedo2_trips', '$hedo2_visits', '$hedo3_trips', '$hedo3_visits', '$jamaica_trips', '$jamaica_visits', '$return_buncher', '$return_trip', '$airline', '$flight_number', '$arrival', '$departure'); ";

    //Third query needed to execute

    $sql .= "INSERT INTO login_info
            (`UserID`, `login_1`, `pass_1`, `login_2`, `pass_2`, `comments`)
            VALUES
            ('NULL', '$login_1', '$pass_1', '$login_2', '$pass_2', '$comments')";

    mysql_query($sql) or die(mysql_error());

    header('Location: addme.php');
}
?>

Practically, what I did was concatenate the 3 queries. In mysql, a command ends with a semicolon. So, it looks like this:

$query = "command_1; ";
$query .= "command_2; ";
$query .= "command_3";

echo $query; // outputs: command_1; command_2; command_3

// note: 
$x .= "string";
// ...and
$x = $x . "string"
//... are the same thing
0

Crapola:
Notice: Undefined variable: return_buncher in C:\wamp\www\WB_Signup\update.php on line 33

Notice: Undefined variable: login_1 in C:\wamp\www\WB_Signup\update.php on line 40

Notice: Undefined variable: pass_1 in C:\wamp\www\WB_Signup\update.php on line 40

Notice: Undefined variable: login_2 in C:\wamp\www\WB_Signup\update.php on line 40

Notice: Undefined variable: pass_2 in C:\wamp\www\WB_Signup\update.php on line 40
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO travel_info (`UserID`, `hedo2_trips`, `hedo2_visits`, `hedo3_tr' at line 30

I don't think it liked your code one bit. Do you think it would be possible to essentially encapsulate the queries in tags and close them each time?

eg.
<?php
$sql = "INSERT INTO personal_info
(`UserID`,`guest_1`, `age_1`, `guest_2`, `age_2`, `country`, `region`, `email`)
VALUES ('NULL','$guest_1','$age_1','$guest_2','$age_2','$country','$region','$email'); ";

mysql_close();

?>

<?php
$sql .= "INSERT INTO travel_info
(`UserID`, `hedo2_trips`, `hedo2_visits`, `hedo3_trips`, `hedo3_visits`, `jamaica_trips`, `jamaica_visits`, `return_buncher`, `return_trip`, `airline`, `flight_number`, `arrival`, `departure`)
VALUES
('NULL', '$hedo2_trips', '$hedo2_visits', '$hedo3_trips', '$hedo3_visits', '$jamaica_trips', '$jamaica_visits', '$return_buncher', '$return_trip', '$airline', '$flight_number', '$arrival', '$departure') ";

mysql_close;

?>

<?php
$sql .= "INSERT INTO login_info
(`UserID`, `login_1`, `pass_1`, `login_2`, `pass_2`, `comments`)
VALUES
('NULL', '$login_1', '$pass_1', '$login_2', '$pass_2', '$comments')";

?>

So it would be running the query, closing, then running the next, closing, then running the last...or does the script just shut down as soon as it gets a close command?

0

Never mind...there really is such thing as a silly question!

0

You cant have multiple queries in one mysql_query command:

//you can't do this:
mysql_query("SELECT * FROM `table` where `id` = 2; SELECT * FROM `table` WHERE `film` = 'Reservoir Dogs'");

// this works:
mysql_query("SELECT * FROM `table` where `id` = 2");
mysql_query("SELECT * FROM `table` WHERE `film` = 'Reservoir Dogs");

And, alas, that was EXACLTY what I was doing. Damn.

Check out this post. Use mysql_free_result(), if needed.
Basically, you make 3 queries like the above example. Carefull with security and mysql syntax. Make sure your table names match with the command.

I apologize for the involuntary misleading. I'll try to help you as much as I can in return.

0

I never suggested multiple queries from one command. I want to do three separate inserts to 3 separate tables from the same script.

The problem, as I see it is differentiating the three queries so that mysql receives 3 separate commands. As it stands, mySQL receives 1 query and then gets confused by the repeated queries from the same script (but oddly enough gets it back together enough to catch the last value of the last query).

There has to be a proper way to do this and changing the format of the variables is not it.
Vanna, will you introduce the next contestant, please?

0

WOOHOO! I WIN!!
I got this figured out! Took some searching but I found the answer. I just needed to be using mySQLi instead of mySQL.
I did a $batchconnect (and adjusted some faulty field naming) and everything works. I now have all my queries posting to their respective tables and no errors!
Diligence has its rewards, I guess.
Thanks go out to those who attempted a solution to my problem...at least you tried.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.