Update Code

They have: 8 posts

Joined: Mar 2003

Hello, I have a script I am working on and cannot figure it out. It is supposed to update a database but it does not. THe code is as follows:

<?php
session_start
();
if (isset(
$_SESSION['username']))
{
$title1 = \"Update a Joke\";
require('config.php');
echo <<<END
<html><head><title>
$title1</title><link href=\"styles.css\" rel=\"stylesheet\" type=\"text/css\"></head>
<body>
<center>
<table cellpadding=\"0\" cellspacing=\"0\" width=\"400\">
<!--Start Head-->
<tr><td height=\"30\" width=\"30\" background=\"images/lcorner.gif\"></td><td background=\"images/tophead.gif\" width=\"340\"><center><b><font size=\"5\">
$title1</font></b></center></td><td width=\"30\" height=\"30\" background=\"images/rcorner.gif\"></td></tr>
<!--End Head-->
<tr><td colspan=\"3\">
<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#B3B3B3\" style=\"BORDER-COLLAPSE: collapse\" border=\"1\">
<tr><td>
<table cellpadding=\"0\" cellspacing=\"0\" height=\"250\" width=\"100%\">
<tr><td valign=\"top\">
END;
if(
$_GET['act'] == \"upd\")
{
if(!
$_POST['update'])
{
require('files/anav1.php');
echo <<<END
<hr width=\"80%\">
<center>You did not check a box.<br>Please go back and change it.<p>
<input type=\"button\" onClick=\"java script:history.go(-1)\" value=\"<-- Back\"></center>
END;
footer();
die;
}
else
{
// Changes made here
foreach(
$_POST['update'] as $id)
{
$sql = \"UPDATE `joke2` SET `name` = '\" . $_POST['name'][$id] . \"'\";
$sql .= \", `joke` = '\" . $_POST[\"joke\"][$id] . \"'\";
$sql .= \", `date` = '\" . $_POST[\"date\"][$id] . \"'\";
$sql .= \" WHERE `ID` = $id\";
mysql_query(
$sql) or die(\"Unable to process query: \" . mysql_error());
}
// End of changes
require('files/anav1.php');
echo <<<END
<hr width=\"80%\">
<br><br><center>The selected jokes have been updated.<br>Please <a href=\"../index.php\">Click Here</a> to view jokes.</center>
<p>&nbsp;</p>
END;
}
footer();
die;
}
$q = mysql_query(\"SELECT * FROM joke2\");
if(!mysql_num_rows(
$q))
{
require('files/anav1.php');
echo <<<END
<hr width=\"80%\">
<center><br><br>
<h1>No Jokes Found</h1></center>
END;
footer();
die();
}
else
{
require('files/anav1.php');
echo <<<END
<hr width=\"80%\">
<center><br>
<form method=\"post\" action=\"update2.php?act=upd\">
<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\" width=\"300\">
<tr><td>&nbsp;</td><td width=\"55\"><b>Name:</b></td><td width=\"3\">&nbsp;</td><td width=\"212\"><b>Joke:</b></td><td width=\"3\">&nbsp;</td><td><b>Date:</b></td></tr>
END;
}
while(
$d = mysql_fetch_array($q) )
{
echo (' <tr valign=\"top\">
<td><input type=\"checkbox\" name=\"update[]\" value=\"' . stripslashes(
$d['ID']) . '\"></td>
<td width=\"55\"><input type=\"text\" name=\"name[' . stripslashes(
$d['ID']) . ']\" value=\"' . stripslashes($d['name']) . '\" size=\"9\" maxlength=\"9\"></td>
<td width=\"3\">&nbsp;</td>
<td width=\"212\"><TEXTAREA name=\"joke[' . stripslashes(
$d['ID']) . ']\">' . stripslashes($d['joke']) . '</TEXTAREA></td>
<td width=\"3\">&nbsp;</td>
<td><input type=\"text\" name=\"date[' . stripslashes(
$d['ID']) . ']\" value=\"' . stripslashes($d['date']) . '\" size=\"9\" maxlength=\"9\"></td>
</tr>');
}
echo <<<END
<tr><td colspan=\"6\"><center><input type=\"submit\" value=\"Update Jokes\">&nbsp;&nbsp;<input type=\"reset\" value=\"Clear Checks\"></center></td></tr></table></form></center>
END;
footer();
}
else
{
require('index.php');
}

function footer()
{
echo <<< END
<!--End main body-->

</td></tr></table></td></tr></table></td></tr></table><center>

<font size=\"1\" color=\"#000000\">&copy; <a class=\"jokes\" href=\"http://www.wlhosting.com\"><font size=\"1\" color=\"#000000\">Daniel Kassner</font></a></font></center>

</body></html>
END;
}

?>

Then the database is:
#
# Table structure for table `j_logintable`
#

CREATE TABLE j_logintable (
ID int(10) NOT NULL auto_increment,
username varchar(30) NOT NULL default '',
password varchar(30) NOT NULL default '',
PRIMARY KEY (ID)
) TYPE=MyISAM;
# --------------------------------------------------------

#
# Table structure for table `j_topic`
#

CREATE TABLE j_topic (
int int(11) NOT NULL auto_increment,
name mediumtext NOT NULL,
PRIMARY KEY (int)
) TYPE=MyISAM;
# --------------------------------------------------------

#
# Table structure for table `joke2`
#

CREATE TABLE joke2 (
ID int(10) NOT NULL auto_increment,
name varchar(9) NOT NULL default '1',
joke longtext NOT NULL,
date varchar(10) NOT NULL default '2',
PRIMARY KEY (ID,ID),
KEY ID (ID)
) TYPE=MyISAM;

If anybody can help me that would be great. Thanks!

Daniel
WLHosting.com
[email protected]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

What do you see after you submit the form?

Do you know if it actually entering the foreach()?
You may want to replcae the mysql_query() with die(). This will let you know if it is entering the foreach(), and if so, what the exact query is that is being run.

I'm assuming you are running PHP>=4.1.0

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 8 posts

Joined: Mar 2003

thanks for your help. I got it to work now. I forgot to change the part with the form action to the new file name. "Its always the minor things that cause the biggest problems." Thanks!

Daniel
WLHosting.com
[email protected]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.