Edit a specific mysql row using php
I am creating a RSVP management system for a site. I have already created a page that displays all the results of people who have already registered. I want to be able to create a link on each row that allows the information to be brought up in another page and allows me to edit or delete the information. My thought is that I could but all the information in corresponding text boxes and make the values the original information. I could then change the values and use a update php script.
Could really use some help on this, as I am still a beginner at PHP and MYSQL. Will post code if it will make i easier, but really looking for the code that I would add in the loop on the display page and then the code for an edit.php page that will get the information from only the row selected.
not sure if I have this right
Thanks for the quick reply. Here is partial code where I inserted the code you gave me on the page that views all the rows
PHP Code:
<?php
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<div align="center">
<table width="80%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="10%"><a href="edit.php?id=$row['id']">Edit</a></td><td><a href="delete.php?id=$row['id']">Delete</a></td>
<td width="20%" valign="top" class="accomodations">
1. <? echo $rows['guest_1']; ?><br>
2. <? echo $rows['guest_2']; ?><br>
3. <? echo $rows['guest_3']; ?><br>
4. <? echo $rows['guest_4']; ?><br>
5. <? echo $rows['guest_5']; ?></td>
<td width="10%" valign="top" align="center" class="accomodations"><? echo $rows['total_guests']; ?></td>
<td width="10%" valign="top" align="center" class="accomodations"><? echo $rows['vegetarian']; ?></td>
<td width="22%" valign="top" class="accomodations"><? echo $rows['email']; ?></td>
<td width="18%" valign="top" class="accomodations"><? echo $rows['accomodations']; ?></td>
</tr>
<hr width="80%" align="center">
</table>
</div>
<?
// close while loop
}
// close connection
mysql_close();
?>
Here is the code I have in the edit.php
PHP Code:
<?php
session_start();
if (@$_SESSION['auth'] !="yes")
{
header("Location: login_form.php");
exit();
}
?>
<?php
$host="localhost";
$username="username";
$password="password";
$db_name="db_namer";
$tbl_name="table";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$member_id = $_GET['id'];
$results = mysql_query("select * from member where id = $member_id");
$row = mysql_fetch_assoc($results);
?>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div align="center">
<table width="80%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Guest 1 <input type="text" value="<? echo $row['guest_1']; ?>" name="guest_1"></td>
</tr>
</div>
</body>
</html>
And here is the code for the delete.php
PHP Code:
<?php
$host="localhost";
$username="username";
$password="password";
$db_name="db_namer";
$tbl_name="table";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
header("Location: rsvp_view.php"); // bring back to original page
$member_id = $_GET['id'];
mysql_query("delete from member where id = $member_id limit 1");
?>
When I click edit, I don't get anything in the guest 1 text box, and I get this error "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/m/a/r/markandmichell/html/manager/edit.php on line 22
"
When I click delete, the browser processing, but nothing is deleted on the screen. Not sure what I am doing wrong, but some help would be much appreciated.