【问题标题】:How to solve duplicate entry, and instead, have one entry inserted into mysql table?如何解决重复条目,而是将一个条目插入到 mysql 表中?
【发布时间】:2023-04-01 10:21:01
【问题描述】:
<?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

【问题讨论】:

    标签: php mysql database post sql-insert


    【解决方案1】:

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

    【讨论】:

    • 所以,我尝试使用您建议的代码。但是 mqsql 表中只显示了 image_name 和 image_path。当我想要插入所有数据时,其他记录(offence_place、vehicle_no、offence_type、offence_lotnumber、offence_charges)未显示。
    【解决方案2】:

    通常您会使用 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 */
            }
        }
    ?>
    

    【讨论】:

    • 好的,谢谢!在我开始尝试您建议的代码之前,请问您是什么意思: /* 确保这些代码有默认值 */ $path = $image_name='';我是否应该为这两个变量中的每一个赋值?
    • 否,但在您的初始查询中,我看不到这两个变量的声明位置 - 因此您可能会出错。
    • 对不起,你能举个例子说明你的意思是什么默认值吗?这样我可以从你的角度理解。
    • 正如它所写的那样 - 一个空字符串就足够了,因为它将在第二个查询中更新
    • 解决了!好的,感谢您的友好帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多