Jump to content


Photo

removing something from a mysql database


  • Please log in to reply
8 replies to this topic

#1 erfg1

erfg1
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 04 October 2006 - 04:51 PM

if (isset($HTTP_GET_VARS['delete'])){
	$sql = "DELETE FROM web_announcements WHERE id=$id";
	$result = mysql_query($sql);
	echo"The announcement has successfully been removed.<br><br>- <a href=./acp.php>Back</a>";
	}
	if (isset($HTTP_GET_VARS['announce2'])){
	echo"You are now ready to either remove or edit an announcement, please select your options below.<hr color=#990000>";
	 $query = "SELECT * FROM web_announcements";
	 $result = mysql_query($query);
	  while ($results = mysql_fetch_array($result)){
	  extract($results);
	  echo"- $title (<a href=>EDIT</a>)/(<a href=$PHP_SELF?delete&id=$id>DELETE</a>)<br>";
	}
	}

What's wrong with this code? It says "The announcement has successfully been removed." But it hasn't.

#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 04 October 2006 - 05:10 PM

if (isset($HTTP_GET_VARS['delete'])){
	$sql = "DELETE FROM web_announcements WHERE id=$id";
	$result = mysql_query($sql);
        /*********************/
        if($result)
        {
             echo"The announcement has successfully been removed.<br><br>- <a href=./acp.php>Back</a>";
        }
        /********************/
	}
	if (isset($HTTP_GET_VARS['announce2'])){
	echo"You are now ready to either remove or edit an announcement, please select your options below.<hr color=#990000>";
	 $query = "SELECT * FROM web_announcements";
	 $result = mysql_query($query);
	  while ($results = mysql_fetch_array($result)){
	  extract($results);
	  echo"- $title (<a href=>EDIT</a>)/(<a href=$PHP_SELF?delete&id=$id>DELETE</a>)<br>";
	}
	}

You need to add another if statement to check if $result returns true or false. Then you can display the success message.

Jeremy

#3 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 04 October 2006 - 05:14 PM

You might be experiencing an error with your delete query thus the web announcement is not deleted. You can check to see if there is an error by using mysql_errno(). Your current code will print "The announcement has successfully been removed." for every instance because you do not check for errors. Adding a conditional statement to your code will help in identifying possible issues.

$sql = "DELETE FROM web_announcements WHERE id=$id";
$result = mysql_query($sql);
// any errors?
if ( mysql_errno() ) {
echo "There was an error deleting the record from the database";
} else {
echo "The announcement has successfully been removed.";
}

Hope this helps.Cheers!

#4 erfg1

erfg1
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 04 October 2006 - 05:39 PM

if ( mysql_errno() ) {
 echo "There was an error deleting the record from the database";
} else {
 echo "The announcement has successfully been removed.";
}

I added that, and it comes back as an error. Why is that? What can I do to fix it?

Also, I did mysql_error() and got this error...
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


#5 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 04 October 2006 - 06:11 PM

How/where do you define $id ? As it is now you are depending for register globals set ON - and my guess is that $id is not set here.

*EDIT* I see it in your second query as GET

Also, you are not setting delete properly in your link (i added the red bit): a href=$PHP_SELF?delete=1&id=$id


<?php

if (isset($_GET['delete']) && !empty($_GET['id']))
{
$sql = mysql_query("DELETE FROM web_announcements WHERE id='$id'") or die(mysql_error());
if($sql) echo "The announcement has successfully been removed.<br><br>- <a href=./acp.php>Back</a>";
else echo "No row deleted..";
}
else
{
echo "You are now ready to either remove or edit an announcement, please select your options below.<hr color=#990000>";
$query = mysql_query("SELECT * FROM web_announcements") or die(mysql_error());
while ($results = mysql_fetch_array($query))
{
extract($results);
echo"- $title (<a href=>EDIT</a>)/(<a href=$PHP_SELF?delete=1&amp;id=$id>DELETE</a>)<br>";
}
}

