Jump to content


Photo

What's wrong with this code?


  • Please log in to reply
9 replies to this topic

#1 hpughLW

hpughLW
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 April 2006 - 11:54 PM

I keep getting an error message with this script. What seems to be the problem? PHP is very new to me, so I appreciate anyone who can help me! The database name, table name, username and password have all been replaced with dummies, FYI.

<?

//check for required query string variables
if (!$_GET[id]) {
header("Location:http://localhost/contact_menu.php");
exit;
} else {
//if form variables are present,start a session
session_start();
}

//check for validity of user
if ($_SESSION[valid] != "yes") {
header("Location:http://localhost/contact_menu.php");
exit;
}

//set up table and database names
$db_name ="testDB";
$table_name ="my_contacts";

//connect to server and select database
$connection = @mysql_connect("localhost","username","password") or die(mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

//build and issue query
$chk_id = "SELECT id FROM $table_name WHERE id ='$_GET[id]'";
$chk_id_res = @mysql_query($chk_id,$connection) or die(mysql_error());
$chk_id_num = mysql_num_rows($chk_id_res);

//check for valid results
if ($chk_id_num !=1){

//if not valid,redirect to menu
header("Location:http://localhost/contact_menu.php");
exit;

} else {

//if valid,get information
$sql ="SELECT media_outlet, media_type, f_name, l_name, title, email, phone, cell, fax, notes FROM $table_name WHERE id ='$_GET[id]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//get results for display
while ($row =mysql_fetch_array($result)) {
$media_outlet = $row['media_outlet'];
$media_type = $row['media_type'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$title = $row['title'];
$email = $row['email'];
$phone = $row['phone'];
$cell = $row['cell'];
$fax = $row['fax'];
$notes = $row['notes'];
}
}
?>
<HTML>
<HEAD>
<TITLE>Media Database: Read-Only Contact Details</TITLE>
</HEAD>
<BODY>
<h1>Media Database</h1>
<h2>Contact Details for <? echo "$f_name $l_name"; ?></h2>
<P><strong>Name & Media Information:</strong><br>
<? echo "$f_name $l_name"; ?><br>
<? echo "$title"; ?><br>
<? echo "$media_outlet"; ?><br>
<? echo "$media_type"; ?><br>
<? echo "<a href=\"mailto:$email\">$email"; ?><br>
<? echo "$phone"; ?><br>
<? echo "$cell"; ?><br>
<? echo "$notes"; ?><br></P>

<p><a href="contact_menu.php">Return to Main Menu</a></p>
</BODY>
</HTML>

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 16 April 2006 - 12:10 AM

OK, so what's the error message?
Legend has it that reading the manual never killed anyone.
My site

#3 hpughLW

hpughLW
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 16 April 2006 - 12:13 AM

[!--quoteo(post=365187:date=Apr 15 2006, 08:10 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 15 2006, 08:10 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
OK, so what's the error message?
[/quote]

This page cannot be displayed.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 16 April 2006 - 12:24 AM

[a href=\"http://localhost/contact_menu.php\" target=\"_blank\"]http://localhost/contact_menu.php[/a] is the only page that wouldn't be displayed. Are you sure it exists with that name in that folder? Personally, I'd change the redirect to a relative page address so when the script is uploaded to the server it'll still work as expected.
Legend has it that reading the manual never killed anyone.
My site

#5 hpughLW

hpughLW
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 16 April 2006 - 12:33 AM

Yes, that address actually exists and works.

What do you mean by a relative page address?


#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 16 April 2006 - 12:46 AM

[!--quoteo(post=365192:date=Apr 15 2006, 08:33 PM:name=hpughLW)--][div class=\'quotetop\']QUOTE(hpughLW @ Apr 15 2006, 08:33 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]What do you mean by a relative page address?[/quote]
This link explains ... [a href=\"http://www.thuto.org/ubh/web/html/relad1.htm\" target=\"_blank\"]http://www.thuto.org/ubh/web/html/relad1.htm[/a]

In your case, if contact_menu.php is in the same folder as the script with the problem, then this will work [relative address]
header("Location: contact_menu.php");

Legend has it that reading the manual never killed anyone.
My site

#7 hpughLW

hpughLW
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 16 April 2006 - 12:53 AM

Ok, I tried that, but now the page with the problems links back to the main page (/contact_menu.php) instead of showing the contact's information. I think you can gather by the code I showed you that this particular page is supposed to show the address book information typed into the table.

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 April 2006 - 10:26 AM

So have you checked that your queries are behaving as expected?

I'd start with the id check

try

//build and issue query
$chk_id = "SELECT id FROM $table_name WHERE id ='$_GET[id]'";
$chk_id_res = @mysql_query($chk_id,$connection) or die(mysql_error());
$chk_id_num = mysql_num_rows($chk_id_res);

[!--coloro:#3366FF--][span style=\"color:#3366FF\"][!--/coloro--]exit ("$check_id <br> $chk_id_num"); // <---- temp code to check query
[!--colorc--][/span][!--/colorc--]

//check for valid results
if ($chk_id_num !=1){

//if not valid,redirect to menu
header("Location:http://localhost/contact_menu.php");
exit;
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 hpughLW

hpughLW
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 16 April 2006 - 11:30 AM

It still says this page cannot be displayed.

#10 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 16 April 2006 - 12:47 PM

Suggestions.

First, I assume that id is sent to this page from a page (contact_menu.php presumably) which contains a form using the method="get" since your commented code suggests checking form values. If the form uses method="post" the $_GET array value will always be absent.

Second, there's no way to tell at which stage the script tries to find a page that doesn't exist. For debugging purposes I'd suggest changing each instance of the header/location function to something simple like:
echo "failed at line whatever";
exit;
At least then you'll know what's causing it to fail.

Third, add Barand's check in your script.

And if a page can't be found, then the script is looking for a file that doesn't exist with the name you expect in the location you expect.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users