PHP-Mysql query unnecessarily inserting data twice
So, there's a few problems here..
$conn->query
as mentioned will directly run the code therefore execute is redundant however, you're wide open for SQL Injections therefore you should bind such as my example below:
$stmt = $this->conn->prepare("INSERT INTO `candidates`(`Fname`,`Lname`,`dob`,`password`,`contact`) VALUES (?,?,?,?,?)");
$stmt->execute([$fname,$lname,$dob,$password,$contact]);
Also, you shouldn't be running a try { } catch {}
on generic queries such as this (especially if you're on a live environment as everybody will be able to see such problems as well as yourself).
On a side note, adding the password in unencrypted is also leaving you with security issues. You should take a look at using password_hash
documentation: http://php.net/manual/en/function.password-hash.php
SQL Insert executes twice, php
Do you see this
$result = $db->query($query);
And the next line:
if ($db->query($query) === TRUE) {
This means that you run your query twice. Remove one of the $db->query
, e.g.:
$result = $db->query($query);
if ($result === TRUE) { /* do stuff */
PHP inserting data to mySQL two times instead of one
change this code
$sqlin="INSERT INTO data_test (username, password) VALUES ('valname','valpass')";
$result=$conn->query($sqlin);
if ($conn->query($sqlin) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlin . "<br>" . $conn->error;
}
$conn->close();
include_once "read.php";
to
$sqlin="INSERT INTO data_test (username, password) VALUES ('valname','valpass')";
$result=$conn->query($sqlin);
if ($result === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlin . "<br>" . $conn->error;
}
$conn->close();
include_once "read.php";
Because if you run this line $conn->query($sqlin);
twice it will run your query twice and insert record in database two time
Related Topics
What Is the Maximum Size of an Array in PHP
Stored Procedures, MySQL and PHP
Hide Variation Info from Cart Item Title in Woocommerce 3+
Php: Check for Duplicate Values in a Multidimensional Array
PHP - Add Link to a Url in a String
How Does PHP Compare Strings with Comparison Operators
Better Way to Check Variable for Null or Empty String
Xampp: Couldn't Start Apache (Windows 10)
PHP File Upload Error Tmp_Name Is Empty
Unsetting Array Values in a Foreach Loop
PHP and Microsoft Access Database - Connection and Crud
Use Git Smudge/Clean to Replace File Contents
MySQL Error When Inserting Data Containing Apostrophes (Single Quotes)