<?php
//open connection to mysql db
$connect = mysqli_connect("localhost","root"," ","tutorial") or die("Error " . mysqli_error($connect));
$offence_place = $_POST["offence_place"];
$vehicle_no = $_POST["vehicle_no"];
$offence_type = $_POST["offence_type"];
$offence_lotnumber = $_POST["offence_lotnumber"];
$offence_charges = $_POST["offence_charges"];
$query = " Insert into eSummon(offence_place, vehicle_no, offence_type, offence_lotnumber, offence_charges, image_name, image_path)
values ('$offence_place','$vehicle_no','$offence_type','$offence_lotnumber','$offence_charges','$image_name','$path');";
mysqli_query($connect,$query) or die (mysqli_error($connect));
// IMAGE
header('Content-type : bitmap; charset=utf-8');
// Image Connection to Database
if (isset($_POST["encoded_string"])){
$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];
$decoded_string = base64_decode($encoded_string);
// Save image on the server
$path = 'images/'.$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
// Save the path to the Database
if($is_written > 0) {
// Open connection to mysql Database
$connect = mysqli_connect("localhost","root"," ","tutorial") or die("Error " . mysqli_error($connect));
$query = " Insert into eSummon(offence_place, vehicle_no, offence_type, offence_lotnumber, offence_charges, image_name, image_path)
values ('$offence_place','$vehicle_no','$offence_type','$offence_lotnumber','$offence_charges','$image_name','$path');";
$result = mysqli_query($connect, $query) or die("Error in Selecting " . mysqli_error($connect));
if($result){
echo "Success";
}else{
echo "Failed";
}
mysqli_close($connect);
}
}
?>
运行上面的 php 代码后,我将在如下所示的 mysql 表中获得 2 个不同 ID 的条目。第一个条目 (ID:71) 不包含 $image_name 和 $image_path,但第二个条目 (ID:72) 包含第一个条目中的所有数据,带有 $image_name 和 $image_path。因此,当我只想看到一个插入了所有数据的条目时,我在表中得到了两个条目。有没有办法解决我遇到的这个问题?谢谢。
最佳答案
那么你应该只插入一个而不是 2 个。
这就是为什么会出现重复条目的原因
mysqli_query($connect,$query) --> you use this twice
删除你的一些顶级代码,并将你的代码变成这样:
<?php
if (isset($_POST["encoded_string"])){
$offence_place = $_POST["offence_place"];
$vehicle_no = $_POST["vehicle_no"];
$offence_type = $_POST["offence_type"];
$offence_lotnumber = $_POST["offence_lotnumber"];
$offence_charges = $_POST["offence_charges"];
$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];
$decoded_string = base64_decode($encoded_string);
// Save image on the server
$path = 'images/'.$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
// Save the path to the Database
if($is_written > 0) {
// Open connection to mysql Database
$connect = mysqli_connect("localhost","root"," ","tutorial") or die("Error " . mysqli_error($connect));
$query = " Insert into eSummon(offence_place, vehicle_no, offence_type, offence_lotnumber, offence_charges, image_name, image_path)
values ('$offence_place','$vehicle_no','$offence_type','$offence_lotnumber','$offence_charges','$image_name','$path');";
$result = mysqli_query($connect, $query) or die("Error in Selecting " . mysqli_error($connect));
if($result){
echo "Success";
}else{
echo "Failed";
}
mysqli_close($connect);
}
}
?>
关于php - 如何解决重复条目,而是将一个条目插入到 mysql 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34704438/