twilitegxa Posted July 9, 2009 Share Posted July 9, 2009 In the following code, I receive the following error: Notice: Undefined index: op in C:\wamp\www\addentry.php on line 2 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from concat_ws(' ', f_name, l_name) as display_name from master_name where i' at line 1 <?php if (($_POST['op'] != "add") || ($_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 from 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> What is the correct syntax to use here and how do I define this index? Someone told me I could us if(!isset($_POST['op'])), but if I use that, I still get the error. What is the problem? Quote Link to comment Share on other sites More sharing options...
RussellReal Posted July 9, 2009 Share Posted July 9, 2009 'op' is not getting POSTED thru a form or w.e is goin on on the page.. and you are trying to pull from a table concated thru values that you'd get from a table, so ofcourse there is an error.. you're trying to pull from a table named something like: 'firstName lastName' but you're using fields instead of static text.. also you're3 not actually SELECTING anything.. coz you just used SELECT FROM not SELECT * FROM Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 9, 2009 Author Share Posted July 9, 2009 I don't really understand what I need to do differently. :-( Does it have to say SELECT * FROM to select from the table? This tutorial in this book I'm working from doesn't have the *. It just says select from. It this outdated code, possibly that I'm using? I'm not sure what to do about the 'op' thing. As stated in the last post, someone told me to use isset, but it's not working on this one. It worked on my other ones though. How do I fix the problem where I'm pulling from the concated table? This is how it was in the tutorial. :-( Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 10, 2009 Author Share Posted July 10, 2009 Can anyone see the problem? I'd like to get this code working again. I had most of it working before. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 10, 2009 Share Posted July 10, 2009 $get_names = "SELECT CONCAT_WS(' ', f_name, l_name) as display_name FROM master_name WHERE id = $_GET[master_id]"; SELECT (column) FROM (table) WHERE (condition) As for the rest.. :S , lets go a step at a time.. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 10, 2009 Author Share Posted July 10, 2009 So would it then be just: $get_names = "SELECT (' ', f_name, l_name) FROM master_name WHERE id= $_GET[master_id]"; ? The tutorial in the book uses that CONCAT_WS thing and also the as display_name. Should I leave these out? Is the above statement correct? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 10, 2009 Share Posted July 10, 2009 No, the basic syntax is similar to: SELECT (column) FROM (table) WHERE (condition) Your query should (probably) be what I posted: $get_names = "SELECT CONCAT_WS(' ', f_name, l_name) as display_name FROM master_name WHERE id = $_GET[master_id]"; CONCAT_WS(' ', f_name, l_name) AS display_name == the column (combing two as one with the CONCAT_WS function), then naming it to "display_name" for easy reference. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 10, 2009 Author Share Posted July 10, 2009 Oh, okay. I misunderstood. Sorry about that. Okay, I changed that, and now I'm receiving these errors: Notice: Undefined index: op in C:\wamp\www\addentry.php on line 2 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 What does it mean by undefined index? And did I not define the variable for display_name? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 10, 2009 Share Posted July 10, 2009 It means that nothing exists at $_POST['add'] because $_POST['add'] was never created. It's likely due to how you're making the form, and testing it. It's very hard for me to read what you're doing, but it seems like an "if they haven't submitted the form, show this, otherwise show that" or something.. I think the issue is that you're looking for variable that don't exist yet. Most of those errors will go away when you fix the root error it seems. The root error being the condition that look for if the form has been submitted. As far as I can see you look in all cases to see if $_POST['op'] == 'add' as your driving condition.. maybe the top one should be <?php if (!isset($_POST['op']) || ($_GET['master_id'] != "")) { meaning that the form has not been submitted (and therefor doesn't exist).(Line 2 error, probably) The line 8 error is because you are not accessing the page correctly as : yourpage.php?master_id=2, instead probably just : yourpage.php Line 24 and 67 should fix if you fix the line 2 and 8 errors. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 10, 2009 Author Share Posted July 10, 2009 I changed the line you suggested, but I'm still getting these errors: 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 The page is supposed to let users add their address, and then after that, another link is supposed to send them back to this page which allows them to edit their address. So the page is called addentry.php and should first allow them to add an entry into the "address book". Then, if they use select their name from the selentry.php page (which works), they can click an edit info link, which takes them to a page like this: http://localhost/addentry.php?master_id=24, which should allow them to edit their post. But when I try to use the edit one, it just brings up a blank page right now and the add entry one isn't working, as you can tell from the above errors. The delentry.php (delete entry) page is also generating an error. :-( So addentry.php should allow them to add their address, and IF they want to edit it, allow this as well. But only give the option after they have added their address. It shouldn't try to do it initially. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 10, 2009 Share Posted July 10, 2009 At this point the best I can say is walk through it step by step. Start breaking the code down from the top to make sure everything is getting to it. So it says master_id is not defined, so at the very top debug like this: <?php echo $_GET['master_id']; die(); //... (rest of code) then access the page as: http://yourhost.com/addentry.php?master_id=24 and make sure it is printing 24 Go through it in small sections like that making sure the variables do indeed contain what you expect. I'd rather suggest redoing the form as it seems rather convoluted, but it seems like it's nearly there, so it's up to you how much you want to mess with it. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 10, 2009 Author Share Posted July 10, 2009 Well, that does print 24, but I don't understand much about debugging. Can you give me a few more examples of how I'd do it in this script? Since it printed 24, doesn't that mean it's able to get the master_id? Why does it say it isn't a defined index? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 10, 2009 Share Posted July 10, 2009 Basically, you go through line by line checking to make sure it behaves as expected. Your next step might be: <?php if (($_POST['op'] != "add") || ($_GET['master_id'] != "")) { var_dump($_POST['op']); var_dump($_GET['master_id']); die(); And check to see what those variables contain, etc.. keep going line by line or section.. and see that it works as intended. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 12, 2009 Author Share Posted July 12, 2009 When I added that code, I received these errors: Notice: Undefined index: op in C:\wamp\www\addentry.php on line 3 NULL Notice: Undefined index: master_id in C:\wamp\www\addentry.php on line 4 NULL What does this mean? Quote Link to comment 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.