twilitegxa Posted July 8, 2009 Share Posted July 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/ Share on other sites More sharing options...
ignace Posted July 8, 2009 Share Posted July 8, 2009 What is he displaying? Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-871389 Share on other sites More sharing options...
twilitegxa Posted July 8, 2009 Author Share Posted July 8, 2009 A blank page. :-( It also displays as blank when records are in the database. Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-871419 Share on other sites More sharing options...
twilitegxa Posted July 8, 2009 Author Share Posted July 8, 2009 I don't know if those lines are really the problem, because it doesn't display anything even when there are entries in the database. Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-871475 Share on other sites More sharing options...
twilitegxa Posted July 8, 2009 Author Share Posted July 8, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-871510 Share on other sites More sharing options...
twilitegxa Posted July 9, 2009 Author Share Posted July 9, 2009 I though the problem was solved, but no I am receiving this error: Notice: Undefined variable: display_block in C:\wamp\www\delentry.php on line 82 What happened? Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-871701 Share on other sites More sharing options...
twilitegxa Posted July 9, 2009 Author Share Posted July 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-871750 Share on other sites More sharing options...
ignace Posted July 9, 2009 Share Posted July 9, 2009 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'])) { Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-871956 Share on other sites More sharing options...
twilitegxa Posted July 12, 2009 Author Share Posted July 12, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-873838 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-874025 Share on other sites More sharing options...
twilitegxa Posted July 13, 2009 Author Share Posted July 13, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-874320 Share on other sites More sharing options...
ignace Posted July 22, 2009 Share Posted July 22, 2009 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'])) Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-880354 Share on other sites More sharing options...
conker87 Posted July 22, 2009 Share Posted July 22, 2009 <input type=\"hidden\" name=\"master_id\" value=\"$_GET[master_id]\"> Should be: <input type=\"hidden\" name=\"master_id\" value=\"{$_GET[master_id]}\"> Quote Link to comment https://forums.phpfreaks.com/topic/165236-display_block-no-displaying-message/#findComment-880359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.