logo
Tags down

shadow

duplicate insert into mysql ?


By : Maria Eduarda duda
Date : May 03 2020, 07:39 AM

I'm using code and testing it using xampp on my localhost.

It works fine and inserts the data however for some reason it creates duplicate row in to my database.

Is there are reason why it makes it appear twice?

<?php
require "conn.php";
$name =$_POST["name"];
$surname = $_POST["surname"];
$age = $_POST["age"];
$username = $_POST["username"];
$userpass = $_POST["password"];

$mysql_qry = "insert into employee_data(name, surname, age, username, password) values ('$name', '$surname', '$age', '$username', '$userpass')";

$result = mysqli_query($conn, $mysql_qry);

if ($conn->query($mysql_qry) === TRUE){
    echo "insert success";
}
else{
    echo "Error:" .$mysql_qry . "<br> " . $conn->error;
}
$conn->close();
?>

Thank you in advance

Answer :

YES, you run the query TWICE, see comments in the code

<?php
require "conn.php";
$name =$_POST["name"];
$surname = $_POST["surname"];
$age = $_POST["age"];
$username = $_POST["username"];
$userpass = $_POST["password"];

$mysql_qry = "insert into employee_data
                        (name, surname, age, username, password) 
                values ('$name', '$surname', '$age', '$username', '$userpass')";

//ONCE HERE
$result = mysqli_query($conn, $mysql_qry);

//AND AGAIN HERE
if ($conn->query($mysql_qry) === TRUE){
    echo "insert success";
}
else{
    echo "Error:" .$mysql_qry . "<br> " . $conn->error;
}
$conn->close();
?>

 Use prepared parameterized statements in either the MYSQLI_ or PDO API's

Coded using prepared and bound queries

<?php
require "conn.php";

$sql = "insert into employee_data
                        (name, surname, age, username, password) 
                values (?,?,?,?,?)";

$stmt = $conn-prepare($sql);
$stmt->bind_param('sssss', $_POST["name"],
                            $_POST["surname"];
                            $_POST["username"];
                            $_POST["password"];

if ( $stmt->execute() ){
    echo "insert success";
}else{
    echo "Error:" .$mysql_qry . "<br> " . $conn->error;
}
$conn->close();
?>

Now I have to mention how bad it is to use Plain Text Password.

PHP provides password_hash() and password_verify() please use them. And here are some good ideas about passwords



Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • php function return an array
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • php array_push empty
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql insert into existing row
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org