Forum Moderators: coopster
<?
//include our database connection file
include('dbconnect.php');if($_POST['action']=="doedit"){
//grab the post vars
$title = $_POST['title'];
$id = $_POST['id'];
$news = $_POST['news'];
//update the database
$news = "UPDATE news SET title='$title', news='$news' WHERE id = $id";
$editnews = mysql_query($news);
echo("news edited.");
}
//print the news titles, with links to the edit page
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("> $title<a href=editnews.php?id=$id&action=edit> Edit</a>");
echo("> <a href=editnews.php?id=$id&action=delete> Delete</a><br />");
}
echo("<br /><br />");
//if we are editing a news item, print the following..
if($_GET['action']=="edit"){
$id = $_GET['id'];
$getnews = mysql_query("select * from news WHERE id=$id");
while($r=mysql_fetch_array($getnews)){
extract($r);
//our form
//if we are deleting a news item, print the following..
if($_GET['action']=="delete"){
$query1=mysql_query("delete from news where id=$id");
mysql_query($dbname, $query1) or die("Failed Query of " . $query1);
echo("> $title . $id . ' has been deleted as requested'.<br>");
}
//our form
?>
<form action="editnews.php" method="POST">
<input type="text" name="title" value="<? echo($title); ?>" /><br />
<textarea name="news" rows="6" cols="50"><? echo($news); ?></textarea>
<input type="submit" value="edit" />
<input type="hidden" name="id" value="<? echo($id); ?>" />
<input type="hidden" name="action" value="doedit" />
</form>
<?
}
}
?>
Thanks for any help you can give.
$query1=mysql_query("delete from news where id=$id");and the syntax for mysql_query in the following line:
mysql_query($dbname, $query1) or die("Failed Query of " . $query1);is wrong. The call to mysql_query should have these parameters:
mysql_query ( string $query [, resource $link_identifier ] )
When I click the delete link next to the news thread I want to delete nothing happens. I refresh the page to see if the topics are gone but they are still there.
I will try your suggestions and report back.
Looking at your form you have this hidden element
<input type="hidden" name="action" value="doedit" />
This will execute the edit logic in the PHP code but nothing else.
You either need a new form for deleting that has the hidden elements value="delete" Or you need to change it from a hidden element to a drop down list or something where the user can select the action they want.
From what I can tell ($_GET['action']=="delete") will always return false and so the code doesn't get run ever.
If you do have an element named "action" set to "delete" make sure you only have one element with the name "action" otherwise it will return a list and you will need to change all your code to read.....
if ("delete" in $_GET['action'])
instead of
if ($_GET['action']=="delete")
The form you are referring to only comes up when a user clicks the dit link that is displayed beside the news title. Originally these scripts were only set up to add news and edit news, no option to delete.
So I was trying to splice in a option to delete but have no idea what I am doing. The script I posted was the edit news script because I figured that would be the best one to change.
Should I try to change the edit news script into a delete news script only?
I really need help so thanks for your time with this.
You need to be careful about making a delete statement available to users, but I guess if they have the right to edit then giving them the right to delete isn't that big a deal.
If it doesn't work post back.
The script is very simple, consisting of a dbconnect.php file which has the login info for the db. It also has a news.php file which diplays the news whereever you call the file in the page.
Then there is this editnews file that I posted. This is the only file that mentions the hidden action you were referring to. I tried adding the second form and changing the words but it still does not work. The delete link shows up when you click edit but when you click delete nothing happens.
If it were you, how would you write in that section to add delete. Here is the original file
<?
//include our database connection file
include('dbconnect.php');if($_POST['action']=="doedit"){
//grab the post vars
$title = $_POST['title'];
$id = $_POST['id'];
$news = $_POST['news'];
//update the database
$news = "UPDATE news SET title='$title', news='$news' WHERE id = $id";
$editnews = mysql_query($news);
echo("news edited.");
}
//print the news titles, with links to the edit page
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("> <a href=editnews.php?id=$id&action=edit>$title</a><br />");
}
echo("<br /><br />");
//if we are editing a news item, print the following..
if($_GET['action']=="edit"){
$id = $_GET['id'];
$getnews = mysql_query("select * from news WHERE id=$id");
while($r=mysql_fetch_array($getnews)){
extract($r);
//our form
?>
<form action="editnews.php" method="POST">
<input type="text" name="title" value="<? echo($title); ?>" /><br />
<textarea name="news" rows="6" cols="50"><? echo($news); ?></textarea>
<input type="submit" value="edit" />
<input type="hidden" name="id" value="<? echo($id); ?>" />
<input type="hidden" name="action" value="doedit" />
</form>
<?
}
}
?>
if($_GET['action']=="delete"){
$query1=mysql_query("delete from news where id=$id");
if ($query1)
echo "$title has been deleted as requested. <br>";
else
echo "Failed to delete $title <br>";
}
but you have to work on your HTML to ever get to executing this code
DaMaestro mentioned adding another form so if I put your and his comments together I get the following which looks good but still does not delete when the delete button is clicked.
<?
//include our database connection file
include('dbconnect.php');if($_POST['action']=="doedit"){
//grab the post vars
$title = $_POST['title'];
$id = $_POST['id'];
$news = $_POST['news'];
//update the database
$news = "UPDATE news SET title='$title', news='$news' WHERE id = $id";
$editnews = mysql_query($news);
echo("news edited.");
}
//print the news titles, with links to the edit page
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("> <a href=editnews.php?id=$id&action=edit>$title</a><br />");
}
echo("<br /><br />");
//if we are editing a news item, print the following..
if($_GET['action']=="edit"){
$id = $_GET['id'];
$getnews = mysql_query("select * from news WHERE id=$id");
while($r=mysql_fetch_array($getnews)){
extract($r);
//our form
if($_GET['action']=="delete"){
$query1=mysql_query("delete from news where id=$id");
if ($query1)
echo "$title has been deleted as requested. <br>";
else
echo "Failed to delete $title <br>";
}
?>
<form action="editnews.php" method="POST">
<input type="text" name="title" value="<? echo($title); ?>" /><br />
<textarea name="news" rows="6" cols="50"><? echo($news); ?></textarea>
<input type="submit" value="edit" />
<input type="hidden" name="id" value="<? echo($id); ?>" />
<input type="hidden" name="action" value="doedit" />
</form>
<br>
<br>
<form action="editnews.php" method="POST">
<? echo($title); ?>
<input type="submit" value="delete" />
<input type="hidden" name="id" value="<? echo($id); ?>" />
<input type="hidden" name="action" value="delete" />
</form>
<?
}
}
?>
it should work, but as i said i didn't test it because i don't have time to put up a database just for this. let me know if you get any errors, if something doesn't work, or if you don't understand any part of it.
<?
include('dbconnect.php');
$id = $_POST["id"];
$title = $_POST["txtTitle"];
$msg = $_POST["txtMsg"];
if ($_POST["action"] == "edit")
{
$query=mysql_query("SELECT * FROM news WHERE id=".$id);
if ($query)
{
echo "$title is ready to be edited. <br><br>";
$row=mysql_fetch_array($query);
}
else
editNews();
}
elseif ($_POST["action"] == "delete")
{
$query=mysql_query("DELETE FROM news WHERE id=".$id);
if ($query)
echo "$title was successfully deleted. <br><br>";
else
echo "Failed to delete $title <br><br>";
editNews();
}
elseif ($_POST["action"] == "doedit")
{
$query=mysql_query("UPDATE news SET title=".$title.", news=".$msg." WHERE id=".$id);
if ($query)
echo "$title was successfully updated. <br><br>";
else
echo "Failed to update $title <br><br>";
editNews();
}
else
{
editNews();
}
function editNews()
{
$query=mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row=mysql_fetch_array($query))
echo "<a href=editnews.php?id=".$row["id"]."&action=edit>".$row["title"]."</a><br />";
exit(0);
}
?>
<form action="editnews.php" method="post">
Title: <input type="text" name="txtTitle" value="<? echo $row['title'] ?>" />
<input type="submit" name="Submit1" value="Change" />
<br />
Message: <br />
<textarea name="txtMsg" rows="6" cols="50"><? echo $row['news'] ?></textarea>
<input type="hidden" name="action" value="doedit" />
<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
</form>
<br />
<br />
<form action="editnews.php" method="post">
<input type="submit" name="Submit2" value="Delete" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
</form>
[edited by: Vis3R at 3:31 pm (utc) on Feb. 28, 2008]
The first time I ran it, it deleted one of the news items automatically. It displayed "was successfully deleted" then below that it showed one of the other news itme s that did not have a subject.
I then went to the addnews script and added a couple more test news items. I then loaded the editnews again. This time it did not delete anything at first. It did show the "was successfully deleted" at the top of the screen and then below that the 2 news items I added with clickable links.
When I click the titles of the news items I added, the deleted mesage dissappears and the 2 news items remain at the top. No form ever shows up.
If you would like a link to a test server that I have setup for this, pm me and I will give it to you.
Thanks again for all of your help.
Below is a working version. Now edit and everything else works as it should, i've put up a database and tested it. Don't refresh the websites, because their post data get re-sent. Just type in the url again and click go.
<?
include('dbconnect.php');
$id = $_POST["id"];
$title = $_POST["txtTitle"];
$msg = $_POST["txtMsg"];
if ($_GET["action"] == "edit")
{
$query=mysql_query("SELECT * FROM news WHERE id='".$_GET["id"]."'");
if ($query)
{
$row=mysql_fetch_array($query);
echo $row["title"]." is ready to be edited. <br><br>";
}
else
editNews();
}
elseif ($_POST["action"] == "delete")
{
$query=mysql_query("DELETE FROM news WHERE id='".$id."'");
if ($query)
echo "The record was successfully deleted. <br><br>";
else
echo "Failed to delete the record <br><br>";
editNews();
}
elseif ($_POST["action"] == "doedit")
{
$query=mysql_query("UPDATE news SET title='".$title."', news='".$msg."' WHERE id='".$id."'");"
if ($query)
echo "$title was successfully updated. <br><br>";
else
echo "Failed to update $title <br><br>";
editNews();
}
else
{
editNews();
}
function editNews()
{
$query=mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row=mysql_fetch_array($query))
echo "<a href=editnews.php?id=".$row["id"]."&action=edit>".$row["title"]."</a><br />";
exit(0);
}
?>
<form action="editnews.php" method="post">
Title: <input type="text" name="txtTitle" value="<? echo $row['title'] ?>" />
<input type="submit" name="Submit1" value="Change" />
<br />
Message: <br />
<textarea name="txtMsg" rows="6" cols="50"><? echo $row['news'] ?></textarea>
<input type="hidden" name="action" value="doedit" />
<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
</form>
<br />
<br />
<form action="editnews.php" method="post">
<input type="submit" name="Submit2" value="Delete" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
</form>
addnews.php
<form action="submit.php" method="post">
<b>Title</b>
<BR />
<input type="text" name="title" size="40" maxlength="80" value="" />
<br />
<br />
<b>News</b><BR><textarea name="news" rows="3" cols="40"></textarea>
<br />
<br />
<input type="submit" value="submit" /> <input type="reset" value="reset" />
</form>
submit.php
<?
//grabs the variables
$news = $_POST["news"];
$title = $_POST["title"];
//gets mysql info
include("dbconnect.php");
//gets the current date...
$date = date("j F");
$addnews =MYSQL_QUERY("INSERT INTO news (id,title,date,news)". "VALUES ('NULL', '$title', '$date', '$news')");
//success...
echo("News Added!");
?>
news.php
<?
include("dbconnect.php");
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("<span class=eventhdr> $title</span> <br><BR>$news");
echo("<br>");
}
?>
Hope this helps.
do this ...
1. upload the script and the dbconnect.php ..
2. go to the url of the site by typing it in
3. once you're on the url, click refresh
4. it should work.
if you see a blank screen.. it means there's no news in your database.. or news without titles..
[edited by: Vis3R at 10:21 pm (utc) on Feb. 28, 2008]
I checked out the script on Vis3R's server and it works there but I do not know what version PHP his server has.
<?
include('dbconnect.php');$id = $_POST["id"];
$title = $_POST["txtTitle"];
$msg = $_POST["txtMsg"];
if ($_GET["action"] == "edit")
{
$query=mysql_query("SELECT * FROM news WHERE id='".$_GET["id"]."'");
if ($query)
{
$row=mysql_fetch_array($query);
echo $row["title"]." is ready to be edited. <br><br>";
}
else
editNews();
}
elseif ($_POST["action"] == "delete")
{
$query=mysql_query("DELETE FROM news WHERE id='".$id."'");
if ($query)
echo "The record was successfully deleted. <br><br>";
else
echo "Failed to delete the record <br><br>";
editNews();
}
elseif ($_POST["action"] == "doedit")
{
$query=mysql_query("UPDATE news SET title='".$title."', news='".$msg."' WHERE id='".$id."'");"
if ($query)
echo "$title was successfully updated. <br><br>";
else
echo "Failed to update $title <br><br>";
editNews();
}
else
{
editNews();
}
function editNews()
{
$query=mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row=mysql_fetch_array($query))
echo "<a href=editnews.php?id=".$row["id"]."&action=edit>".$row["title"]."</a><br />";
exit(0);
}
?>
<form action="editnews.php" method="post">
Title: <input type="text" name="txtTitle" value="<? echo $row['title'] ?>" />
<input type="submit" name="Submit1" value="Change" />
<br />
Message: <br />
<textarea name="txtMsg" rows="6" cols="50"><? echo $row['news'] ?></textarea>
<input type="hidden" name="action" value="doedit" />
<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
</form>
<br />
<br />
<form action="editnews.php" method="post">
<input type="submit" name="Submit2" value="Delete" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
</form>