Jump to content

How to modify a record


seriousdamage

Recommended Posts

Hi,

I have just begun to create my first database,

after I query the DB, the records are shown in a table,

I would like to have for every record returned a possibility to be able to modify it by using a form and then resubmit into the database the changed record.

below is the search script I use,

could anyone help me with this?

 

<?php

$where = @$_POST['name'] ;
$trimmed = trim($where); //trim whitespace from the stored variable

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($where))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }



$link = mysql_connect('localhost', 'Username', 'password') 
            or  die(mysql_error());
mysql_select_db("contacts"); 

$where = mysql_real_escape_string ($_POST['name']); 

$query = 'SELECT * FROM contacts WHERE name=\''.$where.'\''; 
  
$sql = mysql_query($query); 

while ($row = mysql_fetch_object($sql)) { 
  //print your data as you like 
    echo "<table border=1><tr><td>$row->name</td><td><a href=delete.php?del=$row->name>Delete</a></td><td><a href=modify.php?mod=$row->name>Modify</a></td></tr></table>"; 
} 

?>

Link to comment
https://forums.phpfreaks.com/topic/83135-how-to-modify-a-record/
Share on other sites

Is your goal to search on an exact entry or part of it?  For example, your code will only find "joe" if "joe" exists.  If "joseph" exists, your code won't find it.  So you may want to either broaden your search by using LIKE along with % or provide a list of all entries and then click on one to edit.

Thanks revraz, that is a good tip which I need to use.

But the part I can't make work is :

when the query returns the records, are returned in a simple html table,

I am trying to then select one of the records and output it in a form so that I can change it and after that I can update it by re-submitting to form.

 

How can I output a record in a form after is in an html table?

in your table add another field for a link like Revraz has mentioned... write it with the id# as the link.  such as

 

echo "<a href='editpage.php?id=$id'>EDIT THIS RECORD</a>";  // $id will be your variable assigned the sql unique id your table gives it.

 

on your editpage.php

just have a form for the fields you want to edit, then use the submit script to update the database for that ID#

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.