INSERT Help
Moderator: General Moderators
INSERT Help
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?
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
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.
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?
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:
Here is the code:
Code: Select all
<?
include '../../includes/db.php';
// Define post fields into simple variables. These are called CONSTANTS.
$first_name = $_POSTї'first_name'];
$last_name = $_POSTї'last_name'];
$email_address = $_POSTї'email_address'];
$username = $_POSTї'username'];
$password = $_POSTї'password'];
$password_encrypt = md5($password);
$address = $_POSTї'address'];
$city = $_POSTї'city'];
$state = $_POSTї'state'];
$zip = $_POSTї'zip'];
$phone1 = $_POSTї'phone1'];
$phone2 = $_POSTї'phone2'];
$birth = $_POSTї'birth'];
$dl = $_POSTї'dl'];
$active = $_POSTї'active'];
$newsletter = $_POSTї'newsletter'];
$closed = $_POSTї'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)
){
echo 'You did not submit the following required information! <br />';
if(!$first_name){
echo "First Name is a required field. Please enter it below.<br />";
}
if(!$last_name){
echo "Last Name is a required field. Please enter it below.<br />";
}
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!
}
/* 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) {
print "ERROR - browse query failed.";
exit();
}
while ( $row = mysql_fetch_array($result) )
{
$userid = $rowї'userid'];
header('Location: step2.php?userid=$userid);
}
?>-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
AHHHHH...
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?
Well for some reason, it is not getting the "userid"... is there something with my code?
Re: McGruff
Maybe I picked you up wrong but I though you wanted to:AliasBDI wrote:There is no need for that since the database creates the number for me...
(1) insert a new row
(2) get the auto-incremented id for the new row & declare that as a php var.
?
McGruff
That is correct...(1) insert a new row
(2) get the auto-incremented id for the new row & declare that as a php var.
Maybe I am misunderstanding you then? I'm not too familiar with mysql_insert_id().
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.
ahhh
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?
It is passing the url
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) {
print "ERROR - browse query failed.";
exit();
}
while ( $row = mysql_fetch_array($result) )
{
$userid = $rowї'userid'];
header('Location: 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?step2.php?userid="$userid"
Variables aren't parsed inside a single quoted string.
Try this:
I always concatenate variables & strings since (with syntax highlighting) it makes it much more obvious which is which.
Try this:
Code: Select all
<?php
header('Location: step2.php?userid=' . $userid);
?>
Last edited by McGruff on Tue Aug 09, 2005 11:02 pm, edited 1 time in total.