php - 如何解决重复条目,而是将一个条目插入到 mysql 表中?

标签 php mysql database post sql-insert

<?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。因此,当我只想看到一个插入了所有数据的条目时,我在表中得到了两个条目。有没有办法解决我遇到的这个问题?谢谢。

mysql table entries

最佳答案

那么你应该只插入一个而不是 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/

相关文章:

mysql - 如何使用另一个表更新 phpmyadmin 中的表

php - 如何从 Prestashop 数据库中获取特定产品

mysql - 仅当某些属性包含相同值时才自动增量

android - 通配符在 SQLite 中不起作用

.net - 使用 SqlBulkCopy 在不同来源之间传输数据

php - 如何在 symfony 2.6 中的另一个服务中使用一个服务

php - 如何在 Behat FeatureContext 中获取标签

php - 检查重复记录

php - 如何计算特定标签 ex。 <li> 标签或 <p> 标签

php - 为什么我应该关闭或保持 Redis 连接打开?