Adrian Romero Adrian Romero - 2 months ago 10
MySQL Question

how to insert the same id from 1st table to the 2nd table

I'm here again, but today i just wanted to ask how to insert the current id of the user to another table. I mean the user has already an ID in the first one. and I want to insert the exact same Id in 2nd table.

<?php
include('connect.php');
$id=$_POST['P_Id'];
$date=$_POST['appdate'];
$time=$_POST['apptime'];
$lname=$_POST['lname'];
$fname=$_POST['fname'];
$contact=$_POST['contact'];
$service=$_POST['service'];
$status = "pending";
$dentist= "Dr. Adrian Romero";
$msg= "Appointment Sucessfully Inserted ";
$update= mysql_query("INSERT INTO appointments (P_Id,LasName,FirstName,contact,appdate,apptime,service,status) values ('$id','$date','$time','$dentist','$fname','$lname','$contact','$service','$status')" );
$id=mysql_insert_id();
if($update)
{
echo "<script> alert('Thank you For Requesting an appointment. please wait for the administrator s response after 24 hrs')</script>";
header('Location:Patient.php');
}
else
{
$msge= mysql_error();
$errormsg="Something went wrong, Try again!";
echo "<script type='text/javascript'>alert('$errormsg');</script>";
}


plus. I don't really Understand the function of mysql_insert_id(); please help thank you have a good day! :D

Answer

The basic terminology in the mysql and as well as mysqli is the mysqli_insert_id

Your Insert Statement is Entirely Wrong . You have Inserted Random Values in the Insert Statement. Try using the code below that i have mentioned.

<?php
include('connect.php');
$id=$_POST['P_Id'];
$date=$_POST['appdate'];
$time=$_POST['apptime'];
$lname=$_POST['lname'];
$fname=$_POST['fname'];
$contact=$_POST['contact'];
$service=$_POST['service'];
$status = "pending";
$dentist= "Dr. Adrian Romero";
$msg= "Appointment Sucessfully Inserted ";
$query ="INSERT INTO `appointments` (`P_Id`,`LasName`,`FirstName`,`contact`,`appdate`,`apptime`,`service`,`status`) VALUES ('".$id."','".$lname."','".$fname."','".$contact."','".$date."','".$time."','".$service."','".$status."')";
$update= mysql_query($query);
$id=mysql_insert_id();
if($id)
{
echo "<script> alert('Thank you For Requesting an appointment. please wait for the administrator s response after 24 hrs')</script>";
header('Location:Patient.php');
}
else
{
$msge= mysql_error();
$errormsg="Something went wrong, Try again!";
echo "<script type='text/javascript'>alert('$errormsg');</script>";
}
?>

As of now the mysql.* has been depreciated use of mysqli.* is preferred.

mysqli_insert_id — Returns the auto generated id used in the last query

The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.

Note: Performing an INSERT or UPDATE statement using the LAST_INSERT_ID() function will also modify the value returned by the mysqli_insert_id() function.

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");

// Print auto-generated id
echo "New record has id: " . mysqli_insert_id($con);

mysqli_close($con);
?> 

If you want to get the last inserted id in mysqli.* you have to pass the $con- connection variable for further purpose.

Comments