?>


Another thing (not related to the error) - you should start using $_GET[] instead of $HTTP_GET_VARS[]
http://no2.php.net/m....predefined.php

#6 erfg1

erfg1
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 05 October 2006 - 04:33 AM

Sorry alpine, your code didnt work. Somethings up...

#7 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 05 October 2006 - 02:32 PM

What is the problem now? The same ?

#8 erfg1

erfg1
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 05 October 2006 - 04:39 PM

<?php 
define('IN_PHPBB',true);

$phpbb_root_path = "./forums/"; // set path to phpBB files 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 

// 
// Start session management 
// 
$userdata = session_pagestart($user_ip, PAGE_LOGIN); // initiate session 
init_userprefs($userdata); // load user prefs 
// 
// End session management 
// 
$page_title = "RoAR clan :: ACP"; 

// session id check
if (!empty($HTTP_POST_VARS['sid']) || !empty($_GET['sid']))
{
	$sid = (!empty($HTTP_POST_VARS['sid'])) ? $HTTP_POST_VARS['sid'] : $_GET['sid'];
}
else
{
	$sid = '';
}
$link = mysql_connect(localhost, MY_USERNAME, MY_PASSWORD); 
$db = mysql_select_db(forum, $link);
?>

<?php
			  if ($userdata['session_logged_in'] && !isset($_GET['login']) && !isset($_GET['logout']) && !isset($_GET['announce']) && !isset($_GET['submitannounce']) && !isset($_GET['announce2']) && !isset($_GET['delete']) && !isset($_GET['editannouncement'])){
			  if ($userdata[group_id] = "2"){
			  //We are admin! WELCOME THEM BACK!
			  echo"Welcome back to the administrations control panel for the RoAR clans website $userdata[username]!";
			  //Announcements section
			  echo"<hr color=#990000><b><u><font color=red>ANNOUNCEMENTS SECTION</u></b><br>- <a href=acp.php?announce=true>Add an announcement</a><br>- <a href=acp.php?announce2=true>Remove/Edit an announcement</a><br>- <a href=index.php>View announcements</a>";
			  //Downloads section
			  echo"<hr color=#990000><b><u><font color=red>DOWNLOADS SECTION</u></b><br>- <a href=>Add a download</a><br>- <a href=>Remove/Edit a download</a><br>- <a href=downloads.php>View downloads</a>";
			  //Members section
			  echo"<hr color=#990000><b><u><font color=red>MEMBERS SECTION</u></b><br>- <a href=>Add a member</a><br>- <a href=>Remove/Edit a member</a><br>- <a href=members.php>View registered members</a><br>- <a href=>View membership applications</a>";
			  }
			  else{
			  //Not an admin? GET OUT!
			  echo'You are not an administrator of the RoAR clan! <b><font color="red">GET OUT OF THIS PAGE NOW!</b>';
			  }
	}
	else if (!$userdata['session_logged_in'] && !isset($_GET['login']) && !isset($_GET['logout'])){
	//Not logged in? Please login!
	echo'You are currently not logged in! <b><font color="red">LOG IN NOW!</b>';
	}
	
	//----------------------------
	// ADD ANNOUNCEMENTS SECTION!
	//----------------------------
	if ($userdata[group_id] = "2" && isset($_GET['announce']) && !isset($_GET['submitannounce']) && !isset($_GET['delete']) && !isset($_GET['editannouncement'])){
	$today = date("Y-m-d");
	echo"Welcome to the administration announcement addition part of the administrative control panel. Lets add a new announcement!<hr color=#990000>";
	echo"<form action=\"acp.php?submitannounce=true\" method=\"post\"><table>
	<tr><td>Author:</td><td><input type=\"text\" size=\"30\" name=\"author\" value=\"$userdata[username]\"></td></tr>
	<tr><td>Date:</td><td><input type=\"text\" size=\"30\" name=\"date\" value=\"$today\"></td></tr>
	<tr><td>Title:</td><td><input type=\"text\" size=\"30\" name=\"title\"></td></tr>
	<tr><td>Content:</td><td><textarea size=\"30\" name=\"content\" rows=\"6\" cols=\"40\"></textarea></td></tr>
	<tr><td>- <a href=\"./acp.php\">Back</a></td><td><input type=\"submit\" value=\"Submit New Announcement\"></td></tr>
	</table></form>";
	}
	if (isset($_GET['submitannounce'])){
	 if (!$_POST[content]){ 
     echo "Missing CONTENT, please fill out the form again.<br>";
	 }
     else{
    $query = "INSERT INTO web_announcements (author, date, title, content) VALUES ('$_POST[author]', '$_POST[date]', '$_POST[title]', '$_POST[content]')"; 
    $result = mysql_query($query, $link);
    echo'Your new announcement was succesfully added!<br><br>- <a href="./acp.php">Back</a>'; 
	}
	}
	
	//----------------------------
	// REMOVE/EDIT ANNOUNCEMENTS SECTION!
	//----------------------------
	//DELETE
