Jump to content

Anyone can help with this SIMPLE yet frustrating question


damiendarien

Recommended Posts

Having an issue with this code, there are 3 parts and it is a simple guestbook type thing. But my problem is that it worked the first time I signed the guestbook but now its saying

Unable to create the table.

 

Error code 1050: Table 'visitors' already exists << I know it already exists but WHY is it trying to create it again?

<< Heres the code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guest Book</title>
</head>

<body>
<?php

if (empty($_GET['first_name']) || empty($_GET['last_name']))
die("<p>You must enter your first and last name! Click your browser's Back button to return to the Guest Book form.</p>");

$DBConnect = @mysqli_connect("localhost", "***********", "***********")
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";

$DBName = "guestbook";
if (!@mysqli_select_db($DBConnect, $DBName)) {
	$SQLstring = "CREATE DATABASE $DBName";
	$QueryResult = mysqli_query($DBConnect, $SQLstring)
		Or die("<p>Unable to execute the query.</p>"
		. "<p>Error code " . mysqli_errno($DBConnect)
		. ": " . mysqli_error($DBConnect)) . "</p>";
	echo "<p>You are the first visitor!</p>";
	mysqli_select_db($DBConnect, $DBName);
}

$TableName = "visitors";
$SQLstring = "SELECT * FROM $TableName";
if (!$QueryResult) {
		$SQLstring = "CREATE TABLE $TableName (countID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, last_name VARCHAR(40), first_name VARCHAR(40))";
		$QueryResult = mysqli_query($DBConnect, $SQLstring)
			Or die("<p>Unable to create the table.</p>"
			. "<p>Error code " . mysqli_errno($DBConnect)
			. ": " . mysqli_error($DBConnect)) . "</p>";
}

$LastName = addslashes($_GET['last_name']);
$FirstName = addslashes($_GET['first_name']);
$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$LastName', '$FirstName')";
$QueryResult = mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
echo "<h1>Thank you for signing our guest book!</h1>";

mysqli_close($DBConnect);
?>
</body>
</html>

 

Can someone please point my to the right direction. Getting frustrated and I am assuming it is something quite simple i am overlooking.

Thanks in advance.

Link to comment
Share on other sites

apparently because in this lines:

 

$SQLstring = "SELECT * FROM $TableName";
if (!$QueryResult) {

 

$QueryResult doesn't exist the second time that you execute your script, hence the if is always true.

 

Seems that you need to execute the query that you defined in $SQLstring first and the ask for the value of $QueryResult,

 

Independent of that... what are the reasons to have the CREATE DATABASE and CREATE TABLE in your code?.... you should create both only one time using PHPADMIN, MYSQL WORKWENCH or what ever mechanism that you prefer but I don see any reason to have that in your code.  And in addition you should eliminate the @'s of your code... you are suppressing the display of errors instead of control if they occur.

Link to comment
Share on other sites

Thank you for your response. This is for a school assignment that is why i have to put the @'s in there and that is the way the code is in the book and that is how the instructor wants it but in working order.

I have went throughout the assignment numerous times and it is exactly as the code in the book is supposed to be with the exception of it working lol.

I would rather eliminate that section of code and just manually create the DB in phpMyAdmin but that block of code is supposed to check for the database and if its not there then CREATE it.

Any further help to solve this would be greatly appreciated.

 

Oh yeah and yes i am new to PHP as if anyone hasnt noticed lol

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.