Jump to content

Recommended Posts

I have the following code, but when no records are in the database, a message is supposed to display, but it is not working. Can anyone help me?

 

<?php
//connect to database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());


if(!isset($_POST['op'])) {
//haven't seen the form, so show it
$display_block = "<h1>Select An Entry</h1>";

//get parts of records
$get_list = "select id, concat_ws(', ', l_name, f_name) as display_name
	from master_name order by l_name, f_name";
$get_list_res = mysql_query($get_list) or die(mysql_error());

if (mysql_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 Delete:</strong><br>
	<select name=\"sel_id\">
	<option value=\"\">-- Select One --</option>";

	while ($recs = mysql_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>
	<input type=\"hidden\" name=\"op\" value=\"delete\">
	<p><input type=\"submit\" name=\"submit\"
		value=\"Delete Selected Entry\"></p>
	</form>";
}

} else if ($_POST['op'] == "delete") {
//check fo required fields
	if ($_POST['sel_id'] == "") {
	header("Location: delentry.php");
	exit;
}

//issue queries
$del_master = "delete from master_name where id = $_POST[sel_id]";
mysql_query($del_master);

$del_address = "delete from address where id = $_POST[sel_id]";
mysql_query($del_address);

$del_tel = "delete from address telephone where id = $_POST[sel_id]";
mysql_query($del_tel);

$del_fax = "delete from fax where id = $_POST[sel_id]";
mysql_query($del_fax);

$del_email = "delete from email where id = $_POST[sel_id]";
mysql_query($del_email);

$del_note = "delete from personal_notes where id = $_POST[sel_id]";
mysql_query($del_master); //$del_note???

$display_block = "<h1>Record(s) Deleted</h1>
<p>Would you like to
<a href=\"$_SERVER[php_SELF]\">delete another</a>?</p>";
}
?>
<html>
<head>
<title>My Records</title>
</head>
<body>
<? print $display_block; ?>
</body>
</html>

 

Here is the block with just the message that is supposed to be displayed if there are no records in the database:

 

if (mysql_num_rows($get_list_res) < 1) {
	//no records
	$display_block .= "<p><em>Sorry, no records to select!</em></p>";

}

 

What am I missing or doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/
Share on other sites

I re-did the code and now it works. Not sure what I left out, but here is the correct code:

 

<?php
//connect to database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

if(!isset($_POST['op'])) {
//havent seen the form, so show it
$display_block = "<h1>Select An Entry</h1>";

//get parts of records
$get_list = "select id, concat_ws(', ', l_name, f_name) as display_name
	from master_name order by l_name, f_name";
$get_list_res = mysql_query($get_list) or die(mysql_error());

if (mysql_num_rows($get_list_res) < 1) {
	//no records
	$display_block .= "<p><em>Sorry, no records to select!</em></p>
	<p align=\"center\"><a href=\"mymenu.php\">Back To Main Page</a></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 Delete:</strong><br>
	<select name=\"sel_id\">
	<option value=\"\">-- Select One --</option>";

	while ($recs = mysql_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>
	<input type=\"hidden\" name=\"op\" value=\"delete\">
	<p><input type=\"submit\" name=\"submit\"
		value=\"Delete Selected Entry\"></p>
	</form>";
}

} else if(!isset($_POST['op'])) {

//check for required fields
if ($_POST['sel_id'] == "") {
	header("Location: delentry.php");
	exit;
}

//issue queries
$del_master = "delete from master_name where id = $_POST[sel_id]";
mysql_query($del_master);

$del_adress = "delete from address where id = $_POST[sel_id]";
mysql_query($del_address);

$del_tel = "delete from telephone where id = $_POST[sel_id]";
mysql_query($del_tel);

$del_fax = "delete from fax where id = $_POST[sel_id]";
mysql_query($del_fax);

$del_email = "delete from email where id = $_POST[sel_id]";
mysql_query($del_email);

$del_note = "delete from personal_notes where id = $_POST[sel_id]";
mysql_query($del_note);

$display_block = "<h1>Record(s) Deleted</h1>
<p>Would you like to
<a href=\"$_SERVER[php_SELF]\">delete another</a>?</p>";
}
?>
<html>
<head>
<title>My Records</title>
</head>
<body>
<?php print $display_block; ?>
</body>
</html>

It shows the message when no records are in the database, but this error displays when you try to delete a record.

 

Notice: Undefined variable: display_block in C:\wamp\www\delentry.php on line 82

 

This error appears when you try to add a record:

 

Notice: Undefined variable: add_email in C:\wamp\www\addentry.php on line 95

 

Notice: Undefined variable: add_email in C:\wamp\www\addentry.php on line 96

Query was empty

All of those errors mean that you are trying to use a variable that hasn't been initialized (it has no type yet) and is thus unknown to your system as to what it should return. Please post these specific lines of code as these are not shown in the examples.

 

Also the body of this statement is never executed as it is preceded with the same if statement.

} else if(!isset($_POST['op'])) {

 

 

The addenry.php produces these errors right now:

 

Notice: Undefined index: master_id in C:\wamp\www\addentry.php on line 8

 

Notice: Undefined variable: display_name in C:\wamp\www\addentry.php on line 24

 

Notice: Undefined index: master_id in C:\wamp\www\addentry.php on line 67

 

Line 8:

if ($_GET['master_id'] != "") {

 

Line 24:

if ($display_name != "") {

 

Line 67:

<input type=\"hidden\" name=\"master_id\" value=\"$_GET[master_id]\">

 

I don't know what happened to my other errors. I guess I changed something and now it produces the above listed errors.

 

Here is the full code if needed:

<?php
if (!isset($_POST['op']) || ($_GET['master_id'] != "")) {
   //haven't seen the form, so show it
   $display_block = "
   <h1>Add An Entry</h1>
   <form method=\"post\" action=\"$_SERVER[php_SELF]\">";
   
   if ($_GET['master_id'] != "") {
      //connect to database
      $conn = mysql_connect("localhost", "root", "")
         or die(mysql_error());
      mysql_select_db("smrpg",$conn) or die(mysql_error());
      
      //gets first, last names for display/tests validity
      $get_names  = "SELECT CONCAT_WS(' ', f_name, l_name) as
         display_name FROM master_name WHERE id = $_GET[master_id]";
      $get_names_res = mysql_query($get_names) or die(mysql_error());
      
      if (mysql_num_rows($get_names_res) == 1) {
         $display_name = mysql_result($get_names_res,0,'display_name');
      }
   }
   
   if ($display_name != "") {
      $display_block .="<p>Adding information for
         <strong>$display_name</strong>:</p>";
   } else {
      $display_block .= "
      <p><strong>First/Last Names:</strong><br>
      <input type=\"text\" name=\"f_name\" size=30 maxlength=75>
      <input type=\"text\" name=\"l_name\" size=30 maxlength=75>";
   }
   $display_block .= "<p><strong>Address:</strong><br>
      <input type=\"text\" name=\"address\" size=30>
      
      <p><strong>City/State/Zip:</strong><br>
      <input type=\"text\" name=\"city\" size=30 maxlength=50>
      <input type=\"text\" name=\"state\" size=5 maxlength=2>
      <input type=\"text\" name=\"zipcode\" size=10 maxlength=10>
      
      <p><strong>Address Type:</strong><br>
      <input type=\"radio\" name=\"add_type\" value=\"home\" checked> home
      <input type=\"radio\" name=\"add_type\" value=\"work\"> work
      <input type=\"radio\" name=\"add_type\" value=\"other\"> other
      
      <p><strong>Telephone Number:</strong><br>
      <input type=\"text\" name=\"tel_number\" size=30 maxlength=25>
      <input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home
      <input type=\"radio\" name=\"tel_type\" value=\"work\"> work
      <input type=\"radio\" name=\"tel_type\" value=\"other\"> other
      
      <p><strong>Fax Number:</strong><br>
      <input type=\"text\" name=\"fax_number\" size=30 maxlrngth=25>
      <input type=\"radio\" name=\"fax_type\" value=\"home\" checked> home
      <input type=\"radio\" name=\"fax_type\" value=\"work\"> work
      <input type=\"radio\" name=\"fax_type\" value=\"other\"> other
      
      <p><strong>Email Address:</strong><br>
      <input type=\"text\" name=\"email\" size=30 maxlength=150>
      <input type=\"radio\" name=\"email_type\" value=\"home\" checked> home
      <input type=\"radio\" name=\"email_type\" value=\"work\"> work
      <input type=\"radio\" name=\"email_type\" value=\"other\"> other
      
      <p><strong>Personal Note:</strong><br>
      <textarea name=\"note\" cols=35 rows=5 wrap=virtual></textarea>
      <input type=\"hidden\" name=\"op\" value=\"add\">
      <input type=\"hidden\" name=\"master_id\" value=\"$_GET[master_id]\">
      
      <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
      </form>";
      
} else if ($_POST['op'] == "add") {
   //time to add to tables, so check for required fields
   if ((($_POST[f_name] == "") || ($_POST[l_name] == "")) &&
   ($_POST[master_id] == "")) {
      header("Location: addentry.php");
      exit;
   }
   
   //connect to database
   $conn = mysql_connect("localhost", "root", "")
      or die(mysql_error());
   mysql_select_db("smrpg",$conn) or die(mysql_error());
   
   if ($_POST[master_id] == "") {
      //add to master_name table
      $add_master = "insert into master_name values ('', now(),
      now(), '$_POST[f_name]', '$_POST[l_name]')";
      mysql_query($add_master) or die(mysql_error());
      //get master_id for use with other tables
      $master_id = mysql_insert_id();
   } else {
      $master_id = $_POST[master_id];
   }
   
   if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||
      ($_POST[zipcode])) {
      //something relevant, so add to address table
      $add_address = "insert into address values ('', $master_id,
         now(), now(), '$_POST[address]', '$_POST[city]',
         '$_POST[state]', '$_POST[zipcode]', '$_POST[add_type]',)";
      mysql_query($add_address) or die(mysql_error());
   }
   
   if ($_POST[tel_number]) {
      //something relevant, so add to telephone table
      $add_tel = "insert into telephone values ('', $master_id,
         now(), now(), '$_POST[tel_number]', '$_POST[tel_type]')";
      mysql_query($add_tel) or die(mysql_error());
   }
   
   if ($_POST[fax_number]) {
      //something relevant, so add to fax table
      $add_fax = "insert into fax values ('', $master_id, now(),
         now(), '$_POST[fax_number]', '$_POST[fax_type]')";
      mysql_query($add_fax) or die(mysql_error());
   }
   
   if ($_POST[email]) {
      //something relevant, so add to email table
      $add_email = "insert into email values ('', $master_id,
         now(), now(), '$_POST[email]', '$_POST[email_type]')";
      mysql_query($add_email) or die(mysql_error());
   }
   
   if ($_POST[note]) {
      //something relevant, so add to notes table
      $add_note = "replace into personal_notes values ('', $master_id,
         now(), now(), '$_POST[note]')";
      mysql_query($add_note) or die(mysql_error());
   }
   
   $display_block = "<h1>Entry Added</h1>
   <p>Your entry has been added. Would you like to
   <a href=\"addentry.php\">add another</a>?</p>";
}
?>
<html>
<head>
<title>Add An Entry</title>
</head>
<body>
<? print $display_block; ?>
</body>
</html>

Use this:

if (!empty($_GET['master_id']))

 

Validate using:

ctype_digit($_GET['master_id']) /*or*/ is_numeric($_GET['master_id']) /*or*/ $id = (int) $_GET['master_id']

 

For input data which should have a minimum length use:

if (isset($display_name[3]))

 

Where 3 defines the minimum length of $display_name

I changed line 8 to: if (!empty($_GET['master_id'])) as you suggested, but now I am getting this error:

 

 

Parse error: parse error in C:\wamp\www\addentry.php on line 72

 

Line 72 is: } else if ($_POST['op'] == "add") {

 

Also, where do I need to put the other lines of code you suggested for validation and minimum length?

  • 2 weeks later...

I changed line 8 to: if (!empty($_GET['master_id'])) as you suggested, but now I am getting this error:

 

Parse error: parse error in C:\wamp\www\addentry.php on line 72

 

Line 72 is: } else if ($_POST['op'] == "add") {

 

Have you added an { after if (!empty($_GET['master_id']))?

 

Also, where do I need to put the other lines of code you suggested for validation and minimum length?

 

in the body of if (!empty($_GET['master_id']))

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.