Jump to content

Undefined Variable?


twilitegxa

Recommended Posts

Now when I try to delete a record from my database, it produces the following error:

 

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

 

Here is my 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>

 

How is this variable not defined? What have I done wrong? Line 82 is the <?php print $display_block; ?> at the end.

Link to comment
Share on other sites

The original code was supposed to be:

 

} else if  ($_POST[op] == "view) {

 

and

 

if ($_POST[op] != "view") {

 

but I was getting another error and was told to change the first one to:

 

if(!isset($_POST['op'])

 

so I assumed I would make the same changes to the other one. Can anyone tell me what I am supposed to change these to?

Link to comment
Share on other sites

I changed the print to echo int he display_block message and changes the else if statement to } else if(isset($_POST['op'])) { instead of } else if(!isset($_POST['op'])) {. But now some of the records are deleting and some are not. The emails, personal notes, and telephone numbers deleted from their respective tables, but the address and fax numbers did not delete. There were also some undefined variable errors, but I can't remember which ones now. But now it says there are no records in the database. It said the records were deleted and like I said, some of the field were deleted, but some weren't. Can anyone help with the reason why maybe the fax and address fields were not deleted?

Link to comment
Share on other sites

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.