Forum Moderators: coopster

Message Too Old, No Replies

SQL Delete, PHP, Need help

Need help.

         

unclekracker23

12:32 am on Feb 27, 2008 (gmt 0)

10+ Year Member



I am using a small news script. The script allows the adding and editing of news. I am trying to add the deletion of news. Here is my editnews.php file that I am trying to get the Delete working. BTW, I am a newb to PHP and SQL.


<?
//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.

leadegroot

2:26 am on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You haven't actually told us what problem you are encountering,
but I would suggest that you don't need the mysql_query call in the line:
$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 ] )

unclekracker23

4:04 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



Oh, I am sorry. The Delete is not working at all. Like I said earlier, I do not know much about PHP and SQL commands so the Delete section in the above script was written using samples found online.

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.

unclekracker23

6:22 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



I still can't get it to work. Anyone that can help with the deletion of news entires, I could really use the help.

jatar_k

6:35 pm on Feb 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



explain to me what happens exactly from the beginning

from when you view the records because I don't know what the link would look like, so I don't know if you are calling this script correctly. There are also the changes mentioned above so also paste just the delete code that you now have.

Demaestro

6:52 pm on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What jumps out at me first is that the PHP code is checking to see if ($_GET['action']=="delete")

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")

unclekracker23

7:41 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



Thanks for the reply Demaestro.

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.

Demaestro

8:20 pm on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would create a second form, just copy the form you have set up for editing then paste it below and change all the relevant words so it makes sense but the key will be to find the hidden tag named "action" and change it's value from "doedit" to "delete" then try it out.

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.

unclekracker23

9:13 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



This script along with a couple others is only available to the admin of the site to post news in a small section.

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.

Demaestro

11:12 pm on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the PHP file where it has the delete logic change

if($_GET['action']=="delete")
to
if($_POST['action']=="delete")

See if that works

unclekracker23

1:04 am on Feb 28, 2008 (gmt 0)

10+ Year Member



I cant get it to work at all. I think that whole delete section is wrong, as I said earlier I put that in there using examples on other sites and something has to be off.

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>
<?

}
}
?>

Vis3R

1:30 am on Feb 28, 2008 (gmt 0)

10+ Year Member



the code to delete your record would be

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

unclekracker23

1:58 am on Feb 28, 2008 (gmt 0)

10+ Year Member



Vis3R, what would you reccomend on the html? I'm not using much html to post and edit the news. Just these simple php scripts with a few html lines.

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>
<?

}
}
?>

Vis3R

3:28 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



i have NOT tested this, i just wrote it from scratch since yours looks like a mess ;)

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]

unclekracker23

3:50 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



Thanks Vis3R,

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.

Vis3R

4:44 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



It deleted your record because you've used the refresh button, and your post data was re-sent to my script which worked, and so deleted your record.

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>

unclekracker23

9:49 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



Vis3R,

When I upload your file and type in the URL, the screen is blank. Any suggestions? Would you like a link to my test server? Do I need to paste the other 2 files for the news script?

Thanks again.

unclekracker23

9:55 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



Just in case it helps, here are the other files that make up this news script....

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.

Vis3R

10:17 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



well.. i don't know about your other scripts .. but my script works on it's own and doesn't need any other of your scripts except dbconnect.php .. what you have to do is upload this script.. go to it's address .. and it should work. It does work on my server, i've tested it.

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]

unclekracker23

10:35 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



I reuploaded it again and refreshed and it still shows a blank screen. I loaded the news.php script and it shows 2 test news stories. I checked the db as well and everything else is working.

Check your pm amd I will give you the link.

Thanks.

unclekracker23

1:04 am on Feb 29, 2008 (gmt 0)

10+ Year Member



Can someone take a look at this updated script by Vis3R and tell me why it doesnt work for me. My server has PHP 4.4.7.

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>

Vis3R

5:53 pm on Feb 29, 2008 (gmt 0)

10+ Year Member



my server is PHP Version 5.2.5

also.. find the line
$query=mysql_query("UPDATE news SET title='".$title."', news='".$msg."' WHERE id='".$id."'");"

and remove the " at the end...

unclekracker23

8:15 pm on Feb 29, 2008 (gmt 0)

10+ Year Member



That fixed it Vis3R!

Thank you so much for all of your help! I could not have got this working without you. What a wonderful community we have here, I am so appreciative.