Hi, I was wondering if anyone could help me with this problem. Here is the case, I have a News database which you are able to view here:
http://lotrtw.com/news/index_.php
And users will be able to add/delete/edit. Ive worked out how to add and delete entries but Im stuck with the editing part, everytime when you click the Edit button after checking a box of the record you want to edit (and ofcourse changed the text inside the field) it gets the data from the last item you added instead of the text from the checked record. Can anyone help me so that it will take the information from the checked record.
Sorry if Im a bit vague, check out what happens here:
http://lotrtw.com/news/news_admin.php
News_del.php, where the delete and edit command get executed.
[PHP]<?php
$host="localhos t";
$user="xxx";
$pass="xxx";
$db="news_";
$connect = mysql_connect($ host, $user, $pass);
$dbselect = mysql_select_db ($db, $connect);
$query = "SELECT * FROM news ORDER BY id";
$result = mysql_query($qu ery);
$num_rows = mysql_num_rows( $result);
if(isset($_POST["del"]))
{
for($i=0;$i<$nu m_rows;$i++)
{
$cb = $_POST['cb'];
$delete_records = $cb[$i];
$sql = "DELETE FROM news WHERE ID=$delete_reco rds";
$result = mysql_query($sq l);
}
echo '<script language="javas cript">alert("D eletion succesful, returning to admin...")</script>';
echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=news_admin. php'> ";
}
elseif(isset($_ POST["edit"]))
{
for($i=0;$i<$nu m_rows;$i++)
{
$cb = $_POST['cb'];
$edit_records = $cb[$i];
$title = $_POST['rows1'];
$sql = "UPDATE news SET title = '$title' WHERE id=$edit_record s";
$result = mysql_query($sq l);
}
echo '<script language="javas cript">alert("E dit succesful, returning to admin...")</script>';
echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=news_admin. php'> ";
}
else
{
echo '<script language="javas cript">alert("E dit/deletion failed, returning to admin...")</script>';
}
mysql_close();
?>
[/PHP]
News_admin.php, where you select the records and input new data
[PHP]<?php
//$user_chk=$_POS T['user'];
//$pass_chk=$_POS T['pass'];
$user_test="xxx ";
$pass_test="xxx ";
$host="localhos t";
$user=$user_tes t;
$pass=$pass_tes t;
$db="news_";
$connect = mysql_connect($ host, $user, $pass);
$dbselect = mysql_select_db ($db, $connect);
$query = "SELECT * FROM news ORDER BY submitted";
$result = mysql_query($qu ery);
$submit=$_POST['submit'];
//if(($user_chk == $user_test) & ($pass_chk == $pass_test))
//{
echo "<iframe src='news_add.p hp' height='217' width='100%' scrolling='no' name='frame'>
</iframe>";
echo "<form action='submit. php' method='post'>
<input type='submit' name='submit' value='Refresh content' style='outline: solid'/>
</form>";
echo "<form action='news_de l.php' method='post'>" ;
echo ("<table border='1' width='100%' cellspacing='2' height='100'>") ;
echo "<tr border='0'><td colspan='6'><in put type='submit' name='del' value='Delete selected records' style='outline: solid'/>' '<input type='submit' name='edit' value='Edit selected records' style='outline: solid'/></td></tr>";
while($rows = mysql_fetch_row ($result))
{
echo ("<tr bgcolor='lightb lue'><td><input type='checkbox' name='cb[]' value='$rows[0]'></td><td><input type='text' name='rows1' value='$rows[1]'></td><td><input type='text' name='rows2' value='$rows[2]' size='100'></td><td><input type='text' name='rows3' value='$rows[3]'></td><td><input type='text' name='rows4' value='$rows[4]'></td><td><input type='text' name='rows5' value='$rows[5]'></td></tr>");
}
echo "</form>";
echo "</table>";
/*}
else
{
echo "Error: Return to login screen <a href='news_logi n.php'>here</a>.";
}*/
?>[/PHP]