PHP Run Once and Insert Twice in MySQL Database

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



Leave a reply



Submit