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
https://forums.phpfreaks.com/topic/175741-php-and-mysql/
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
https://forums.phpfreaks.com/topic/175741-php-and-mysql/#findComment-926145
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
https://forums.phpfreaks.com/topic/175741-php-and-mysql/#findComment-926167
Share on other sites

Might be a good idea to get a decent code editor that highlights and color codes your code to make it easier to find errors.

 

Also, if you have any more errors, it would help to see the updated code if you could post it.

 

Handy PHP

Link to comment
https://forums.phpfreaks.com/topic/175741-php-and-mysql/#findComment-926625
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
https://forums.phpfreaks.com/topic/175741-php-and-mysql/#findComment-926637
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
https://forums.phpfreaks.com/topic/175741-php-and-mysql/#findComment-926652
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.