slaterino Posted January 23, 2009 Share Posted January 23, 2009 I am getting the 'Notice: Undefined index' message for the 'id' field of my query. I have tried loads of different ways of trying to sort this out, checked all spellings, checked I'm calling everything from the tables that I should be, and still can't get this thing to work. Could someone have a glance over my code and let me know if they see anything that could be causing this problem. Thanks! $id = $_GET['id']; if(isset($_POST['submit'])) { $Site_Status = addslashes($_POST['Site_Status']); $Inactive_msg = addslashes($_POST['Inactive_msg']); $Main_Inactive_msg = addslashes($_POST['Main_Inactive_msg']); $NewReleaseMsg = addslashes($_POST['NewReleaseMsg']); $result = mysql_query("UPDATE status (Site_Status, Inactive_msg, Main_Inactive_msg, NewReleaseMsg) VALUES ('$Site_Status', '$Inactive_msg', '$Main_Inactive_msg', '$NewReleaseMsg') WHERE StatusID='$id' " ,$conn); } elseif($id) { $result = mysql_query("SELECT * FROM status WHERE StatusID='$id' ",$conn); while($myrow = mysql_fetch_assoc($result)) { $Site_Status = $myrow['Site_Status']; $Inactive_msg = $myrow['Inactive_msg']; $Main_Inactive_msg = $myrow['Main_Inactive_msg']; $NewReleaseMsg = $myrow['NewReleaseMsg']; Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/ Share on other sites More sharing options...
justinh Posted January 23, 2009 Share Posted January 23, 2009 your table doesn't have a primary index set? Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744731 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 I didn't think that UPDATE was formatted like that, I though that was for INSERT. Isn't it supposed to be? UPDATE status SET Site_Status = '$Site_Status', Inactive_msg = '$Inactive_msg', Main_Inactive_msg = '$Main_Inactive_msg', NewReleaseMsg = '$NewReleaseMsg' WHERE StatusID='$id' add or die(mysql_error()) after your query executions. You should also put your queries in strings before you execute them so you can echo them out and see what's actually being passed in. Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744735 Share on other sites More sharing options...
revraz Posted January 23, 2009 Share Posted January 23, 2009 Yes, his UPDATE is wrong. the undefined ID is probably coming way before your query. Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744738 Share on other sites More sharing options...
trq Posted January 23, 2009 Share Posted January 23, 2009 I didn't think that UPDATE was formatted like that, I though that was for INSERT. The syntax is fine, either way is valid for an UPDATE. Your entire block of code should be wrapped within another if statement. eg; if (isset($_GET['id'])) { $id = $_GET['id']; // rest of code } I have a feeling $_GET['id'] is not defined. Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744751 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 I didn't think that UPDATE was formatted like that, I though that was for INSERT. The syntax is fine, either way is valid for an UPDATE. Thanks for clearing that up, I thought I remembered people using it that way, but I never have. Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744755 Share on other sites More sharing options...
trq Posted January 23, 2009 Share Posted January 23, 2009 Actually, I was wrong. Either way is valid for an INSERT, not an UPDATE. To the op; You need to change your query to be as Maq describes above. Sorry for the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744759 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 Now I'm confused. UPDATE have to use the format: UPDATE table SET blah = '$blah' INSERT can use both ways ? Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744765 Share on other sites More sharing options...
slaterino Posted January 23, 2009 Author Share Posted January 23, 2009 Okay, well I've changed the UPDATE query as suggesed. I actually missed a bit from my original question though. The error occurs when I click the submit button. The address i'm using to test the page is status.php?id=1 and it's bringing up the correct details. However, when I click submit this error occurs. This is the tag I'm using the form: <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> This is causing the page to lose the ?id=1 off the end of the link. Why would it do this? Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744776 Share on other sites More sharing options...
trq Posted January 23, 2009 Share Posted January 23, 2009 This is causing the page to lose the ?id=1 off the end of the link. Why would it do this? Because you don't re-send the id along with the form. <form method="post" action="id=<?php echo $_GET['id']; ?>"> Note: You don't need to use $_SERVER['PHP_SELF']. Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744784 Share on other sites More sharing options...
slaterino Posted January 23, 2009 Author Share Posted January 23, 2009 I see, thanks for the tip, have had another go at doing this. Now I get the error: The requested URL /login/id=1 was not found on this server. How can I change that second slash to a question mark Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744789 Share on other sites More sharing options...
trq Posted January 23, 2009 Share Posted January 23, 2009 Sorry, thats my fault. Typo. <form method="post" action="?id=<?php echo $_GET['id']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-744806 Share on other sites More sharing options...
slaterino Posted January 25, 2009 Author Share Posted January 25, 2009 Hi, I'm still having problems with this script and really can't figure out why. When I click submit the screen simply becomes blank. I have double checked all entries against those in the database and made sure all entries are spelt correctly. I have error reporting turn on but no errors come up at all. If anyone has any ideas regarding this could they please let me know. Thanks! This is my current script: include '../library/opendb.php'; error_reporting(E_ALL); ini_set("display_errors", 1); $id = $_GET['id']; if(isset($_POST['submit'])) { $Site_Status = addslashes($_POST['Site_Status']); $Inactive_msg = addslashes($_POST['Inactive_msg']); $Main_Inactive_msg = addslashes($_POST['Main_Inactive_msg']); $NewReleaseMsg = addslashes($_POST['NewReleaseMsg']); $result = mysql_query("UPDATE status SET Site_Status='$Site_Status', Inactive_msg='$Inactive_msg', Main_Inactive_msg='$Main_Inactive_msg', NewReleaseMsg='$NewReleaseMsg') WHERE StatusID='$id' " ,$conn); echo "Status Updated Successfully!"; } elseif($id) { $result = mysql_query("SELECT * FROM status WHERE StatusID='$id' ",$conn); while($myrow = mysql_fetch_assoc($result)) { $Site_Status = $myrow['Site_Status']; $Inactive_msg = $myrow['Inactive_msg']; $Main_Inactive_msg = $myrow['Main_Inactive_msg']; $NewReleaseMsg = $myrow['NewReleaseMsg']; ?> <form method="post" action="?id=<?php echo $_GET['id']; ?>"> <input type="hidden" name="id" value="<?php echo $myrow['id']; ?>" /> <select name="Site_Status" id="Site_Status" value="<?php echo $Site_Status;?>"> <option value="In-Active">In-Active</option> <option value="Active">Active</option> </select> <textarea name="NewReleaseMsg" cols="40" rows="4" wrap="VIRTUAL"><?php echo $NewReleaseMsg;?></textarea> <textarea name="Main_Inactive_msg" cols="40" rows="4" wrap="VIRTUAL"><?php echo $Main_Inactive_msg;?></textarea> <textarea name="Inactive_msg" cols="40" rows="4" wrap="VIRTUAL"><?php echo $Inactive_msg;?></textarea> <input name="submit" type="submit" id="submit" value="Save Settings"> </form> } } include '../library/closedb.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-746075 Share on other sites More sharing options...
gevans Posted January 25, 2009 Share Posted January 25, 2009 $result = mysql_query("UPDATE status SET Site_Status='$Site_Status', Inactive_msg='$Inactive_msg', Main_Inactive_msg='$Main_Inactive_msg', NewReleaseMsg='$NewReleaseMsg' WHERE StatusID='$id' " ,$conn); Use that query Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-746079 Share on other sites More sharing options...
slaterino Posted January 25, 2009 Author Share Posted January 25, 2009 Yes, works perfectly! Was it that extra closed bracket then, I must have looked over the code so many times without seeing that. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-746098 Share on other sites More sharing options...
gevans Posted January 25, 2009 Share Posted January 25, 2009 yup, thats all it was Quote Link to comment https://forums.phpfreaks.com/topic/142169-notice-undefined-index/#findComment-746099 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.