当使用Mysqli_insert_id提取最近自动生成的id时,Mysqli/PHP/HTML会复制insert记录
我现在有这个代码可以插入我的收据表当使用Mysqli_insert_id提取最近自动生成的id时,Mysqli/PHP/HTML会复制insert记录,php,html,mysql,Php,Html,Mysql,我现在有这个代码可以插入我的收据表 $query = "insert into receipt(petname,receipttype) values('$petname','$receipttype')"; 我在其中获取自动生成的id $receiptid=mysqli\u insert\u id$db; 并将其发送到下一页。但是,当插入记录时,还会生成另一条记录,其中两条内容相同,但会将另一个自动生成的ID输入数据库 [具有唯一ID的重
$query = "insert into receipt(petname,receipttype)
values('$petname','$receipttype')";
我在其中获取自动生成的id
$receiptid=mysqli\u insert\u id$db;
并将其发送到下一页。但是,当插入记录时,还会生成另一条记录,其中两条内容相同,但会将另一个自动生成的ID输入数据库
[具有唯一ID的重复字段示例]
if (isset($_POST['cmdedit'])){
$petname = mysqli_real_escape_string($db,$_POST['petname']);
$receipttype = mysqli_real_escape_string($db,$_POST['receipttype']);
$query = "insert into receipt(petname,receipttype)
values('$petname','$receipttype')";
$result = mysqli_query($db,$query);
if (mysqli_query($db,$query)){
$receiptid=mysqli_insert_id($db);
if ($result) {
echo "<font color = 'green' > Receipt sucessfully obtained! Page will auto-redirect to order confirmation page in 5 seconds! </font>";
header( "refresh:5; url=addorderhomepage.php?animalid=".$animalid."&receiptid=".$receiptid);
}else{
echo "<p>Something went wrong! </p>" . mysqli_error($db);
}
}
}
?>
<div class = "topbar">
<h2> Order Receipt </h2>
</div>
<?php
$query = "select *
from catalogue
where animalid= " . $animalid;
$result = mysqli_query($db,$query);
if ($result){
while ($rows = mysqli_fetch_array($result))
{
?>
<form method = "post" action = "" >
<table>
<tr>
<th> Animal ID</th>
<td bgcolor="#FFFFFF"> <input type ="text" name = "txtanimalid" value = "<?php echo $rows[0]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Name </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[1]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Type </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[3]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Species </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[2]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Description </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[4]; ?>" readonly /> </td>
</tr>
<tr>
<th> Receipt type </th>
<td> <input type ="text" name = "receipttype" value = "printable" readonly /> </td>
</tr>
<tr>
<th> Receipt Date </th>
<td> <input type ="date" name = "orderdate" value="<?php echo date('Y-m-j'); ?>" readonly="readonly" </td>
</tr>
<tr>
<th> <br/><br/> </th>
<td> </td>
</tr>
<tr>
<th> </th>
<td> <input type ="submit" value = "Obtain receipt" name = "cmdedit" /> </td>
</tr>
重复行可能是因为您使用相同的数据调用了两次mysqli_查询,第一次是在$result变量中存储响应,第二次是在if语句中的条件中
// ... code
$result = mysqli_query($db,$query);
- if (mysqli_query($db,$query)) {
+ if ($result) {
$receiptid = mysqli_insert_id($db);
if ($result) {
// .. rest of the code
太好了,谢谢你,我真的很惊讶我犯了那个错误。