1

Which one is correct out of the following three that work fine in my inexperienced tests and why that one over the other two ?

Each code is different on the IFs that has this line:

echo 'INSERTING SUCESSFULLY: ' .mysqli_stmt_affected_rows($stmt);
<?php

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'mogambo'; //username.
$input_2 = 'admin@mogambo.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 'INSERTING 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);

?>
<?php

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'mogambo'; //username.
$input_2 = 'admin@mogambo.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 'INSERTING 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);

?>
<?php

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'mogambo'; //username.
$input_2 = 'admin@mogambo.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 'INSERTING 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);

?>
  • I don't see much difference between these variants and neither of them is correct. you should remove all if statements. As well as this `echo INSERTING SUCESSFULLY` stuff. Basically you need to write only three lines, prepare, bind and execute, that's all. See also [this answer](https://stackoverflow.com/a/67252635/285587) – Your Common Sense May 16 '22 at 18:11
  • Just ask youself, for whom you are writing all these ornate messages, echo `'Mysqli Error No: ' .mysqli_errno(); echo '
    ';`? Who is supposed to read them and what for?
    – Your Common Sense May 16 '22 at 18:29
  • @Your Common Sense - Those codes are DEV MODE codes.Not PRODUCTION MODE. – Novice Programmer May 16 '22 at 19:43
  • 1
    Then ask yourself, does it make sense to have to versions of the same code? – Your Common Sense May 16 '22 at 19:44
  • Don't get me wrong, you are asking good questions. And the answer is already provided. I just want to hint you into the right direction, so you will see the correct answer yourself. – Your Common Sense May 17 '22 at 04:56

0 Answers0