Jump to content

Script Won't Output


chibineku
 Share

Recommended Posts

Hi, I've copied this script from Sams Teach Yourself PHP, MySQL and Apache and I guess I've made a mistake. The page I'll show you is supposed to display a list of contacts in your address book, and when you choose one and submit the form, post the form to itself and display the record. The include form is just the mysql connect info, which I know works.

<?phpinclude("ch20_include.php5");doDB();if (!$_POST) {  //haven't seen the selection form, so show it  $display_block = "<h1>Select an Entry</h1>";    //get parts of record  $get_list_sql = "SELECT CONCAT_WS(',', l_name, f_name) AS display_name FROM master_name ORDER BY l_name, f_name";  $get_list_res = mysqli_query($mysqli, $get_list_sql) or die(mysqli_error($mysqli));    if (mysqli_num_rows($get_list_res) < 1) {	//no records	$display_block .="<p><em>Sorry, no records to select!</em></p>";  } else {	//has records, so get results and print in a form	$display_block .= "	<form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\">	<p><strong>Select a record to view:</strong><br />	<select name=\"sel_id\">	<option value=\"\">-- Select One --</option>";	while($recs = mysqli_fetch_array($get_list_res)) {	  $id = $recs['id'];	  $display_name = stripslashes($recs["display_name"]);	  $display_block .= "<option value=\"".$id."\">".$display_name."</option>";	}	$display_block .= "	</select>	<br >	<input type=\"submit\" name=\"submit\" value=\"View Selected Entry\"></p>	</form>";  }  //free result  mysqli_free_result($get_list_res);  } else if ($_POST) {	//check for required fields	if($_POST["sel_id"] == "") {	  header("Location: selentry.php5");	  exit;	}	//get master info	$get_master_sql = "SELECT concat_ws(',',f_name, l_name) as display_name FROM master_name WHERE id = '".$_POST["sel_id"]."'";	$get_master_res = mysqli_query($mysqli, $get_master_sql) or die(mysqli_error($mysqli));	while ($name_info = mysqli_fetch_array($get_master_res)) {	  $display_name = stripslashes($name_info['display_name']);	}	$display_block = "<h1>Showing record for ".$display_name."</h1>";	//free result	mysqli_free_result($get_master_res);	//get all addresses	$get_addresses_sql = "SELECT address, city, district, postcode, type FROM addresses WHERE master_id = '".$_POST["sel_id"]."'";	$get_addresses_res = mysqli_query($mysqli, $get_addresses_sql) or die(mysqli_error($mysqli));	if (mysqli_num_rows($get_addresses_res) > 0) {	  $display_block.= "<p><strong>Addresses:</strong><br />	  <ul>";	  while ($add_info = mysqli_fetch_array($get_addresses_res)) {		$address = stripslashes($add_info['address']);		$city = stripslashes($add_info['city']);		$district = stripslashes($add_info['district']);		$postcode = stripslashes($add_info['postcode']);		$address_type = $add_info['type'];		$display_block .= "<li>$address $city $district $postcode ($address_type)</li>";	  }	  $display_block .= "</ul>";	}	//free result	mysqli_free_result($get_address_res);  //get all tel  $get_tel_sql = "SELECT tel_number, type FROM telephone WHERE master_id = '".$_POST["sel_id"]."'";  $get_tel_res = mysqli_query($mysqli, $get_tel_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_tel_res) > 0) {	$display_block .= "<p><strong>Telephone:</strong><br />	<ul>";	while ($tel_info = mysqli_fetch_array($get_tel_res)) {	  $tel_number = stripslashes($tel_info['tel_number']);	  $tel_type = $tel_info['type'];	  $display_block .= "<li>$tel_number ($tel_type)</li>";	}	$display_block .= "</ul>";  }	//free result	mysqli_free_result($get_tel_res);	//get all fax  $get_fax_sql = "SELECT fax_number, type FROM fax WHERE master_id = '".$_POST["sel_id"]."'";  $get_fax_res = mysqli_query($mysqli, $get_fax_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_fax_res) > 0) {	$display_block .= "<p><strong>Fax:</strong><br />	<ul>";	while ($fax_info = mysqli_fetch_array($get_fax_res)) {	  $fax_number = stripslashes($fax_info['fax_number']);	  $fax_type = $fax_info['type'];	  $display_block .= "<li>$fax_number ($fax_type)</li>";	}		$display_block .= "</ul>";  }	//free result	mysqli_free_result($get_fax_res);	 //get all email  $get_email_sql = "SELECT email, type FROM email WHERE master_id = '".$_POST["sel_id"]."'";  $get_email_res = mysqli_query($mysqli, $get_email_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_email_res) > 0) {	$display_block .= "<p><strong>E-mail:</strong><br />	<ul>";	while ($email_info = mysqli_fetch_array($get_email_res)) {	  $email = stripslashes($tel_info['email']);	  $email_type = $email_info['type'];	  $display_block .= "<li>$email ($email_type)</li>";	}		$display_block .= "</ul>";  }	//free result	mysqli_free_result($get_email_res);	 //get personal note  $get_notes_sql = "SELECT note FROM personal_notes WHERE master_id = '".$_POST["sel_id"]."'";  $get_notes_res = mysqli_query($mysqli, $get_notes_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_notes_res) == 1) {	while ($note_info = mysqli_fetch_array($get_notes_res)) {	  $note = n12br(stripslashes($note_info['note']));	}	  $display_block .= "<p><strong>Personal notes:</strong><br />	  $note</p>";	}	//free result	mysqli_free_result($get_notes_res);	$display_block .= "<br />	<p align = \"center\">	<a href=\"".$_SERVER["php_self"]."\">Select another</a></p>";  }  //close connection to MySQL  mysqli_close($mysqli); ?> <html> <head> <title>My Records</title> </head> <body> <?php echo $display_block; ?> </body> </html>

The page displays the list perfectly, but when I hit submit it just shows the list again. I am sure I must just have missed something, but I've looked and looked and, using ConTEXT, everything is the right colour, suggesting I've not made any glaring syntax/parse errors. So I just need a second pair of eyes.Thanks in advance!-Jonathon

Link to comment
Share on other sites

Okay, weird: when I swap POST for GET, the address bar doesn't show anything...

Link to comment
Share on other sites

If it shows the list instead of the records then the first if statement is evaluating to true. Add this on the top to see what gets submitted. It won't redirect if you don't select an item, but you can at least see what's there:

echo 'post: ' . print_r($_POST, true);echo 'get: ' . print_r($_GET, true);

You should be using post though, that's what it's checking for.

Link to comment
Share on other sites

I know, but I swapped all POSTs for GETs. I added the line you suggested that applied to POST (to the original version), and got:

post: Array ( [sel_id] => [submit] => View Selected Entry )Warning: Cannot modify header information - headers already sent by (output started at /homepages/6/d203988372/htdocs/sinaesthesia/addressbook/selentry.php5:4) in /homepages/6/d203988372/htdocs/sinaesthesia/addressbook/selentry.php5 on line 43

The first time, before I had selected any items, I only got:

post: Array()

Link to comment
Share on other sites

Here is the source file from the disc, modified in a few minor ways (as was my hand typed version):

<?phpinclude("ch20_include.php5");doDB();if (!$_POST) {  //haven't seen the selection form, so show it  $display_block = "<h1>Select an Entry</h1>";    //get parts of record  $get_list_sql = "SELECT CONCAT_WS(',', l_name, f_name) AS display_name FROM master_name ORDER BY l_name, f_name";  $get_list_res = mysqli_query($mysqli, $get_list_sql) or die(mysqli_error($mysqli));    if (mysqli_num_rows($get_list_res) < 1) {	//no records	$display_block .="<p><em>Sorry, no records to select!</em></p>";  } else {	//has records, so get results and print in a form	$display_block .= "	<form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\">	<p><strong>Select a record to view:</strong><br />	<select name=\"sel_id\">	<option value=\"\">-- Select One --</option>";	while($recs = mysqli_fetch_array($get_list_res)) {	  $id = $recs['id'];	  $display_name = stripslashes($recs["display_name"]);	  $display_block .= "<option value=\"".$id."\">".$display_name."</option>";	}	$display_block .= "	</select>	<br >	<input type=\"submit\" name=\"submit\" value=\"View Selected Entry\"></p>	</form>";  }  //free result  mysqli_free_result($get_list_res);  } else if ($_POST) {	//check for required fields	if($_POST["sel_id"] == "") {	  header("Location: selentry.php5");	  exit;	}	//get master info	$get_master_sql = "SELECT concat_ws(',',f_name, l_name) as display_name FROM master_name WHERE id = '".$_POST["sel_id"]."'";	$get_master_res = mysqli_query($mysqli, $get_master_sql) or die(mysqli_error($mysqli));	while ($name_info = mysqli_fetch_array($get_master_res)) {	  $display_name = stripslashes($name_info['display_name']);	}	$display_block = "<h1>Showing record for ".$display_name."</h1>";	//free result	mysqli_free_result($get_master_res);	//get all addresses	$get_addresses_sql = "SELECT address, city, district, postcode, type FROM addresses WHERE master_id = '".$_POST["sel_id"]."'";	$get_addresses_res = mysqli_query($mysqli, $get_addresses_sql) or die(mysqli_error($mysqli));	if (mysqli_num_rows($get_addresses_res) > 0) {	  $display_block.= "<p><strong>Addresses:</strong><br />	  <ul>";	  while ($add_info = mysqli_fetch_array($get_addresses_res)) {		$address = stripslashes($add_info['address']);		$city = stripslashes($add_info['city']);		$district = stripslashes($add_info['district']);		$postcode = stripslashes($add_info['postcode']);		$address_type = $add_info['type'];		$display_block .= "<li>$address $city $district $postcode ($address_type)</li>";	  }	  $display_block .= "</ul>";	}	//free result	mysqli_free_result($get_address_res);  //get all tel  $get_tel_sql = "SELECT tel_number, type FROM telephone WHERE master_id = '".$_POST["sel_id"]."'";  $get_tel_res = mysqli_query($mysqli, $get_tel_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_tel_res) > 0) {	$display_block .= "<p><strong>Telephone:</strong><br />	<ul>";	while ($tel_info = mysqli_fetch_array($get_tel_res)) {	  $tel_number = stripslashes($tel_info['tel_number']);	  $tel_type = $tel_info['type'];	  $display_block .= "<li>$tel_number ($tel_type)</li>";	}	$display_block .= "</ul>";  }	//free result	mysqli_free_result($get_tel_res);	//get all fax  $get_fax_sql = "SELECT fax_number, type FROM fax WHERE master_id = '".$_POST["sel_id"]."'";  $get_fax_res = mysqli_query($mysqli, $get_fax_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_fax_res) > 0) {	$display_block .= "<p><strong>Fax:</strong><br />	<ul>";	while ($fax_info = mysqli_fetch_array($get_fax_res)) {	  $fax_number = stripslashes($fax_info['fax_number']);	  $fax_type = $fax_info['type'];	  $display_block .= "<li>$fax_number ($fax_type)</li>";	}		$display_block .= "</ul>";  }	//free result	mysqli_free_result($get_fax_res);	 //get all email  $get_email_sql = "SELECT email, type FROM email WHERE master_id = '".$_POST["sel_id"]."'";  $get_email_res = mysqli_query($mysqli, $get_email_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_email_res) > 0) {	$display_block .= "<p><strong>E-mail:</strong><br />	<ul>";	while ($email_info = mysqli_fetch_array($get_email_res)) {	  $email = stripslashes($tel_info['email']);	  $email_type = $email_info['type'];	  $display_block .= "<li>$email ($email_type)</li>";	}		$display_block .= "</ul>";  }	//free result	mysqli_free_result($get_email_res);	 //get personal note  $get_notes_sql = "SELECT note FROM personal_notes WHERE master_id = '".$_POST["sel_id"]."'";  $get_notes_res = mysqli_query($mysqli, $get_notes_sql) or die(mysqli_error($mysqli));  if (mysqli_num_rows($get_notes_res) == 1) {	while ($note_info = mysqli_fetch_array($get_notes_res)) {	  $note = n12br(stripslashes($note_info['note']));	}	  $display_block .= "<p><strong>Personal notes:</strong><br />	  $note</p>";	}	//free result	mysqli_free_result($get_notes_res);	$display_block .= "<br />	<p align = \"center\">	<a href=\"".$_SERVER["php_self"]."\">Select another</a></p>";  }  //close connection to MySQL  mysqli_close($mysqli); ?> <html> <head> <title>My Records</title> </head> <body> <?php echo $display_block; ?> </body> </html>

I still can't see the difference, but I'll give it a scour later.P.S. This one works!

Link to comment
Share on other sites

Really? Then it's true...computers just hate me. *smiley depicting novice coder being crushed under the weight of the matrix missing*

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...