Jump to content

update query not working ... please help


chemicaluser

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.