作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我又来了,但今天我只是想问一下如何将用户当前的id插入到另一个表中。我的意思是用户在第一个中已经有一个 ID。我想在第二个表中插入完全相同的 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>";
}
加上。我不太理解 mysql_insert_id(); 的功能;请帮忙谢谢你祝你有美好的一天! :D
最佳答案
mysql和mysqli中的基本术语是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()
函数返回对表进行查询生成的 ID,该表的列具有 AUTO_INCRMENT 属性。如果最后一个查询不是 INSERT 或 UPDATE 语句,或者修改的表没有具有 AUTO_INCRMENT 属性的列,则此函数将返回零。
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);
?>
如果您想获取 mysqli.*
中最后插入的 id,则必须传递 $con- 连接变量
以用于进一步目的。
关于php - 如何将相同的ID从第一个表插入到第二个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39424513/
我是一名优秀的程序员,十分优秀!