当我尝试提交表单时如何修复数据重复问题

时间:2018-03-14 19:38:19

标签: php jquery mysql

我有一个严重的问题。
当我第二次提交表单时,使用新数据,然后所有其他数据与最后提交的数据重复。此外,当我更新任何一列时,它会使所有列数据重复。

Html表格

<form id="myForm" class="form-horizontal" role="form" method="post" action="">
    <input class="inputs" type="hidden" required id="id" name="id" placeholder="*Code...">
    <input class="inputs" type="number" required id="code" name="code" placeholder="*Code...">
    <input class="inputs" type="text" required id="product" name="products" placeholder="*Products...">
    <input type="text" autocomplete="off" placeholder="*Suppliers..." name="supplier" id="supplier" class="inputs">
    <input class="inputs" type="number" required id="price" name="price" placeholder="*Price...">
    <button class="btn btn-primary" onclick="SubmitForm();" value="send"> Save </button>
</form>

的search.php

<?php
    header('Content-Type: application/json');
    $response = array();
    if (isset($_GET['scode'])){
        //vul hier je database gebuikersnaam en ww in
        $con=mysqli_connect("localhost", "root", "", "waqar");

        // Check connection
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        $qry = "SELECT * FROM products WHERE code = '".$_GET['scode']."' ";

        $result = mysqli_query($con, $qry);  //mysql_query($qry);

        while ($row = mysqli_fetch_array($result, MYSQL_BOTH)) {
            array_push($response, $row);
        }

        echo json_encode($response);
    } 
?>

Submit.php

<?php
    // Establishing connection with server by passing "server_name", "user_id", "password".
    $connection = mysql_connect("localhost", "root", "");

    // Selecting Database by passing "database_name" and above connection variable.
    $db = mysql_select_db("waqar", $connection);
    $scode = $_POST['code']; // Fetching Values from URL
    $sproduct = $_POST['products'];
    $sprice = $_POST['price'];
    $ssupplier = $_POST['supplier'];
    //echo $semail;

    $query = mysql_query("update products set products='$sproduct', price='$sprice', supplier='$ssupplier'"); //Insert query
    $query = mysql_query("INSERT INTO products (code,products,price,supplier) values('$scode', '$sproduct', '$sprice', '$ssupplier') on duplicate KEY UPDATE 
      code='$scode', products='$sproduct', price='$sprice', supplier='$ssupplier'");

    if($query){
        echo "Data Submitted succesfully";
    }

    mysql_close($connection); // Connection Closed.
?>

我想知道为什么提交后数据会重复。提前谢谢。

1 个答案:

答案 0 :(得分:-1)

您正在更新和插入记录。另外,为什么您以两种不同的形式使用MySQLMySqli?不要使用MySQL_connect。

$query = mysql_query("update products set products='$sproduct', price='$sprice', supplier='$ssupplier'"); //Insert query
$query = mysql_query("INSERT INTO products (code,products,price,supplier) values('$scode', '$sproduct', '$sprice', '$ssupplier') on duplicate KEY UPDATE 
code='$scode', products='$sproduct', price='$sprice', supplier='$ssupplier'");