Hi g0liatH,
I've tweaked your code to make it clearer to me what's going on.
1) Do yourself a favour and always include some 'error/bug' checking.
2) Another thing I always recommend is not to embed variables in strings ... it's hard to read and is actually ever so slightly slower (if you use single-quoted strings to build blocks of HTML, then you can maintain the use double-quoted attributes in the HTML itself).
3) Whereas you can choose GET or POST for the form action, a link to a php page always uses the GET method,
4) I haven't commented on whether passing variables frompage to page is the best way to 'maintain state' ... sessions might be easier, but that's another story.
5) Lastly, because I don't have access to your db, there could be some minor typo's/glitches so you'll to debug this anyway.
list.php
<html>
<head><title>list.php</title></head>
<body>
<form name="form2" method="post">
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<tr><td colspan="2"><strong><font size="3">Posting form...</font></strong></td></tr>
<tr><td width="8%">Table:</td><td width="92%"><input name="table" type="text" id="table" size="30"></td></tr>
<tr><td> </td><td width="92%"><input type="submit" name="Submit" value="Submit"></td></tr>
</table>
</form>
<?php
// Check to see if variable has been sent
if(!isset($_POST['table']){
die('$_POST[table] not set!');
}
include('db.php');
$q = mysql_query('SELECT * FROM '.$_POST['table'].' ORDER BY id, date DESC');
if(mysql_num_rows($q) == 0){
die('No records found.');
}
$trs = ''
while($r = mysql_fetch_array($q)){
$trs .= '<tr>
<td>
<b>'.$r['title'].'</b>
- options:
<a href="edit.php?id='.$r['id'].'&table='.$r['table'].'" target="_blank">Edit post</a>
| <a href="delete.php?id='.$r['id'].'&table='.$r['table'].'" target="_blank">Delete Post</a>
</td>
</tr>';
}
echo '<table width="100%" cellspacing="2">'.$trs.'</table>';
// Is $cnx defined in db.php ?
mysql_close($cnx);
?>
</body>
</html>
edit.php
<html>
<head><title>edit.php</title></head>
<body>
<?
// Check to see if BOTH variables have been sent
// These will be from a click on a link so
// MUST be $_GET vars, not $_POST.
if(!isset($_GET['table']){
die('$_GET[table] not sent!');
}
if(!isset($_GET['id']){
die('$_GET[id] not sent!');
}
include('db.php');
$q = mysql_query('SELECT * FROM '.$_GET['table'].' WHERE id="'.$_GET['id'].'"');
if(mysql_num_rows($q) == 0){
die('No records found.');
}
$r = mysql_fetch_array($q);
// To include the necessary variable for editprocess.php
// use hidden input fields ... easy to see what's going on
echo '<form action="editprocess.php" method="post">
<input type="hidden" name="table" value="'.$_GET['table'].'" />
<input type="hidden" name="id" value="'.$_GET['id'].'" />
<textarea id="news" name="news">'.$r["news"].'</textarea>
<input type="submit" id="submit" />
</form>';
// Where's $cnx ?
// mysql_close($cnx);
?>
</body>
</html>
editprocess.php
<html>
<head><title>editprocess.php</title></head>
<body>
<?php
if(!isset($_POST['table']){
die('$_POST[table] not sent!');
}
if(!isset($_POST['id']){
die('$_POST[id] not sent!');
}
if(!isset($_POST['news']){
die('$_POST[news] not sent!');
}
include('db.php');
$sql = 'UPDATE '.$_POST['table'].' SET news="'.$_POST['id'].'" WHERE id="'.$_POST['news'].'"';
mysql_query($sql) or die('Error: '.$sql);
// Where's $cnx ?
// mysql_close($cnx);
?>
</body>
</html>
Paul. π