Forum Moderators: coopster
What am I doing wrong?
if($submit)
{
$db = mysql_connect("*****", "*****","*****");
mysql_select_db("*******",$db);
$sql = "INSERT INTO dancer (firstname, lastname, email, phone, stAddress, city, state, zip, organization, effectedbyRiley, addtoSFemails)
VALUES ('$firstname', '$lastname', '$email', '$phone', '$stAddress', '$city', '$state', '$zip', '$organization', '$effectedbyRiley', '$addtoSFemails')";
$result = mysql_query($sql);
$idSelect = "SELECT userId from dancer where firstname='$firstname'";
$resultID = mysql_query($idSelect);
echo "Your application to be a dancer has been submitted. Please submit the $15 registration fee to the Student Foundation. ";
echo $resultID;
echo " is your Dancer Number";
}
Thanks for your help in advance!
Question for you. It looks like you're adding a record and then getting the id for that record by searching for the first name. What happens if you add a second 'Bob' to the database? It should pull up the first one and give you that id first. If all you're trying to do is get the unique id from the previous INSERT, look at mysql_insert_id. I'm pretty sure it would do what you're trying to do without the second query.
HTH
- Ryan
It's taking the result and storing it in an associative array. You then pull the array member out. This example will pull out the first record. If there's more than one record, you'll have to loop. If it doesn't pull a record, it will return FALSE.
Have a look @ [us2.php.net...]
- Ryan
$resultID = mysql_query($idSelect);
$dancerID = mysql_fetch_object($resultID);
echo $dancerID->userId;
echo " is your Dancer Number";
And like Nutter mentions, unless your firstname field is unique, you could pull the wrong entry and there`s no way you can have firstname unique, so you may want to think about pulling the data based on something unique, then use the limit clause:
$idSelect = "SELECT userId from dancer where something='$something' LIMIT 1";
or at the very least check against both firstname AND lastname:
$idSelect = "SELECT userId from dancer where firstname='$firstname' AND lastname = '$lastname' LIMIT 1";
fetch_row is apparently slower then fetch_object, but to be honest I can never see any difference. I always use fetch_object over fetch_row because I prefer an associative array over an enumerated one. If you have lots of rows the numbers can get confusing. Plus its easier for debugging if you can see your row names. Works better for me anyway.
fetch_assoc is really an equivalent to fetch_array, except that fetch_array fetches the set as an associative and numeric array. If you need to use the numeric indices with fetch_assoc you need to add MYSQL_ASSOC as a parameter. You probably know this already.
I think its all down to personal preference really. :)
My next step is to send an e-mail to both me to let me know a new person has applied and also to the user as a confirmation e-mail, preferably an HTML e-mail so I can make it look cool. Any suggestions?
Glad we can help, this is what this forum is for. Sending HTML e-mails is down to your headers. By default headers are plain text if nothing is specified. To send HTML e-mails you need to change your header syntax.
This is a snippet of code jatar_k posted a while ago:
$from .= "From: Name<$someemail>\n";
$from .= "X-Sender: Name<$someemail>\n";
$from .= "X-Mailer: PHP\n";
$from .= "X-Priority: 3\n";
$from .= "Return-Path: Name<$someemail>\n";
$from .= "Reply-To: Name<$siteemail>\n";
mail($to,$subject,$message,$from);
You just need to add HTML as normal in your message body with these headers. Give it a try and get back to us.
:)