2016-09-10 10 views
0

もう一度やりましたが、今日は別のテーブルにユーザの現在のIDを挿入する方法を尋ねました。私はユーザーが既に最初のIDを持っていることを意味します。 2番目のテーブルに全く同じIdを挿入したいと思います。1番目のテーブルから2番目のテーブルに同じIDを挿入する方法

<?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。私は実際にmysql_insert_id()の機能を理解していません。あなたは良い一日をありがとう助けてください! :D

+0

何が問題なのですか? –

+0

mysql _ * - 関数は使用しないでください。それらはphp 5.5以降廃止され、PHP 7.0で完全に削除されました。 – Manish

+0

@Adrian Romero mysql_insert_id - 最後のクエリ[http://php.net/manual/en/function.mysql-insert-id.php]で生成されたIDを取得します。 。それはあなたに最後の挿入IDを与えるでしょう。 – Manish

答えて

0

基本的な用語のmysqlでと同様にmysqliのは、あなたのINSERT文は完全に間違っているmysqli_insert_id

です。挿入ステートメントにランダム値を挿入しました。私が言及した以下のコードを使ってみてください。今のところ

<?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>"; 
} 
?> 

mysql.*mysqli.*の使用が好適である減価償却されています。

mysqli_insert_id - 返し自動最後のクエリで使用されるID

を生成mysqli_insert_id()関数はAUTO_INCREMENT属性を持つカラムを持つテーブルでクエリによって生成されたIDを返します。最後の問合せがINSERTまたはUPDATE文でないか、または変更された表にAUTO_INCREMENT属性を持つ列がない場合、この関数はゼロを戻します。

注:またmysqli_insert_id()関数によって返された値を変更しますLAST_INSERT_ID()関数を使用して、INSERTまたはUPDATEステートメントを実行

<?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); 
?> 

あなたはmysqli.*で最後に挿入IDを取得したい場合は、さらに目的のために$con- connection variableを渡す必要があります。

+0

@Adrian Romero。あなたが解決策を得たなら、私に知らせてください。これは非常に一般的なエラーであるため、すべての開発者が作成するので、他の人にとっては役に立ちます。 –

関連する問題