Page 1 of 1
modify a record?
Posted: Thu Sep 01, 2005 3:21 am
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.
Posted: Thu Sep 01, 2005 3:24 am
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>