Howdy once more,
Just got another question on Sql and Php Prepared Statement.
How would you confirm a successful row insertion to a mysql table ?
Which one of the following lines would you add the IF condition to in order to check insertion successful or not ?
1).
mysqli_stmt_execute()
2).
mysqli_stmt_affected_rows()
Which of the following examples A-D are a good way of coding and why and which are a bad way of coding and why based on php's proper way of coding (best practice) ?
Looking below, is there any that is actually unnecessarily going through the IF condition ? (Is pointless). If so, then why ?
A).
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","buzz"); //mysqli_connect("server","user","password","db");
$input_1 = 'magi'; //username.
$input_2 = 'admin@magi.com'; //email.
$sql = "INSERT into users (username,email) VALUES (?,?)";
$stmt = mysqli_prepare($conn,$sql);
mysqli_stmt_bind_param($stmt,"ss",$input_1,$input_2);
mysqli_stmt_execute($stmt);
echo 'INSERTED SUCESSFULLY: ' .mysqli_stmt_affected_rows($stmt);
mysqli_stmt_close($stmt);
mysqli_close($conn);
B).
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","buzz"); //mysqli_connect("server","user","password","db");
$input_1 = 'magi'; //username.
$input_2 = 'admin@magi.com'; //email.
$sql = "INSERT into users (username,email) VALUES (?,?)";
if($stmt = mysqli_prepare($conn,$sql))
{
mysqli_stmt_bind_param($stmt,"ss",$input_1,$input_2);
mysqli_stmt_execute($stmt);
echo 'INSERTED SUCESSFULLY: ' .mysqli_stmt_affected_rows($stmt);
}
else
{
echo 'Mysqli Error: ' .mysqli_error();
echo '<br>';
echo 'Mysqli Error No: ' .mysqli_errno();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
C).
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","buzz"); //mysqli_connect("server","user","password","db");
$input_1 = 'magi'; //username.
$input_2 = 'admin@magi.com'; //email.
$sql = "INSERT into users (username,email) VALUES (?,?)";
if($stmt = mysqli_prepare($conn,$sql))
{
mysqli_stmt_bind_param($stmt,"ss",$input_1,$input_2);
if(mysqli_stmt_execute($stmt))
{
echo 'INSERTED SUCESSFULLY: ' .mysqli_stmt_affected_rows($stmt);
}
else
{
echo 'Mysqli Error: ' .mysqli_error();
echo '<br>';
echo 'Mysqli Error No: ' .mysqli_errno();
echo '<br>';
die('Failed to INSERT!');
}
}
else
{
echo 'Mysqli Error: ' .mysqli_error();
echo '<br>';
echo 'Mysqli Error No: ' .mysqli_errno();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
D).
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","buzz"); //mysqli_connect("server","user","password","db");
$input_1 = 'magi'; //username.
$input_2 = 'admin@magi.com'; //email.
$sql = "INSERT into users (username,email) VALUES (?,?)";
if($stmt = mysqli_prepare($conn,$sql))
{
mysqli_stmt_bind_param($stmt,"ss",$input_1,$input_2);
mysqli_stmt_execute($stmt);
if(mysqli_stmt_affected_rows($stmt))
{
echo 'INSERTED SUCESSFULLY: ' .mysqli_stmt_affected_rows($stmt);
}
else
{
echo 'Mysqli Error: ' .mysqli_error();
echo '<br>';
echo 'Mysqli Error No: ' .mysqli_errno();
echo '<br>';
die('Failed to INSERT!');
}
}
else
{
echo 'Mysqli Error: ' .mysqli_error();
echo '<br>';
echo 'Mysqli Error No: ' .mysqli_errno();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
I experimented with all my example codes and they all work so far.
Cheers!