Gautam P Behera
Gautam P Behera

Reputation: 171

Prevent Form from inserting data multiple times into database

Trying to insert form data into mysql database, my issues is as the user submits the form the data gets inserted twice into database. I know that I am doing something wrong with my 2nd query $query_file = "INSERT INTO upperbit_files... statment as when I remove the whole if loop if(mysqli_query($dbc, $query_info)){...} the form gets submitted once as expected.

Basically I need to insert data into 2 tables. One is for general product info and the other one is to store photos relating to that product both the table are connected via a global variable $advert_id. I am using 2 separate queries

  1. Table1: advert_sell_category1, is for general product info
  2. Table2: upperbit_files, is to store details of the images uploaded

But for some reason the 1st query relating to general product info is getting inserted twice into database and the irony is both the time the $advert_id is the same. Below is my code and a screenshot of the database for your understanding,

enter image description here

    if(isset($_POST['postad'])){

        $adtype =  $_POST['offering_type'];
        $manufacturer = mysqli_real_escape_string($dbc, $_POST['manufaturer']);
        $mediafile = mysqli_real_escape_string($dbc,$_POST['mediafile']);

    $GLOBALS['advrt_post_id'] = crypto_rand_secure(10, 100000);
    $query_info = "INSERT INTO advert_sell_category1(advert_id,manufacturer,image_file) 
    VALUES('$advrt_post_id','$manufacturer','$mediafile')";
    $result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));

       if(mysqli_query($dbc, $query_info)){
    $last_id = mysqli_insert_id($dbc);
    $query_link_id = "SELECT advert_id FROM advert_sell_category1 WHERE id = '$last_id' ";
    $result_id = mysqli_query($dbc, $query_link_id);

    while ($row = mysqli_fetch_assoc($result_id)) {
        $link_id = $row['advert_id'];

       if(!empty($mediafile)){
       $media_file = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafile']));
       $media_file = array_filter($media_file);
       $media_file_size = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafilesize']));
       $media_file_size = array_filter($media_file_size);
       $media_file_type = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafiletype']));
       $media_file_type = array_filter($media_file_type);
        for ($var = 0; $var < sizeof($media_file); $var++){ 
        $query_file = "INSERT INTO upperbit_files(file,size,type,link_id) VALUES ('$media_file[$var]','$media_file_size[$var]','$media_file_type[$var]','$link_id')";
        $result_file = mysqli_query($dbc, $query_file) or die(mysqli_error($dbc));
        } 
      }
    }
  }

Upvotes: 0

Views: 570

Answers (1)

Ghanshyam Bagul
Ghanshyam Bagul

Reputation: 169

/********** Your Code ************/
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));

if(mysqli_query($dbc, $query_info)){
/**********************/

See here in if statement you are calling mysqli_query() second time so same data is inserted twice. Use following code to solve your problem

/********** Suggested Code ************/
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));

if(mysqli_affected_rows()>0){
/**********************/

Upvotes: 1

Related Questions