Jump to content


Photo

help with a php/mysql news script


  • Please log in to reply
7 replies to this topic

#1 UndeadBob

UndeadBob
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 July 2003 - 06:56 PM

i\'m writing my own news script using php and mysql. but i have hit a problem. when i try to delete one entry they all get deleted. i know where the problem is but i don\'t know how to fix it. what happens is when you want to delete a entry you tick a checkbox next to the entry you want to get rid of and it should be deleted. but the php/mysql is setup so that the checkbox on the form are named after the mysql database id. that id number isn\'t sent to mysql when the delete request is made so all the entrys are deleted.
the relevant code is attached. any help is appreciated.

this is the delete form
<form action="delete_notice.php" method="post">$query = "SELECT title, id FROM notices";$result = mysql_query($query);$numrows = mysql_num_rows($result);while($row = mysql_fetch_array($result)){?> <input name="<? echo "$row[id]"; ?>" type=checkbox>  <? echo "$row[title]";print \'<br>\';}?><br><input type="submit" value="Delete selected"></form>

and this is the query that deletes the entries (named delete_notice.php)
$id = $_POST[\'id\'];$link = mysql_connect("localhost", $dbuser, $dbpass) or die ("Database Error: Couldn\'t Connect!");mysql_select_db($dbname, $link) or die ("Couldn\'t open $dbname!");$query = "DELETE FROM notices WHERE \'id\' = \'$id\' ";mysql_query($query, $link) or die ("Couldn\'t delete data!");mysql_close($link);include "header.php";print \'<center>\';print \'<br>\';print \'Notice(s) succesfully deleted\';print \'<br>\';print \'<a href="noticeadmin.php">Return to Notices Administration</a>\';print \'<center>\';

the table that is being accesed is notice and the fields are id (auto-incrementing id no.), title and notice.
i missed out the mysql connect stuff becuase that is ok.

many thanx

#2 BK87

BK87
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts
  • LocationPhiladelphia, PA

Posted 15 July 2003 - 07:05 PM

try replacing

[php:1:55ca29e7a5]
$id = $_POST[\'id\'];
[/php:1:55ca29e7a5]
with
[php:1:55ca29e7a5]
$id = $_POST[\"id\"];
[/php:1:55ca29e7a5]

and

[php:1:55ca29e7a5]<?php
$query = \"DELETE FROM notices WHERE \'id\' = \'$id\' \";
?>[/php:1:55ca29e7a5]
with
[php:1:55ca29e7a5]<?php
$query = \"DELETE FROM notices WHERE id = \'$id\' \";
?>[/php:1:55ca29e7a5]

#3 UndeadBob

UndeadBob
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 July 2003 - 09:02 AM

still no luck, when i made those changes nothing was deleted...

#4 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 16 July 2003 - 11:35 AM

Try this

$query = "DELETE FROM notices WHERE id = \'$id\' ";

Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#5 UndeadBob

UndeadBob
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 July 2003 - 08:17 AM

still no luck, now nothing has been deleted. this is starting to become a real pain the arse. aarrgghh..

#6 panth

panth
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 July 2003 - 12:52 PM

I personally like working with echo rather with <? ?> each time i want to enter HTML code in your first script not in delete_notice.php

as for the delete_notice.php script for debuging i\'d always rather use mysql_error() instead of text in \'or die( )\' part
this will give you a main idea why you have problems
if you still can\'t solve it
come to here and paste what the problems are...

original code :

$id = $_POST[\'id\']; $link = mysql_connect("localhost", $dbuser, $dbpass) or die ("Database Error: Couldn\'t Connect!"); mysql_select_db($dbname, $link) or die ("Couldn\'t open $dbname!"); $query = "DELETE FROM notices WHERE \'id\' = \'$id\' "; mysql_query($query, $link) or die ("Couldn\'t delete data!"); mysql_close($link); include "header.php"; print \'<center>\'; print \'<br>\'; print \'Notice(s) succesfully deleted\'; print \'<br>\'; print \'<a href="noticeadmin.php">Return to Notices Administration</a>\'; print \'<center>\';


change into

$id = $_POST[\'id\']; $link = mysql_connect("localhost", $dbuser, $dbpass) or die (mysql_error()); mysql_select_db($dbname, $link) or die (mysql_error()); $query = "DELETE FROM notices WHERE \'id\' = \'$id\' "; mysql_query($query, $link) or die (mysql_error()); mysql_close($link); include "header.php"; print \'<center>\'; print \'<br>\'; print \'Notice(s) succesfully deleted\'; print \'<br>\'; print \'<a href="noticeadmin.php">Return to Notices Administration</a>\'; print \'<center>\';


#7 UndeadBob

UndeadBob
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 19 July 2003 - 04:40 PM

no mysql errors come up it. it is just that that php/mysql doesn\'t do what i want it to do. it deletes all entrys even one i haven\'t checked the checkbox for when i submit the form. but i have found out that even if i tick no boxes and submit the form all entries are all deleted which makes me think that the checkbox information may not be getting to the mysql query
$query = "DELETE FROM notices WHERE \'id\' = \'$id\' ";


#8 panth

panth
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 22 July 2003 - 05:13 PM

Try do it with links and $_GET instead of $_POST
it\'ll work :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users