Jump to content

removing something from a mysql database


erfg1

Recommended Posts

[code]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>";
}
}[/code]

What's wrong with this code? It says "The announcement has successfully been removed." But it hasn't.
Link to comment
https://forums.phpfreaks.com/topic/22990-removing-something-from-a-mysql-database/
Share on other sites

[code]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>";
}
}[/code]

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

[color=maroon]Jeremy[/color]
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!
[code]if ( mysql_errno() ) {
echo "There was an error deleting the record from the database";
} else {
echo "The announcement has successfully been removed.";
}
[/code]

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...
[code]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[/code]
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[color=red]=1[/color]&id=$id

[code]

<?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>";
}
}

?>

[/code]

Another thing (not related to the error) - you should start using $_GET[] instead of $HTTP_GET_VARS[]
http://no2.php.net/manual/en/language.variables.predefined.php
[code]<?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);
?>[/code]

[code]<?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);
}
  ?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.