twilitegxa Posted July 8, 2009 Share Posted July 8, 2009 I am receiving the following errors when trying to add an entry to my database: Notice: Undefined index: tel_phone in C:\wamp\www\addentry.php on line 81 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 Here is my code, with the lines in question marked: <?php if(!isset($_POST['op'])) { //havent seen the form, so show it $display_block = "<h1>Add An Entry</h1> <form method=\"post\" action=\"$_SERVER[php_SELF]\"> <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></p> <p><strong>Address:</strong><br> <input type=\"text\" name=\"address\" size=30></p> <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> <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> <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> <p><strong>Fax Number:</strong><br> <input type=\"text\" name=\"fax_number\" size=30 maxlength=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> <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> <p><strong>Personal Note:</strong><br> <textarea name=\"note\" cols=35 rows=5 wrap=virtual></textarea> <input type=\"hidden\" name=\"op\" value=\"add\"></p> <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'] == "")) { 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()); //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(); if (($_POST['address']) || ($_POST['city']) || ($_POST['state']) || ($_POST['zipcode'])) { //something relevant, so add to address book $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, //line 81 now(), now(), '$_POST[tel_phone]', '$_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, //line 95 now(), now(), '$_POST[email]', '$_POST[email_type]')"; //line 96 mysql_query($add_email) or die(mysql_error()); } if ($_POST['note']) { //something relevant, so add to notes table $add_note - "insert into personal_notes value ('', $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> <?php print $display_block; ?> </body> </html> What did I do wrong? Quote Link to comment https://forums.phpfreaks.com/topic/165239-solved-undefined-indexvariable-query-was-empty/ Share on other sites More sharing options...
Daniel0 Posted July 8, 2009 Share Posted July 8, 2009 You're trying to access an undefined index in array, just like the error message says. You're not allowed to do that. The error message also tells you on what line and in what file it's occurring. Quote Link to comment https://forums.phpfreaks.com/topic/165239-solved-undefined-indexvariable-query-was-empty/#findComment-871374 Share on other sites More sharing options...
twilitegxa Posted July 8, 2009 Author Share Posted July 8, 2009 That doesn't help me. :-( I don't understand what the error message means exactly. I understand what line it's on (81), and what the index is (tel_phone), but what I don't understand is how to define it. I was doing this from a tutorial in a book, but I don't know what I'm missing. Can someone tell me what I've done wrong? I fixed the errors for undefined variables; I was missing an = sign (it was a - instead). Quote Link to comment https://forums.phpfreaks.com/topic/165239-solved-undefined-indexvariable-query-was-empty/#findComment-871414 Share on other sites More sharing options...
Daniel0 Posted July 8, 2009 Share Posted July 8, 2009 Essentially you just need to ensure that the index exists before trying to read from it. If it's coming from something like $_POST then you can use isset, empty or array_key_exists (the two first being preferred over the last one). Quote Link to comment https://forums.phpfreaks.com/topic/165239-solved-undefined-indexvariable-query-was-empty/#findComment-871447 Share on other sites More sharing options...
twilitegxa Posted July 8, 2009 Author Share Posted July 8, 2009 I tried adding if(isset($_POST['tel_phone'])), and it worked. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/165239-solved-undefined-indexvariable-query-was-empty/#findComment-871462 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.