Jump to content

php and MySQL


pinochio

Recommended Posts

I am trying to create guestbook that will trabsfer all the data into a database. However, when I click submit button I can see part of my code in the browser.

This is my code:

 



<?php
if (empty($_GET["Name"]) || empty($_GET["Email"]))
   die("<p> You must enter name and email. You need to go back to the Guestbook.</p>";

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

$DBName = "guestbook"; //creating database
if (!@mysqli_select_db($DBConnect, $DBName)) {
   SQLString = "CREATE DATABASE $DBName";
   $QueryResult = @mysqli_query($DBConncet, $SQLString);
   Or die("<p>Unable to execute the query.</p>"
   . "<p>Error code " . mysqli_errno($DBCoonect)
   . ": " . mysqli_error($DBConnect)) . "</p>";
echo "<p>You are the first visitor!</p>"
mysqli_select_db($DBConnect, $DBName);
   
$TableName = "visitors";
$SQLString = "SELECT * FROM $TableName";
$QueryResult = mysqli_query($DBConnect, $SQLString);   
   if (!$QueryResult) {
   	  $SQLString = "CREATE $TableName (countID SMALLINT NOT NULL AUTO INCREMENT PRIMARY KEY ,
  guest_name VARCHAR(40), guest_email VARCHAR(40))";
  $QueryResult = @mysqli_query($DBConnect, $SQLString)
  or die("<p>Unable to create table .</p>"
  . "<p>Error Code " . mysqli_errno($DBConnect)
  . ":" . mysqli_error($DBConnect)) . "</p>";
  }

$Name = addslashes($_GET["Name"]);
$Email = addslashes($_GET["Email"]);
$SQLString = "INSERT INTO $TableName VALUES(NULL, "$Name", "$Email")";
$QueryResult = @mysqli_query($DBConnect, $SQLString)
		 or die("<p>Unable to execute query.</p>"
		 . "<p>Error Code " . mysqli_errno($DBConnect)
         . ":" . mysqli_error($DBConnect)) . "</p>";
echo "<p>Thank you for signing our guestook.</p>";
mysqli_close($DBConnect);
?>
<p><a href="SeeGuestbook.html">BACK</a></p>
</body>
</html>

 

Thank you!

Link to comment
Share on other sites

I spent some time debugging my code, so right now it looks or less problematic comparing to what I had an hour ago..... :D However, when I run it this is what I get:

Parse error: syntax error, unexpected ';' in c:\program files\apache group\Apache\htdocs\CT\getguestbook.php on line 5

 

Thanks a lot!!!!

Link to comment
Share on other sites

Missing end parenthesis on line 5:

   die("<p> You must enter name and email. You need to go back to the Guestbook.</p>";

Should be:

   die("<p> You must enter name and email. You need to go back to the Guestbook.</p>");

 

Additional errors will likey happen with each of your die functions since your parenthesis are misplaced.

 

Handy PHP

Link to comment
Share on other sites

what might be wrong with this statement?

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

 

there is a problem with syntax, unexpected T_LOGICAL_OR, but I can't find where the problem is

 

Thanks a lot!

 

Link to comment
Share on other sites

$QueryResult = @mysqli_query($DBConncet, $SQLstring);
   or die("<p>Unable to execute the query.</p>"

semi-colon preceding an OR statement.

 

Generally, your error message will give you a line number for the error.  Generally, the problem is on the line right before the error was found...

You had a semi-colon on line 3 which made the OR statement on line 4 return an error.

 

You might want to consider NOT breaking your lines up so much since this seems to be causing you a lot of problems.  Breaking your lines up can make reading your code easier sometime but in this case, it actually makes it harder to read.  Perhaps if you indent the code after the first break it would make it clearer what belongs with what.

Like so:

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

Also, you have some typing errors:

line 3:

$QueryResult = @mysqli_query($DBConncet, $SQLstring);

Should Be:

$QueryResult = @mysqli_query($DBConnect, $SQLstring)

 

And Line 5

. "<p>Error code " . mysqli_errno($DBCoonect)

Should Be:

. "<p>Error code " . mysqli_errno($DBConnect)

 

You really need to try and pay more attention to your code.  There isn't a spellchecker for PHP, just an error message!

 

Handy PHP

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.