-1

I have a serious issue.
When I submit a form a second time, with new data, then all other data goes duplicate with the last submission's data. Also, when I update any one of the columns it goes all the column data duplicate.

Html Form

<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.
?>

I want to know why that data is duplicating after submission. Thanks in advance.

tektiv
  • 14,010
  • 5
  • 61
  • 70
Hamza Ali
  • 25
  • 5
  • What does `SubmitForm()` do? – Taplar Mar 14 '18 at 19:40
  • 2
    You first update, which adds records, then insert, which adds records. hence the duplication. – Gavin Simpson Mar 14 '18 at 19:40
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Mar 14 '18 at 19:45
  • **WHY** does one script use `MYSQLI_` and the other use the **dead** `MYSQL_` API – RiggsFolly Mar 14 '18 at 19:47
  • @GavinSimpson ___Small Note___ Update does not ADD rows, it change/amends them – RiggsFolly Mar 14 '18 at 19:48
  • afaik @RiggsFolly it will add records if they don't exist – Gavin Simpson Mar 15 '18 at 01:59
  • formatted code and question – tektiv Mar 15 '18 at 11:23

1 Answers1

-1

You are both updating and inserting your records. Also, why are you using MySQL and MySqli in two different forms? Do not use 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'");
sbowde4
  • 767
  • 1
  • 4
  • 23
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Mar 14 '18 at 19:45
  • 1
    Please dont encourage the use of the deprecated (for year) and ___removed forever in PHP7+___ `mysql_` API – RiggsFolly Mar 14 '18 at 19:46
  • I'm not encouraging anything. Did you even read my post? @RiggsFolly – sbowde4 Mar 14 '18 at 21:05
  • I'm not going to rewrite his code for him, he asked why there were duplications and I showed him where they were happening. @RiggsFolly – sbowde4 Mar 14 '18 at 21:11