modify a record?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

modify a record?

Post by lostinphp »

i have a form to add records and i am also able to delete them.
now im wondering wat shud be done if i want to modify them.plus wat do i do to delete one record at a time.;
i did DELETE FROM homepage WHERE 'text'='$text' but it didnt work so i did DELETE FROM homepage so this command deletes all records,naturally.
thank u.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Create a form based on a record ID coming from the URL.

Populate your form according to what's pulled from the database from your record ID

Process your form, updating the record as necessary.

Deleting records looks like this:

Code: Select all

DELETE FROM table WHERE column = 'criteria'

// an example

DELETE FROM users WHERE userID = '17'
Modifying an existing record:

Code: Select all

<?
// If you're posting data, then process and query
if($_POST['action'] == "editrecord")
{
   $recordtoedit = mysql_real_escape_string($_POST['recordtoedit']);
   $player = $_POST['player'];
   $team = $_POST['team'];
   $year = $_POST['year'];
   $coach = $_POST['coach'];
   $wins = $_POST['wins'];
   $losses = $_POST['losses'];
   // Update the record now
   mysql_query("UPDATE table SET player = '$player', team = '$team', year = '$year', coach = '$coach', wins = '$wins', losses = '$losses' WHERE id = '$recordtoedit'");
}

// Now comes the start of the page if you're not posting any data
// Identify your record you want to edit in your URL
// so url looks something like http://www.domain.com/editrecord.php?id=103

// Make variable, and make it safe for MySQL
$record = mysql_real_escape_string($_GET['record']);

// Get an associative array of all fields for this record
$array = mysql_fetch_assoc(mysql_query("SELECT * FROM table WHERE id = '$record'"));

// We'll pretend this record has to deal with basketball cards, we'll store each field in it's own variable
$player = $array['player'];
$team = $array['team'];
$year = $array['year'];
$coach = $array['coach'];
$wins = $array['wins'];
$losses = $array['losses'];

// We now have the information ready to popuplate a form
?>
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<form action="<? echo $_SERVER['php_self']; ?>" method="post">
<input type="hidden" name="action" value="editrecord">
<input type="hidden" name="recordtoedit" value="<? echo $record; ?>">
Player:  <input type="text" name="player" size="20" value="<? echo $player; ?>">
Team:  <input type="text" name="team" size="20" value="<? echo $team; ?>">
Year:  <input type="text" name="year" size="20" value="<? echo $year; ?>">
Coach: <input type="text" name="coach" size="20" value="<? echo $coach; ?>">
Wins: <input type="text" name="wins" size="20" value="<? echo $wins; ?>">
Losses: <input type="text" name="losses" size="20" value="<? echo $losses; ?>">
<input type="submit" value="Edit Record">
</form>
</body>
</html>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply