Page 1 of 1

INSERT Help

Posted: Thu Jan 15, 2004 6:41 pm
by AliasBDI
I have a form for creating new users. The form submits the user's information without a "userid". The "userid" is created by the database (MySQL) incrementing numbers.

I want the form to redirect to the next page with the "userid" as the query variable. So how do I get the "userid" in the header code?

I was thinking that I should have the form query the database, find the last (and largest) number in the "userid" field and then create the next number and INSERT the user's information with that "userid". Then the "userid" can be called in the header as $userid. Is this correct?

If so, I would then need help for that code. I do not know how to query the database and create the next userid number. Anybody?

Posted: Thu Jan 15, 2004 6:57 pm
by kettle_drum
If you dont create the user id yourself then you just have to query the database after you have added the new user to find out what user id the database gave to your data.

Since the username should also be unique you could simply pass that to the next page and use that to do what you are trying to do on the second page.

How?

Posted: Thu Jan 15, 2004 7:23 pm
by AliasBDI
Never thought of that. So I tried it and got an error on line 80 which reads "?>" and thats it. I don't know what the problem is. Any ideas?

Here is the code:

Code: Select all

<?

include '../../includes/db.php';

// Define post fields into simple variables.  These are called CONSTANTS.
$first_name = $_POST&#1111;'first_name'];
$last_name = $_POST&#1111;'last_name'];
$email_address = $_POST&#1111;'email_address'];
$username = $_POST&#1111;'username'];
$password = $_POST&#1111;'password'];
$password_encrypt = md5($password);
$address = $_POST&#1111;'address'];
$city = $_POST&#1111;'city'];
$state = $_POST&#1111;'state'];
$zip = $_POST&#1111;'zip'];
$phone1 = $_POST&#1111;'phone1'];
$phone2 = $_POST&#1111;'phone2'];
$birth = $_POST&#1111;'birth'];
$dl = $_POST&#1111;'dl'];
$active = $_POST&#1111;'active'];
$newsletter = $_POST&#1111;'newsletter'];
$closed = $_POST&#1111;'closed'];

/* Lets strip some slashes in case the user entered
any escaped characters. */

$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$address = stripslashes($address);
$city = stripslashes($city);
$state = stripslashes($state);
$zip = stripslashes($zip);
$phone1 = stripslashes($phone1);
$phone2 = stripslashes($phone2);
$birth = stripslashes($birth);
$dl = stripslashes($dl);
$newsletter = stripslashes($newsletter);
$closed = stripslashes($closed);


/* Do some error checking on the form posted fields */

if((!$first_name) || (!$last_name)
)&#123;
	echo 'You did not submit the following required information! <br />';
	if(!$first_name)&#123;
		echo "First Name is a required field. Please enter it below.<br />";
	&#125;
	if(!$last_name)&#123;
		echo "Last Name is a required field. Please enter it below.<br />";
	&#125;

	echo "Please go BACK and try again."; // Show the form again!
	/* End the error checking and if everything is ok, we'll move on to
	 creating the user account */
	exit(); // if the error checking has failed, we'll exit the script!
&#125;

/* Everything has passed both error checks that we have done.
It's time to create the account! */

// Enter info into the Database.
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, decrypted_password, address, city, state, zip, phone1, phone2, newsletter, closed, active, birth, dl)
		VALUES('$first_name', '$last_name', '$email_address', '$username', '$password_encrypt', '$password', '$address', '$city', '$state', '$zip', '$phone1', '$phone2', '$newsletter', '$closed', '$active', '$birth', '$dl')") or die (mysql_error());

// Query the database to grab the userid that was just created.
$result = mysql_query("SELECT userid FROM users WHERE username='$username' AND password='$password_encrypt' AND dl='$dl'");
if (!$result) &#123; 
    print "ERROR - browse query failed."; 
    exit(); 
&#125; 
while ( $row = mysql_fetch_array($result) ) 
&#123; 
$userid = $row&#1111;'userid'];  
header('Location: step2.php?userid=$userid);
&#125;

?>

Posted: Thu Jan 15, 2004 7:24 pm
by Straterra
What error did you get? Perhaps you forgot to close one of your if or while statements?

Posted: Thu Jan 15, 2004 7:43 pm
by kettle_drum
Your missing a ' on the following line:

header('Location: step2.php?userid=$userid);

AHHHHH...

Posted: Thu Jan 15, 2004 8:00 pm
by AliasBDI
Thanks... that was it. It is usually something little like that. It would have ate my lunch too...!

Well for some reason, it is not getting the "userid"... is there something with my code?

Posted: Fri Jan 16, 2004 3:30 am
by McGruff
I think you want mysql_insert_id()

McGruff

Posted: Fri Jan 16, 2004 9:04 am
by AliasBDI
There is no need for that since the database creates the number for me...

Re: McGruff

Posted: Fri Jan 16, 2004 11:49 am
by McGruff
AliasBDI wrote:There is no need for that since the database creates the number for me...
Maybe I picked you up wrong but I though you wanted to:

(1) insert a new row
(2) get the auto-incremented id for the new row & declare that as a php var.

?

McGruff

Posted: Fri Jan 16, 2004 12:15 pm
by AliasBDI
(1) insert a new row
(2) get the auto-incremented id for the new row & declare that as a php var.
That is correct...

Maybe I am misunderstanding you then? I'm not too familiar with mysql_insert_id(). :?

Posted: Fri Jan 16, 2004 1:35 pm
by McGruff
It's important to have a copy of the manual always available for reference. You can download a copy with user comments (recommended) from php.net.

Code: Select all

<?php
$mysql = etc;
$query = mysql_query($mysql) or die ... etc;
$id = mysql_insert_id();
?>

ahhh

Posted: Mon Jan 19, 2004 8:51 am
by AliasBDI
Apparently, my code is correct in creating the new record and then grabbing the new userid. However, it seems that my "header" is not coded correctly. Can you check it?

Code: Select all

// Query the database to grab the userid that was just created.
$result = mysql_query("SELECT userid FROM users WHERE username='$username' AND password='$password_encrypt' AND dl='$dl'");
if (!$result) &#123; 
    print "ERROR - browse query failed."; 
    exit(); 
&#125; 
while ( $row = mysql_fetch_array($result) ) 
&#123; 
$userid = $row&#1111;'userid']; 
header('Location: step2.php?userid="$userid"');
&#125;
It is passing the url
step2.php?userid="$userid"
, but I need it to switch out the variable code with the variable set. And it is being set, I checked it. I also tried removing the quotes from $userid but that did not work either. Any ideas?

Posted: Mon Jan 19, 2004 8:54 pm
by McGruff
Variables aren't parsed inside a single quoted string.

Try this:

Code: Select all

<?php

header('Location: step2.php?userid=' . $userid);

?>
I always concatenate variables & strings since (with syntax highlighting) it makes it much more obvious which is which.

McGruff

Posted: Tue Jan 20, 2004 8:49 am
by AliasBDI
That did it!

Now I see what was wrong. Thanks a bunch. :D