Problem with admin script.

Hi people I am a beginner to PHP/MySQL and have been reading a lot lately… This is an admin edit page script I am working on, my problem is when I go to the edit section it…

  1. Doesn’t show the headline story in the story text area form, but it brings up the info on the date, headline title and posted by forms… Plus when I change the story form to just an average one like the others and not a text area it shows it.

  2. When I change any part and hit update it shows me the Headline updates message but it doesn’t actually update any of the information.

Theirs probably more errors than just that in the coding but I am a beginner so please, bare with me… And if anybody notices what else is wrong and could explain where I have went wrong I would really appreciate it… The add and delete parts work fine though. Thanks a lot people.

PS. I’m aware the HTML isn’t perfect, that will be fixed it was just kind of thrown together as I wanted to test the PHP script.

[php]<?php
include(“settings.php”);
include("…/header.php");

$pagename = $_SERVER[‘PHP_SELF’];

$action = $_REQUEST[“action”];
$recid = $_REQUEST[“recid”];

$dbh=mysql_connect ($db_server, $db_username, $db_password) or die (‘I cannot connect to the database.’);
mysql_select_db ($db_database);
?>

News Administration
 
Add  |  Update  |  Delete
"; echo"
 
<?php if ($action == "add") { echo ""; echo ""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo""; echo"
Headline Title:
 
Posted By:
 
Date:
 
News Story:
 
 
"; echo"
"; echo"";
}
elseif ($action == "add2") {
	$headlinetitle = $_REQUEST['headlinetitle'];
	$postedby = $_REQUEST['postedby'];
	$date = $_REQUEST['date'];
	$story = $_REQUEST['story'];

	$SQL = "INSERT INTO " . $db_table . " (headlinetitle,postedby,date,story) VALUES('$headlinetitle','$postedby','$date','$story')";
	$result = @mysql_query($SQL) or die("could not complete your query");
	echo "Headline added.";

}
elseif ($action == "update")  {
	$SQL = "SELECT * FROM " . $db_table . " ORDER BY headlinetitle";
	$result = @mysql_query($SQL) or die("could not complete your query");

echo “<form method=“post” action=”$pagename?action=update2">";
echo “<select class=“textBox” name=“recid” size=“15”>”;
while($row = @mysql_fetch_array($result)) {
$value = $row[‘id’];
$headlinetitle = $row[‘headlinetitle’];
echo “<option value=”$value">";
if ($headlinetitle != $headlinetitle) {
echo “$headlinetitle ($headlinetitle)”;
} else {
echo “$headlinetitle”;
}
echo “”;
}
echo "



<input class=“button” type=“submit” value=“Update”>\n";
}
elseif ($action == “update2”) {
$SQL = “SELECT * FROM " . $db_table . " WHERE id=$recid”;
$result = @mysql_query($SQL) or die(“could not complete your query”);
while($row = @mysql_fetch_array($result)) {
$id = $row[‘id’];
$headlinetitle = $row[‘headlinetitle’];
$postedby = $row[‘postedby’];
$date = $row[‘date’];
$story = $row[‘story’];
}

echo "Record Number: $recid";

echo “<form method=“post” action=”$pagename?action=update3">";
echo “<table width=“779” border=“0” cellspacing=“0” cellpadding=“0” align=“center”>”;
echo"

";
echo" “;
echo”";
echo"";
echo"<td class=“textR” width=“100”>Headline Title:";
echo"<td width=“225”><input class=“textBox” type=“text” name=“headlinetitle” size=“25” value="$headlinetitle">";
echo"";
echo"";
echo" “;
echo”";
echo"";
echo"<td class=“textR” width=“100”>Posted By:";
echo"<td width=“225”><input class=“textBox” type=“text” name=“postedby” size=“25” value="$postedby">";
echo"";
echo"";
echo" “;
echo”";
echo"";
echo"<td class=“textR” width=“100”>Date:";
echo"<td width=“225”><input class=“textBox” type=“text” name=“date” size=“25” value="$date">";
echo"";
echo"";
echo" “;
echo”";
echo"";
echo"<td class=“textR” width=“100”>News Story:";
echo"<td width=“225”><textarea class=“textBox” name=“story” rows=“10” cols=“50” value="$story">";
echo"";
echo"";
echo" “;
echo”";
echo"";
echo “<INPUT TYPE=“hidden” NAME=“recid” VALUE=”$recid">";
echo “<INPUT TYPE=“hidden” NAME=“headlinetitle” VALUE=”$headlinetitle">";
echo “<INPUT TYPE=“hidden” NAME=“postedby” VALUE=”$postedby">";
echo “<INPUT TYPE=“hidden” NAME=“date” VALUE=”$date">";
echo “<INPUT TYPE=“hidden” NAME=“story” VALUE=”$story">";
echo"<td class=“text” width=“100”> “;
echo”<td width=“225”><input class=“button” type=“submit” name=“submit” value=“Update”>";
echo"";
echo"";
echo"";
}
elseif ($action == “update3”) {
$headlinetitle = $_REQUEST[‘headlinetitle’];
$postedby = $_REQUEST[‘postedby’];
$date = $_REQUEST[‘date’];
$story = $_REQUEST[‘story’];
	$SQL = "UPDATE " . $db_table . " SET headlinetitle='$headlinetitle',postedby='$postedby',date='$date',story='$story' WHERE id=$recid";
	$result = @mysql_query($SQL) or die("could not complete your query");
echo "Headline updated.";

}
elseif ($action == "delete")  {
	$SQL = "SELECT * FROM " . $db_table . " ORDER BY headlinetitle";
	$result = @mysql_query($SQL) or die("could not complete your query");
	echo "<form method=\"post\" action=\"$pagename?action=delete2\">";
	echo "<select class=\"textBox\" name=\"recid\" size=\"15\">";
	while($row = @mysql_fetch_array($result)) { 
		$value = $row['id'];
		$headlinetitle = $row['headlinetitle'];
		echo "<option value=\"$value\">";
			if ($headlinetitle != $headlinetitle) {
				echo "$headlinetitle ($headlinetitle)";
			} else {
				echo "$headlinetitle";
			}
			echo "</option>";
	}
	echo "</select>




<input class=“button” type=“submit” value=“Delete”>";
}
elseif ($action == “delete2”) {
$SQL = “DELETE FROM " . $db_table . " WHERE id=$recid”;
$result = @mysql_query($SQL) or die(“could not complete your query”);
echo “Headline Deleted!”;
}
else {
echo “Please select a choice.”;
}
?>

	</td>
