INSERT Help

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

INSERT Help

Post 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?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

How?

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

?>
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

What error did you get? Perhaps you forgot to close one of your if or while statements?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Your missing a ' on the following line:

header('Location: step2.php?userid=$userid);
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

AHHHHH...

Post 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?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I think you want mysql_insert_id()
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

McGruff

Post by AliasBDI »

There is no need for that since the database creates the number for me...
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: McGruff

Post 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.

?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

McGruff

Post 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(). :?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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();
?>
Last edited by McGruff on Tue Aug 09, 2005 11:47 pm, edited 1 time in total.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

ahhh

Post 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?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Tue Aug 09, 2005 11:02 pm, edited 1 time in total.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

McGruff

Post by AliasBDI »

That did it!

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