damiendarien Posted August 25, 2010 Share Posted August 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211663-anyone-can-help-with-this-simple-yet-frustrating-question/ Share on other sites More sharing options...
mikosiko Posted August 25, 2010 Share Posted August 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211663-anyone-can-help-with-this-simple-yet-frustrating-question/#findComment-1103416 Share on other sites More sharing options...
damiendarien Posted August 25, 2010 Author Share Posted August 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/211663-anyone-can-help-with-this-simple-yet-frustrating-question/#findComment-1103417 Share on other sites More sharing options...
damiendarien Posted August 25, 2010 Author Share Posted August 25, 2010 now i realize what you were saying so i added $QueryResult = mysqli_query($DBConnect, $SQLstring) ahead of what you replied Quote Link to comment https://forums.phpfreaks.com/topic/211663-anyone-can-help-with-this-simple-yet-frustrating-question/#findComment-1103421 Share on other sites More sharing options...
damiendarien Posted August 25, 2010 Author Share Posted August 25, 2010 and everything worked properly after i added a whole line of code i missed (duhhhh me) THANKS again. How do i mark this as solved now? I am new to this forum. Sorry for the new posts didnt notice the "Modify" button. wont happen again Quote Link to comment https://forums.phpfreaks.com/topic/211663-anyone-can-help-with-this-simple-yet-frustrating-question/#findComment-1103422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.