</tr>
[/php]

Hi people, it’s me… Jay G, the same person that posted the above thread. I have just registered with this site. I fixed the problem of the text area showing the story information by adding [php]echo stripslashes($story);[/php] in between the text area, it works now. But I still cant figure out why it’s saying it has been updated when I change the information but it doesn’t actually edit anything… any help would be appreciated and please don’t think I am one of them people that throws together anything I can find and then I just post it up on here because im too lazy to try and fix it myself… I hate people like that, I have had numerous problem from when I started with this script and I have managed to fix most by myself but iv tried a lot of things to fix the edit problem and I just cant seem to see where I am going wrong… Note that I am a beginner and when I say beginner I mean beginner… I have literally been learning PHP for a week… Thanks for any help again!

Hello, nice to see someone persevere, stick with it you’ll soon get the hang of it.

Let me give you a few tips:
Don’t do this: @mysql_… The @ sign is suppressing error output that may occur, try and write your code error free, if a warning/error occurs, fix it, don’t mute it.

Stop using $_REQUEST, that’s lazy, use $_GET or $_POST etc. appropriately.
As much as you possibly can, keep the logic(php) separated from the view(html).
Use CSS for styling instead of inline commands like border=“0”, padding=“2” as they are obsolete in HTML5.

Finally, look at lots of other code, written by coders, not learners, and try and find a style that suits you then from that moment on, write your code in the same fashion each and every time - You’ll be surprised how much it helps and also you can start to re-use old code easier this way.

Hope that helps,
Red :wink:

Yeah I really need to fix up with my HTML… I haven’t bothered with coding for years… I used to learn HTML and that’s how everyone I new back then coded but I definitely need to work on that too, I only decided to start properly again and learn PHP etc a week ago so I have to be patient while im learning as frustrating as it can be… Anyway thanks a lot for your post, il take on board everything you said Redscouse… Think il ditch this script and start fresh… I’l keep posting as I make progress… thanks a lot for your help :).

Sponsor our Newsletter | Privacy Policy | Terms of Service