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

时间:2016-01-10 10:38:25

标签: 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 个答案:

答案 0 :(得分: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);
}


}   

?>

答案 1 :(得分:0)

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