Why does my echo statement show up even after I populate the database?

On line 28 there is an echo statement “You are the first visitor!” that displays even after the database has been populated. Can anyone tell me what and where my error is?

[php]

Sign Guest Book <?php if (empty($_POST['first_name']) || empty($_POST['last_name'])) { echo "

You must enter your first and last name! Click your browser's back button to return to the Guest Book form.

"; } else { $DBConnect = @mysql_connect("localhost", "user", "password"); } if ($DBConnect === FALSE) { echo "

Unable to connect to the database server.

" . "

Error code " . mysql_errno() . ": " . mysql_error() . "

"; } else { $DBName = "guestbook"; } if (!@mysql_select_db($DBName, $DBConnect)) { $SQLstring = "CREATE DATABASE $DBName"; $QueryResult = @mysql_query($SQLstring, $DBConnect); } if ($QueryResult === FALSE) { echo "

Unable to execute the query.

" . "

Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "

"; } else { echo "

You are the first visitor!

"; } mysql_select_db($DBName, $DBConnect); $TableName = "visitors"; $SQLstring = "SHOW TABLES LIKE '$TableName'"; $QueryResult = @mysql_query($SQLstring, $DBConnect); if (mysql_num_rows($QueryResult) == 0) { $SQLstring = "CREATE TABLE $TableName (countID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, last_name VARCHAR(40), first_name VARCHAR(40))"; $QueryResult = @mysql_query($SQLstring, $DBConnect); } if ($QueryResult === FALSE) { echo "

Unable to create the table.

" . "

Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "

"; } $LastName = stripslashes($_POST['last_name']); $FirstName = stripslashes($_POST['first_name']); $SQLstring = "INSERT INTO $TableName VALUES(NULL, '$LastName', '$FirstName')"; $QueryResult = @mysql_query($SQLstring, $DBConnect); if ($QueryResult === FALSE){ echo "

Unable to execute the query.

" . "

Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "

"; } else { echo "

Thank you for signing our guest book!

"; } mysql_close($DBConnect); ?>

[/php]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

<head>
<title>Guest Book</title>
</head>

<body>

<h2>Enter your name to sign our guest book</h2>

<form method="POST" action="SignGuestBook.php">
<p>First Name <input type="text" name="first_name" /></p>
<p>Last Name <input type="text" name="last_name" /></p>
<p><input type="submit" value="submit" /></p>
</form>


</body>

</html>

You need to drop dynamically creating databases. Set it up and use it, that’s it.

On top of that, you are using obsolete code that has been completely removed from PHP. You need to use PDO with prepared statements.

https://phpdelusions.net/pdo

Sponsor our Newsletter | Privacy Policy | Terms of Service