...

View Full Version : Cant get this simple class assignment working :-(



talimore1
12-05-2010, 09:04 PM
Hello everyone, for this class assignment I have to just copy and paste out of the book but its not working!!! How is that even possible. I sharing the simple code here but Ill also share what Im getting from it:

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", "mgs314", "password"); if ($DBconnect === FALSE) echo "

Ubable 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 = @myswl_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); } } ?>

And here are the two pages code:

<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>

and the php page that it sends to:


<?php

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

else {
$DBConnect = @mysql_connect("localhost", "mgs314", "password");
if ($DBconnect === FALSE)
echo "<p>Ubable to connect to the database server.</p>" . "<p>Error Code " . mysql_errno() . ": " . mysql_error() . "</p>";
else {
$DBName = "guestbook";
if (!@mysql_select_db($DBName, $DBConnect)) {
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult ===FALSE)
echo "<p>Unable to execute the query.</p>" . "<p>Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";
else
echo "<p> You are the first visitor!<p>";
}
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 = @myswl_query($SQLstring, $DBConnect);
if ($QueryResult === FALSE)
echo "<p> Unable to create the table.</p>" . "<p>Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";

$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 "<p>Unable to execute the query.</p>" . "<p>Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";

else
echo "<h1>Thank you for signing our guest book!</h1>";
}
mysql_close($DBConnect);
}
}

?>

Both are using Strict DTD.

Thanks in Advance for the help!

Lamped
12-05-2010, 09:12 PM
Perhaps you can help us out by explaining what isn't working and what errors you're getting?

Fou-Lu
12-05-2010, 09:37 PM
If that first block is your actual output you will need to use a machine that has PHP installed.

talimore1
12-05-2010, 09:37 PM
Perhaps you can help us out by explaining what isn't working and what errors you're getting?

Its simply not working, as in, not outputting anything I need. It outputs the first sentence correctly but then just outputs all the php code afterwords.


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", "mgs314", "password"); if ($DBconnect === FALSE) echo "

Ubable 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 = @myswl_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); } } ?>

See it says nothing is getting inserted when in fact I do have both fields with data and then it outputs that there is nothing in those fields, afterwards it just outputs all my php code and doesnt even translate it into html....

talimore1
12-05-2010, 10:11 PM
Im trying to run this all local using the Unifrom Server and its suppose to just naturally support PHP.

EDIT: And the first echo works fine.

Fou-Lu
12-05-2010, 10:30 PM
So it doesn't show the:


<?php

if (...

Part? That would indicate something has gone awry with the text file itself. Open it up in notepad and save it in ansi and try again. Something could be wrong with any of those special characters that may cause this.

If it does show the <?php part (check your source code, since it starts with a < it is considered to be an element) then it is not processing your PHP. If you are accessing from the file:// protocol, then this PHP won't parse. If you are accessing it from http:// then its your webserver that is choking on it (and I have never used Unifrom Server so I can't help you on that).

ProfessorMurray
12-08-2010, 03:20 AM
It was talked about in class today duh!


<?php

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

else {
$DBConnect = @mysql_connect("localhost", "mgs314", "password");
if ($DBconnect === FALSE)
echo "<p>Ubable to connect to the database server.</p>" . "<p>Error Code " . mysql_errno() . ": " . mysql_error() . "</p>";
else {
$DBName = "guestbook";
if (!@mysql_select_db($DBName, $DBConnect)) {
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult ===FALSE)
echo "<p>Unable to execute the query.</p>" . "<p>Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";
else
echo "<p> You are the first visitor!<p>";
}
mysql_select_db($DBName, $DBConnect);
} <- add me here maybe ***********************************************
$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 = @myswl_query($SQLstring, $DBConnect);
if ($QueryResult === FALSE)
echo "<p> Unable to create the table.</p>" . "<p>Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";

$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 "<p>Unable to execute the query.</p>" . "<p>Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";

else
echo "<h1>Thank you for signing our guest book!</h1>";
}
mysql_close($DBConnect);
}
} <- delete me here maybe******************************************

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum