...

View Full Version : problems with form in newsscript



g0liatH
10-02-2004, 08:27 PM
ok I'm following a tutorial on a news script with php, but instead of using one table, I want to use more tables. so everytime $table comes in the script, I have to create a form to let the script know which table I'll use.
This worked pretty well until I came to the edit page:

list.php (edited)

<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>&nbsp;</td>
<td width="92%"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
<?php

if($_POST['table'] == td_news) {

include('db.php');
$table = $_POST['table'];

$q = mysql_query("SELECT * FROM $table ORDER BY id, date DESC");



echo "<table with='100%' cellspacing='2'>";

while($r=mysql_fetch_array($q)){

$id = $r["id"];

$title = $r["title"];



echo "<tr><td><b>$title - options: <a href=\"edit.php?id=$id\" target=\"_blank\">Edit post</a> |

<a href=\"delete.php?id=$id\" target=\"_blank\">Delete Post</a></td></tr>";

}

echo "</table>";

mysql_close($cnx);
}
?>

ok I can see the list with news if I enter the table, but then comes edit.php:


<?

include('db.php');

$table = $_COOKIE['table'];

$id = $_GET['id'];

$q = mysql_query("SELECT * FROM $table WHERE id='$id'");

$r = mysql_fetch_array($q);

$news = $r["news"];

echo "<form action=\"editprocess.php?id=$id\" method=\"post\">

<textarea id=\"news\" name=\"news\">$news</textarea><input type=\"submit\"

id=\"submit\"></form>";

?>

I tried about everything to get the table variable here, even cookies :mad:

can someone please help me?

also: Instead of the type form I want to have a list form so you can just select the right table. how can I do that?

help is appreciated :)

Celtboy
10-03-2004, 07:04 AM
I would create a master table, with rows containing the names of the different tables.

ie:



# Table: 'tbl_news_tables'
#
CREATE TABLE `tbl_news_tables` (

`table_id` int(11) NOT NULL auto_increment,

`table_name` varchar(255) default '',

`table_description` varchar(255) default '',

PRIMARY KEY (`table_id`),

UNIQUE KEY `table_id` (`table_id`)

) TYPE=MyISAM;




Then I would do the following:

list.php:


<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%">
<select name="news_table" id="news_table">
<?php

include('db.php');
$query = "SELECT * FROM tbl_news_tables";

$result = mysql_query($query);
if (!$result) { die( "The following Error Occurred: " .mysql_error()); }

if (mysql_num_rows($result) != 0) {

while($row = mysql_fetch_assoc($result)) {
print "<option value=\"" . $row["table_name"] . "\">" . $row["table_description"] . "</option>\n";
}

} else {
print "We were unable to pull data from the database.<br/>";
}
?>

</select>
</td>
</tr>

<tr>
<td>&nbsp;</td>
<td width="92%"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>



<?php


if (isset($_POST["news_table])) {

$news_table = $_POST["news_table"];
$q = mysql_query("SELECT * FROM $news_table ORDER BY id, date DESC");

echo "<table with='100%' cellspacing='2'>";

while($r=mysql_fetch_array($q)){

$id = $r["id"];
$title = $r["title"];

echo "<tr><td><b>$title - options: <a href=\"edit.php?id=$id&news_table=" . $news_table . "\" target=\"_blank\">Edit post</a> |
<a href=\"delete.php?id=$id\" target=\"_blank\">Delete Post</a></td></tr>";

}

echo "</table>";
}

mysql_close($cnx);
?>


and for edit.php :


<?php

include('db.php');

$id = $_GET['id'];
$news_table = $_GET['news_table'];



/* generate the select box, populated with table names. defaults to current table */
$select_news_table = "<select name=\"news_table\">\n";
$query_select_table = mysql_query("SELECT * FROM tbl_news_tables");
if (!($query_select_table) { die( "The follwowing error occurred: " . mysql_error()); }
if (mysql_num_rows($query_select_table) != 0) {

while ($row = mysql_fetch_assoc($query_select_table)) {
($row["table_name"] == $news_table) ? $selected = "SELECTED" : $selected = "";

$select_news_table .= "<option value=\"" . $row["table_name"] . "\" . $selected . ">" . $row["table_description"] . "</option>\n";
}
}
$select_news_table = "</select>";


/* generate the edit form, including the table-select <select> field */
$q = mysql_query("SELECT * FROM $table WHERE id='$id'");

if (!$q) { die( "The following Error Occurred: " . mysql_error()); }

if (mysql_num_rows($q) != 0) {

$r = mysql_fetch_array($q);
$news = $r["news"];

echo "<form action=\"editprocess.php?id=$id&news_table=" . $news_table . "\" method=\"post\">\n";
echo "News Table: " . $select_news_table . "\n<br/>";
echo "News: <textarea id=\"news\" name=\"news\">$news</textarea>";
echo "<input type=\"submit\" id=\"submit\" value=\"Edit News\">\n
echo "</form>";

}

?>


Now i didn't actually test any of that code, so don't be surprised if it throws up a few Parse errors, whatever.

Basically what I did was:

create a master index of all tables
To generate the select box, simply grab all possible tables from the master index, setting the default on the edited page.
To pass the table name to use, simply append it to the url, and access it via the $_GET superglobal



I hope that helps. I'll try it whenever I get around to it... :-P

notes:

You could also get the list of tables my executing a schema sql query, but that doesn't seem like the best method, to me.
Again, I didn't actually test any code.
I renamed a the generic "table" variable, as it was too non-descriptive
I added some mysql_error checking ---- VERY BASIC
I dropped the use of the cookie.



DO NOT DELETE YOUR ORIGINALS...lol. At least, i wouldn't. You can if you want, but make sure you have backups, as I don't know what the exact consequences of running my code could be lol :-P

HTH!

-Celt

g0liatH
10-03-2004, 11:37 AM
hmm thanks, but there are indeed some parse errors in the code. I tried to get them out but I don't really know how to fix this:


print "<option value=\"" . $row["table_name"] . "\">" . $row["table_description"] . "</option>\n";

and some more :p

I'm kinda new to php so...

and if I use that new table, then I have to edit all the other files too, right? :eek:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum