Php 如何解决重复条目问题,并将一个条目插入mysql表?
那么您应该只插入一个,而不是2个 这就是为什么会有重复条目的原因Php 如何解决重复条目问题,并将一个条目插入mysql表?,php,mysql,database,post,sql-insert,Php,Mysql,Database,Post,Sql Insert,那么您应该只插入一个,而不是2个 这就是为什么会有重复条目的原因 <?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_
<?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);
}
}
?>
删除一些顶级代码,并将代码转换为:
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);
}
}
?>
$sql=“插入'eSummon'('officity\u place'、'vehicle\u no'、'officity\u type'、'officity\u lotnumber'、'officity\u charges'、'image\u name'、'image\u path`)
值(“$officity\u place”、“$vehicle\u no”、“$officity\u type”、“$officity\u lotnumber”、“$officity\u charges”、“$image\u name”、“$path”)
关于重复密钥更新
`违例地点“=$违例地点”,
`车辆编号“=”$车辆编号“,
`犯罪类型“=”$犯罪类型“,
`违例事项编号“=”$违例事项编号“,
`违例收费“=”违例收费“,
`image\u name`='$image\u name',
`图像路径“=”$path“;”;
因此,我尝试使用您建议的代码。但在mqsql表中只显示映像_名称和映像_路径。当我希望插入所有数据时,其他记录(违例地点、车辆编号、违例类型、违例批号、违例费用)不显示。好的,谢谢!在我开始尝试您建议的代码之前,请允许我问一下您的意思:/*确保这些*/$path=$image\u name=''有一个默认值;我应该为这两个变量中的每一个输入值吗?不,但在您的初始查询中,我看不到这两个变量的声明位置-因此您有可能出错。对不起,您能给我一个示例,说明您的默认值是什么吗?这样我就可以从您的角度理解了。正如所写的一样-一个空字符串应该足够了,因为它将在第二个查询中更新解决!好的,谢谢你的帮助。非常感谢。
$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 */
}
}
?>