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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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#

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.