Debugging scripts with SQL commands

In the previous example, we like to create commands, like the next line, using a php script:

INSERT INTO rent VALUES ("6", "3", CURDATE());

To be sure, what we are doing, we could leave same debug info in the output of our string. It allows us to evaluate, if our script works correctly. I would like to recommend a fixed format, directly to user screen, using "echo" and numbered output, created by a command similar like:

echo "<br>debug 27:".$tested_variable_name; 

Note using <br> to keep debug info from the left, and connecting the variable value using the dot ("."), resulting in one string output. You can add other string to assure, what is the real content of variable (if spaces works). In our program, it can look like that (the echo commands are in the dark blue):

<?php
$link=mysqli_connect('mysql.webzdarma.cz','zpp','[password]','zpp')
 or die('Error: '.mysqli_error($link) );

echo "<br>debug 0: successfully logged into the database (we hope)<br>";

$query = 'SELECT id FROM people WHERE login="'.$_GET['z'].'" AND pwd="'.$_GET['p'].'";';
echo "<br>debug 1:".$query."<br>";
$result=mysqli_query($link,$query);
if ($result) {  //we have value, so test, if it is a valid user...
$row=mysqli_fetch_assoc($result);
if ($row["id"]>0){  //yes, if "id" is bigger then zero, it is a valid user 
$id_z=$row["id"];
$id_a=$_GET["o"];
echo "<br>debug 2:".$id_z.":".$id_a."<br>";

$query='INSERT INTO rent VALUES ("'.$id_z.'", "'.$id_a.'", CURDATE());'
echo "<br>debug 3:".$query."<br>";
$result=mysqli_query($link,$query);

$query='UPDATE cars SET who="'.$id_z.'" WHERE id="'.$id_a.'";'
echo "<br>debug 4:".$query."<br>";
$result=mysqli_query($link,$query);

} else {echo "internal error of our script";};}

mysqli_close($link); 
echo "<br>debug 5: all done<br>";
?>

After successfully debugged, you can turn these lines to debug comments, using double slashes:

// echo "<br>debug 27:".$tested_variable_name;

Before publishing the script, it is better to erase these lines completely.