How can I INSERT data in two or more tables in mysql using single query in php?

Sultan

New member
Hi all.
I have two tables in mysql database.

Database Name: "mydatabase"
Table1 Name: "users"
Table2 Name: "profile"

In "users" Table I have these fields.

id
firstname
lastname
email
password
sex
dob
date

In "profile" Table I have these fields

id
firstname
lastname
sex
dob
city
country

Now I want that when any user click register, his id (which is auto increment) should also insert into profile table's id field as well as user table's id field.
What should I change in this code in INSERT query?

PHP code here

<?php

echo "<h2 class='head'>Register Here</h2>";

$submit = (isset($_POST['submit'])) ? $_POST['submit'] : '';

$firstname = strip_tags((isset($_POST['firstname'])) ? $_POST['firstname'] : '');
$lastname = strip_tags((isset($_POST['lastname'])) ? $_POST['lastname'] : '');
$email = strtolower(strip_tags((isset($_POST['ema… ? $_POST['email'] : ''));
$password = strip_tags((isset($_POST['password'])) ? $_POST['password'] : '');
$repeatpassword = strip_tags((isset($_POST['repeatpassword… ? $_POST['repeatpassword'] : '');
$sex = strip_tags((isset($_POST['sex'])) ? $_POST['sex'] : 'M');
$dob = strip_tags((isset($_POST['birthmonth'])) ? $_POST['birthmonth'] : '1') . "/" . strip_tags((isset($_POST['birthday'])) ? $_POST['birthday'] : '1') . "/" . strip_tags((isset($_POST['birthyear'])) ? $_POST['birthyear'] : '1900');

$birthmonth = strip_tags((isset($_POST['birthmonth'])) ? $_POST['birthmonth'] : '');
$birthday = strip_tags((isset($_POST['birthday'])) ? $_POST['birthday'] : '');
$birthyear = strip_tags((isset($_POST['birthyear'])) ? $_POST['birthyear'] : '');

if ($birthyear != '' && $birthmonth != '' && $birthday != '') {
// Generate date of birth in format of YYYY-mm-dd
$dob = $birthyear.'-'.$birthmonth.'-'.$birthday…
}
$date = date("Y-m-d");

if ($submit)
{
$connect = mysql_connect("localhost","root","");
mysql_select_db("phplogin");

$emailcheck = mysql_query("SELECT email FROM users WHERE email='$email'");
$count = mysql_num_rows($emailcheck);

if ($count!=0)
{
die ("This email is already registered.");
}

if ($firstname&&$lastname&&$email&&$passwor…
{

if ($password==$repeatpassword)
{
if (strlen($firstname)>25||strlen($lastname…
{
echo "Length of Firstname/Lastname is too long!";
}
else
{
if (strlen($password)>25||strlen($password)…
{
echo "Password must be between 6 to 25 characters.";
}
else
{
//register the user!
$password = md5($password);
$repeatpassword = md5($repeatpassword);

$queryreg = mysql_query("
INSERT INTO users VALUES
('','$firstname','$lastname','$email',
'$password','$sex','$dob','$date')
");
die ("You've been registered! <a href='index.php'>Return to login page.</a>");
}
}
}
else
echo "Your passwords do not match.";
}
else
echo "Please fill in <b>all</b> fields.";
}

?>
 
Back
Top