if (isset($_GET['delete']) && !empty($_GET['id']))
{
$sql = mysql_query("DELETE FROM web_announcements WHERE id='$id'") or die(mysql_error());
if($sql) echo "The announcement has successfully been removed.<br><br>- <a href=./acp.php>Back</a>";
else echo "No row deleted..";
}
else
{
echo "You are now ready to either remove or edit an announcement, please select your options below.<hr color=#990000>";
$query = mysql_query("SELECT * FROM web_announcements") or die(mysql_error());
while ($results = mysql_fetch_array($query))
{
extract($results);
echo"- $title (<a href=>EDIT</a>)/(<a href=$PHP_SELF?delete=1&amp;id=$id>DELETE</a>)<br>";
}
}

//VIEW
	if (isset($_GET['announce2'])){
	echo"You are now ready to either remove or edit an announcement, please select your options below.<hr color=#990000>";
	 $query = "SELECT * FROM web_announcements";
	 $result = mysql_query($query);
	  while ($results = mysql_fetch_array($result)){
	  extract($results);
	  echo"- $title (<a href=$PHP_SELF?editannouncement&id=$id>EDIT</a>)/(<a href=$PHP_SELF?id=$id&delete>DELETE</a>)<br>";
	}
	}
	//EDIT
	if (isset($_GET['editannouncement'])){
	echo"You are now ready to edit an announcement.<hr color=#990000>";
	 $query = "SELECT * FROM web_announcements WHERE id=$id";
	 $result = mysql_query($sql, $link);
	 extract($results);
	 echo"<form action=\"acp.php?submiteditedannounce=true\" method=\"post\"><table>
	 <input type=\"hidden\" name=\"id\" value=\"$id\">
	<tr><td>Author:</td><td><input type=\"text\" size=\"30\" name=\"author\" value=\"$author\"></td></tr>
	<tr><td>Date:</td><td><input type=\"text\" size=\"30\" name=\"date\" value=\"$date\"></td></tr>
	<tr><td>Title:</td><td><input type=\"text\" size=\"30\" name=\"title\" value=\"$title\"></td></tr>
	<tr><td>Content:</td><td><textarea size=\"30\" name=\"content\" rows=\"6\" cols=\"40\">$content</textarea></td></tr>
	<tr><td>- <a href=\"./acp.php\">Back</a></td><td><input type=\"submit\" value=\"Submit Edited Announcement\"></td></tr>
	</table></form>";
	}
	if (isset($_GET['submiteditedannounce'])){
	$sql = "UPDATE web_announcements SET author='$_POST[author]', date='$_POST[date]', title='$_POST[title]', content='$_POST[content]' WHERE id=$id";
	$result = mysql_query($sql, $link);
	}
			  ?>


#9 erfg1

erfg1
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 05 October 2006 - 04:52 PM

I think it just had a problem with carrying over the ID.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users