0

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

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
Adrian Romero
  • 17
  • 1
  • 4
  • What's not working ? – Alok Patel Sep 10 '16 at 09:14
  • You shouldn't use any mysql_*-functions. They have been deprecated since php 5.5 and completely removed in php 7.0 – Manish Sep 10 '16 at 09:18
  • @Adrian Romero mysql_insert_id — Get the ID generated in the last query [http://php.net/manual/en/function.mysql-insert-id.php]. Its will give you last insert id. – Manish Sep 10 '16 at 09:20
  • @AlokPatel it just goes to else. and it does nothing.but when i remove the variable $id and P_Id in the column inside the query it returns 0 – Adrian Romero Sep 10 '16 at 09:23
  • But i suggest not to use mysql_*. because It was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead of mysql_* , MySQLi or PDO_MySQL extension should be used. – Manish Sep 10 '16 at 09:24
  • @Adrian Romero The reason behind you didn't give auto increment to `p_id`. – Manish Sep 10 '16 at 09:27
  • check these link [http://www.w3schools.com/sql/sql_primarykey.asp] [http://stackoverflow.com/questions/8761570/how-can-i-alter-a-primary-key-constraint-using-sql-syntax]. These will give you information how to alter/ create primary key constraint. – Manish Sep 10 '16 at 09:29

1 Answers1

0

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.

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
  • @Adrian Romero . If you have obtained the solution let me know. If you find it useful +1 since this is a very common error that all the developers make hence it may be useful for the rest of them. – Naresh Kumar P Sep 10 '16 at 09:47