chemicaluser Posted April 21, 2011 Share Posted April 21, 2011 I would like to edit a record that I already have in my database but my query is not working for some reason. Hope someone can help. The errors i get are Notice: Undefined variable: first_name in C:\chemicaluser\www\editinfo.php on line 40 Notice: Undefined variable: last_name in C:\chemicaluser\www\editinfo.php on line 40 Notice: Undefined variable: log_number in C:\chemicaluser\www\editinfo.php on line 40 .... line 40 = my query $query = "UPDATE table1 SET first_name = '$first_name', last_name = '$last_name' WHERE log_number = '$log_number' "; Very simple database, 1 table w/ 2 fields table name = table1 fields = first_name, last_name my php file <?php require ('/menu.php'); require_once('sqlconnect.php'); if (isset($_GET['log_number']) && isset($_GET['first_name']) && isset($_GET['last_name']) ) { $log_number = $_GET['log_number']; $first_name = $_GET['first_name']; $last_name = $_GET['last_name']; } else if (isset($_POST['log_number']) && isset($_POST['first_name']) && isset($_POST['last_name']) ) { $log_number = $_POST['log_number']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; } if (isset($_POST['submit'])) { $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query = "UPDATE table1 SET first_name = '$first_name', last_name = '$last_name' WHERE log_number = '$log_number' "; $result = mysqli_query($dbc, $query) or die ('Error querying database'); mysqli_close($dbc); } ?> my form <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="first_name">First name</label> <input type="text" id="first_name" name="first_name" value="<?php if (!empty($first_name)) echo $first_name; ?>"/> <label for="last_name">Last Name</label> <input type="text" id="last_name" name="last_name" value="<?php if (!empty($last_name)) echo $last_name; ?>" /> <input type="submit" value="Edit Information" name="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/234323-update-query-not-working-please-help/ Share on other sites More sharing options...
Muddy_Funster Posted April 21, 2011 Share Posted April 21, 2011 ok, you're a little off center here - You have told the form that it is using POST, so you don't need to check for $_GET. You have 2 fields in your table, but refference a third in your WHERE condidtion. You're variables are not being deffined as you are checking for (isset($_POST['log_number'])) which does not exist in your form. Quote Link to comment https://forums.phpfreaks.com/topic/234323-update-query-not-working-please-help/#findComment-1204397 Share on other sites More sharing options...
murli800 Posted April 21, 2011 Share Posted April 21, 2011 first of all make field in ur database whose name would be log_number..bcz u mentioned u have only 2 fields..and no need to use log_number in your program instead in query..log_number just provide a identification that which row to update..if you want the full code then i can provide that just reply to this post Quote Link to comment https://forums.phpfreaks.com/topic/234323-update-query-not-working-please-help/#findComment-1204422 Share on other sites More sharing options...
murli800 Posted April 21, 2011 Share Posted April 21, 2011 these are the codes i have written to update delete and insert data in database [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/234323-update-query-not-working-please-help/#findComment-1204424 Share on other sites More sharing options...
chemicaluser Posted April 21, 2011 Author Share Posted April 21, 2011 Sorry but I did not give you the proper structure of my database.... I do have a third+fourth field in my database (log_number and date_added) ... log_number its my p.key however the only two fields that i would like up update are the first_name and last_name ...i still get the same results even when completely removing the check for GET .. actually what ends up happening then, in my form when i view the record, the data already in my database field does not show up in the input box .... its just two empty input boxes could it have something to do with not mentioning the date_added in my query? murli800 ... thanks for the files, i will try playing around with your code later (ill let you know how it goes) Quote Link to comment https://forums.phpfreaks.com/topic/234323-update-query-not-working-please-help/#findComment-1204573 Share on other sites More sharing options...
DavidAM Posted April 22, 2011 Share Posted April 22, 2011 The errors you are getting have nothing to do with the database. That is PHP telling you that you have not defined the variables: $first_name, $last_name, and $log_number. They are not defined because your IF tests are based on ALL THREE having been passed from the form. But there is no INPUT field on the form called log_number, so your if (isset($_POST['log_number']) && isset($_POST['first_name']) && isset($_POST['last_name']) ) { is never true, so NONE of the fields are defined. Then you test for the SUBMIT button to generate the UPDATE query. Well, the submit button exists, so you try to create the query and you can't because those fields are not defined. Since your UPDATE depends on the value of log_number, you need to get that value from somewhere. It looks like you expected it to be on the form, so maybe you need to look at the code that generates the form and fix it to add the log_number. Quote Link to comment https://forums.phpfreaks.com/topic/234323-update-query-not-working-please-help/#findComment-1204752 Share on other sites More sharing options...
chemicaluser Posted April 26, 2011 Author Share Posted April 26, 2011 Thanks DavidAM, i was just missing the log number on my form Quote Link to comment https://forums.phpfreaks.com/topic/234323-update-query-not-working-please-help/#findComment-1206301 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.