Jump to content

Recommended Posts

Ok i have my database with 1 table in it, say i wanna edit a record which has been submited, say because it is stupid or typoed etc. I would like to be able to update it with text feilds, say name is spelt wrong, i have a text feild where i typed the correct name in and it updates that row of information. How would i go about doing this, any nice tuts or some advice is great. It can be very simple :D nothing advanced

Link to comment
https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/
Share on other sites

Your first the pull whatever row you want to edit from the database, you'll place each column within a form field. When you submit the form you just run an UPDATE query, eg

 

UPDATE TABLE `table_here` SET col1name=`$col1value`,  col12name=`$col2value` etc WHERE row_id_col=$rowid

This i what i had so far,

<?php

mysql_select_db("roberts_work", $connect);

mysql_query("UPDATE tblbasicform SET location = 'Harlow'
WHERE name = 'chris' AND email = '[email protected]'");

mysql_close($connect);



?>

 

But i need it to work in a form, for example, i have a db with email and name in, i search the users by email. On my page i would like 2 text feilds and an update button, when i fill out them 2 feileds, what ever user with that name, is updated with the text i put in.

If you have setup a unique primary key in your database, you could use it for updating the information.

 

Create the form that has the fields you want to update the database:

<form name="form1" method="post">
Input First Name: <input type="text" name="firstname" /><br />
Input Email: <input type="text" name="email" /><br />
Input Location: <input type="text" name="location" /><br />
Input ID Number: <input type="text" name="id" /><br />
<input type="submit" name="submit" value="Update Data" />
</form>

 

Of course being a little rusty, in the PHP section of the page you do something like this:

<?php
  
  foreach($_POST as $key => $value)
      $$key = $value;  //This sets the form field names to values.

  mysql_query("UPDATE table SET location = '$location', name = '$firstname', email = '$email' WHERE id = '$id'") or die(mysql_error());

?>

 

That way you have a clean update of any information that needs to be updated.

 

 

I dont understand that completly, it doesnt update when i changed everything to. I would of thought first i would need a box where i search for name/id number, then if i get a result back it would let me use 4 text boxxes to update the rows i have.

I dont understand that completly, it doesnt update when i changed everything to. I would of thought first i would need a box where i search for name/id number, then if i get a result back it would let me use 4 text boxxes to update the rows i have.

 

You could do a drop down box that selects all the people in the database and sets the values of the form to auto-populate with the information and you can change and update the information.

 


<?php

  $nameQry = "SELECT * FROM database";
      $result = mysql_query($nameQry) or die(mysql_error());
  
  while($row = mysql_fetch_array($result)){
    $name = $row['name'];
    $email = $row['email'];
    $location = $row['location'];
    $id = $row['id'];
  }

?>

 

Then in your form control you have this:

 

<form name="form1" method="post">
<select id="names" name="names">
  <option>option values</option>
</select>
  Input First Name: <input type="text" name="firstname" value="<? echo $name ?>" /><br />
  Input Email: <input type="text" name="email" value="<? echo $email ?>" /><br />
  Input Location: <input type="text" name="location" value="<? echo $location?>" /><br />
  Input ID Number: <input type="text" name="id" value="<? echo $id?>" /><br />
<input type="submit" name="submit" value="Update Data" />
</form>

 

or something along those lines.

Alright....lets try this:

 

<?php

  $nameQry = "SELECT * FROM database";
      $result = mysql_query($nameQry) or die(mysql_error());

  $i = 0;  

  while($row = mysql_fetch_array($result)){
    $name = $row['name'];
    $email = $row['email'];
    $location = $row['location'];
    $id = $row['id'];
    $optBox[$i] = $row;
    $i++;
  }

  if(isset($names)){
     $valueQry = "SELECT * FROM database WHERE id = '$id'";
         $result = mysql_query($valueQry) or die(mysql_error());

    while($row = mysql_fetch_array($result)){
        $valID = $row['id'];
        $valName = $row['name'];
        $valEmail = $row['email'];
        $valLocation = $row['location'];
    }
  }

?>

 

Then in the form area:

 


<form name="form1" method="post">
<select id="names" name="names">
  <?
     for ($j=0; sizeof( $optBox ); j++){
        echo "<option value='".$optbox[$j]['id']."'>".$optbox[$j]['name']."</option>";
     }
  ?>
</select>
  Input First Name: <input type="text" name="firstname" value="<? echo $valName ?>" /><br />
  Input Email: <input type="text" name="email" value="<? echo $valEmail ?>" /><br />
  Input Location: <input type="text" name="location" value="<? echo $valLocation?>" /><br />
  Input ID Number: <input type="text" name="id" value="<? echo $valID?>" /><br />
<input type="submit" name="submit" value="Update Data" />
</form>

 

This code is untested, but in theory should help.

i forgot to put the form code bit in the code, now im reciving back

 

Parse error: syntax error, unexpected T_INC, expecting ')' in C:\xampp\htdocs\Website\update.php on line 35

 

i took out a ; to see if that fixxed it and i get unexpected 2 string to

Hi, you are missing the $ infront of your variable j when incrementing it

 

change:

for ($j=0; sizeof( $optBox ); j++){

 

to:

 

for ($j=0; sizeof( $optBox ); $j++){

 

Thanks for catching that.  Usually when I code, I go a bit fast and forget to put in the $ and ; every once in a while.  It bugs the crap out of people when that happens, but it happens.

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.