如何解决重复条目,而是将一个条目插入 mysql table?
How to solve duplicate entry, and instead, have one entry inserted into mysql table?
<?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 table 中得到 2 个不同 ID 的条目,如下所示。第一个条目 (ID:71) 不包含 $image_name 和 $image_path,但第二个条目 (ID:72) 包含第一个条目中带有 $[=18= 的所有数据] 和 $image_path。因此,当我只想看到一个插入了所有数据的条目时,我在 table 中得到了两个条目。有没有办法解决我遇到的这个问题?谢谢你。
那么你应该只插入一个而不是 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);
}
}
?>
通常您会使用 insert into table on duplicate key update
语法 - 假设有某种主键
$sql="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')
on duplicate key update
`offence_place`='$offence_place',
`vehicle_no`='$vehicle_no',
`offence_type`='$offence_type',
`offence_lotnumber`='$offence_lotnumber',
`offence_charges`='$offence_charges',
`image_name`='$image_name',
`image_path`='$path';";
<?php
/* Create db connection object */
$connect = new mysqli( 'localhost', 'root', ' ', 'tutorial' ) or die('Error: unable to connect to db ');
/* Get the variables assigned */
$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'];
/* Ensure there is a default value for these */
$path = $image_name='';
/* Create the sql statement */
$sql="insert into `eSummon`( `offence_place`, `vehicle_no`, `offence_type`, `offence_lotnumber`, `offence_charges`, `image_name`, `image_path` )
values ( ?, ?, ?, ?, ?, ?, ? )
on duplicate key update
`offence_place`=?,
`vehicle_no`=?,
`offence_type`=?,
`offence_lotnumber`=?,
`offence_charges`=?,
`image_name`=?,
`image_path`=?;";
/* Use aprepared statement */
$stmt=$connect->prepare( $sql );
$stmt->bind_params( 'sssssss', $offence_place,$vehicle_no,$offence_type,$offence_lotnumber,$offence_charges,$image_name,$path );
$stmt->execute();
/* Why this header? If you echo text further it will break the image! */
header('Content-type: bitmap; charset=utf-8');
if( isset( $_POST['encoded_string'] ) ){
$encoded_string = $_POST['encoded_string'];
$image_name = $_POST['image_name'];
$decoded_string = base64_decode( $encoded_string );
$path = 'images/'.$image_name;
$file = fopen( $path, 'wb' );
$is_written = fwrite( $file, $decoded_string );
fclose( $file );
if( $is_written > 0 ) {
/* New values have been assigned to image_name and path, execute statement again */
$res=$stmt->execute();
echo $res ? 'Success' : 'Failed';/* this would break the image */
}
}
?>
<?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 table 中得到 2 个不同 ID 的条目,如下所示。第一个条目 (ID:71) 不包含 $image_name 和 $image_path,但第二个条目 (ID:72) 包含第一个条目中带有 $[=18= 的所有数据] 和 $image_path。因此,当我只想看到一个插入了所有数据的条目时,我在 table 中得到了两个条目。有没有办法解决我遇到的这个问题?谢谢你。
那么你应该只插入一个而不是 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);
}
}
?>
通常您会使用 insert into table on duplicate key update
语法 - 假设有某种主键
$sql="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')
on duplicate key update
`offence_place`='$offence_place',
`vehicle_no`='$vehicle_no',
`offence_type`='$offence_type',
`offence_lotnumber`='$offence_lotnumber',
`offence_charges`='$offence_charges',
`image_name`='$image_name',
`image_path`='$path';";
<?php
/* Create db connection object */
$connect = new mysqli( 'localhost', 'root', ' ', 'tutorial' ) or die('Error: unable to connect to db ');
/* Get the variables assigned */
$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'];
/* Ensure there is a default value for these */
$path = $image_name='';
/* Create the sql statement */
$sql="insert into `eSummon`( `offence_place`, `vehicle_no`, `offence_type`, `offence_lotnumber`, `offence_charges`, `image_name`, `image_path` )
values ( ?, ?, ?, ?, ?, ?, ? )
on duplicate key update
`offence_place`=?,
`vehicle_no`=?,
`offence_type`=?,
`offence_lotnumber`=?,
`offence_charges`=?,
`image_name`=?,
`image_path`=?;";
/* Use aprepared statement */
$stmt=$connect->prepare( $sql );
$stmt->bind_params( 'sssssss', $offence_place,$vehicle_no,$offence_type,$offence_lotnumber,$offence_charges,$image_name,$path );
$stmt->execute();
/* Why this header? If you echo text further it will break the image! */
header('Content-type: bitmap; charset=utf-8');
if( isset( $_POST['encoded_string'] ) ){
$encoded_string = $_POST['encoded_string'];
$image_name = $_POST['image_name'];
$decoded_string = base64_decode( $encoded_string );
$path = 'images/'.$image_name;
$file = fopen( $path, 'wb' );
$is_written = fwrite( $file, $decoded_string );
fclose( $file );
if( $is_written > 0 ) {
/* New values have been assigned to image_name and path, execute statement again */
$res=$stmt->execute();
echo $res ? 'Success' : 'Failed';/* this would break the image */
}
}
?>