Jump to content

DELETE FROM


Ptsface12

Recommended Posts

Hello,

I currently have a piece of code, that works when it doesn't check if the buttons been pressed. However, when put into ISSET the code doesn't work, and the MySQL echos back

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 '1' at line 1

 

My full code is:

<? 
session_start();
if(!session_is_registered(username)){
header("location:../index.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PS3Panda • Housekeeping • Home</title>
<link href="styles/layout.css" rel="stylesheet" type="text/css" />
<link href="styles/wysiwyg.css" rel="stylesheet" type="text/css" />
<!-- Theme Start -->
<link href="themes/blue/styles.css" rel="stylesheet" type="text/css" />
<!-- Theme End -->

</head>
<?php


if ((isset($_GET['action']))&&($_GET['action'] == 'delete')) {
$user_id = (int) $_GET['id'];
include 'config.php';
mysql_connect(localhost,$username, $password);
@mysql_select_db($database) or die ("Unable to select Database");
$query = mysql_query("SELECT username FROM users WHERE id='$user_id'");
$row = mysql_fetch_assoc($query);
}

If(isset($_POST['delete']))
	{
         $user_id = (int) $_GET['id'];
include 'config.php';
mysql_connect(localhost,$username, $password);
@mysql_select_db($database) or die ("Unable to select Database");
$result = mysql_query("DELETE FROM users WHERE id='$user_id'");
$return = mysql_query($result) or die(mysql_error());
echo $return;
}
?>

<body id="homepage">
<div id="header">
    	<a href="" title=""><img SRC="img/cp_logo.png" alt="Control Panel" class="logo" /></a>
    	<div id="searcharea">
            <p class="left smltxt"><a href="#" title="">Advanced</a></p>
            <input type="text" class="searchbox" value="Search control panel..." onclick="if (this.value =='Search-2520control-2520panel..-2E'){this.value=''}"/>
            <input type="submit" value="Search" class="searchbtn" />
        </div>
    </div>
        
    <!-- Top Breadcrumb Start -->
    <div id="breadcrumb">
    	<ul>	
        	<li><img SRC="img/icons/icon_breadcrumb.png" alt="Location" /></li>
        	<li><strong>Location:</strong></li>
            <li><a href="#" title="">Manage Users</a></li>
            <li>/</li>
            <li class="current">Delete <?php echo $row['username']; ?></li>
        </ul>
    </div>
    <!-- Top Breadcrumb End -->
     
    <!-- Right Side/Main Content Start -->
    <div id="rightside">
    
    	<!-- Status Bar Start --><!-- Status Bar End -->
        
         <!-- Red Status Bar Start --><!-- Red Status Bar End -->
        
        <!-- Green Status Bar Start --><!-- Green Status Bar End -->
        
        <!-- Blue Status Bar Start --><!-- Blue Status Bar End -->   
    
        <!-- Content Box Start -->
      <div class="contentcontainer">
            <div class="headings alt">
                <h2>Delete <?php echo $row['username']; ?>?</h2>
            </div>
            <div class="contentbox">
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="2%"> </td>
                  <td width="98%">You have decided to delete <?php echo $row['username']; ?>, would you like to delete them from the database?</td>
                </tr>
                <tr>
                  <td> </td>
                  <td> </td>
                </tr>
                <tr>
                  <td> </td>
                  <td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <input type="submit" class="btn" value="Delete User" id="delete" name="delete" /><a href="manageusers.php">
                    <input type="submit" class="btnalt" value="Return to last page" id="return" name="return"/>
                  </a></form></td>
                </tr>
              </table>
            </div>
        </div>
       
        
        <!-- Content Box End -->
        
         <!-- Graphs Box Start --><!-- Graphs Box End -->
        
        <!-- Alternative Content Box Start --><!-- Alternative Content Box End -->
  <div style="clear:both;"></div>

        <!-- Content Box Start --><!-- Content Box End -->
      <div id="footer">
       	© Copyright 2011 PS3Panda</div> 
          
    </div>
    <!-- Right Side/Main Content End -->
    
        <!-- Left Dark Bar Start -->
   <?php
   include 'sidebar.php';
   ?>
    <!-- Left Dark Bar End --> 
    
    <!-- Notifications Box/Pop-Up Start --> 
    <div id="notificationsbox">
        <h4>Notifications</h4>
        <ul>
       <?php
   include 'notifypop.php';
   ?>
        </ul>
        <p class="loadmore"><a href="#" title="">Load more notifications</a></p>
    </div>
    <!-- Notifications Box/Pop-Up End --> 
    
    <script type="text/javascript" SRC="http://dwpe.googlecode.com/svn/trunk/_shared/EnhanceJS/enhance.js"></script>	
    <script type='text/javascript' SRC="http://dwpe.googlecode.com/svn/trunk/charting/js/excanvas.js"></script>
<script type='text/javascript' SRC="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type='text/javascript' SRC="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script type='text/javascript' SRC="scripts/jquery.wysiwyg.js"></script>
    <script type='text/javascript' SRC="scripts/visualize.jQuery.js"></script>
    <script type="text/javascript" SRC="scripts/functions.js"></script>
    
<!--[if IE 6]>
    <script type='text/javascript' src='scripts/png_fix.js'></script>
    <script type='text/javascript'>
      DD_belatedPNG.fix('img, .notifycount, .selected');
    </script>
    <![endif]--></body>
</html>

 

The code we're looking at is:

 <?php


if ((isset($_GET['action']))&&($_GET['action'] == 'delete')) {
$user_id = (int) $_GET['id'];
include 'config.php';
mysql_connect(localhost,$username, $password);
@mysql_select_db($database) or die ("Unable to select Database");
$query = mysql_query("SELECT username FROM users WHERE id='$user_id'");
$row = mysql_fetch_assoc($query);
}

If(isset($_POST['delete']))
	{
         $user_id = (int) $_GET['id'];
include 'config.php';
mysql_connect(localhost,$username, $password);
@mysql_select_db($database) or die ("Unable to select Database");
$result = mysql_query("DELETE FROM users WHERE id='$user_id'");
$return = mysql_query($result) or die(mysql_error());
echo $return;
}
?>

 

Any help is most appreciated!

 

Best Wishes,

Ptsface12

Link to comment
Share on other sites

the main issue that I see here is that you are running your delete query twice, which will cause an error since it won't be able to find that id twice..

 

$result = mysql_query("DELETE FROM users WHERE id='$user_id'"); //you run it here
$return = mysql_query($result) or die(mysql_error()); //and again here

 

remove one of these 2 lines..

 

Link to comment
Share on other sites

Does anybody understand this problem?

Please give adequate time for people to answer your question.

 

I believe what is happening is that since you're calling mysql_query twice, the second call is trying to execute a query on a resource id (1).

 

$result = mysql_query("DELETE FROM users WHERE id='$user_id'");

Now $result is the resource id, which in your particular error message is 1.

 

You're calling mysql_query() again on $result, passing in 1:

$return = mysql_query($result) or die(mysql_error());

which is an invalid query.

 

Link to comment
Share on other sites

I typically like to create a pure String for the query, that way you can echo exactly what you're passing in:

$query = "DELETE FROM users WHERE id='$user_id'";
$return = mysql_query($query) or die(mysql_error());

 

I would also take a look at this article to learn how to handle mysql exceptions properly:

http://www.phpfreaks.com/blog/or-die-must-die

Link to comment
Share on other sites

<? 
session_start();
if(!session_is_registered(username)){
header("location:../index.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PS3Panda • Housekeeping • Home</title>
<link href="styles/layout.css" rel="stylesheet" type="text/css" />
<link href="styles/wysiwyg.css" rel="stylesheet" type="text/css" />
<!-- Theme Start -->
<link href="themes/blue/styles.css" rel="stylesheet" type="text/css" />
<!-- Theme End -->

</head>

<?php
include 'config.php';
$user_id = (int) $_GET['id'];
mysql_connect(localhost,$username, $password);
@mysql_select_db($database) or die ("Unable to select Database");
$query = mysql_query("SELECT username FROM users WHERE id='$user_id'");
$row = mysql_fetch_assoc($query);
?>
<body id="homepage">
<div id="header">
    	<a href="" title=""><img SRC="img/cp_logo.png" alt="Control Panel" class="logo" /></a>
    	<div id="searcharea">
            <p class="left smltxt"><a href="#" title="">Advanced</a></p>
            <input type="text" class="searchbox" value="Search control panel..." onclick="if (this.value =='Search-2520control-2520panel..-2E'){this.value=''}"/>
            <input type="submit" value="Search" class="searchbtn" />
        </div>
    </div>
        
    <!-- Top Breadcrumb Start -->
    <div id="breadcrumb">
    	<ul>	
        	<li><img SRC="img/icons/icon_breadcrumb.png" alt="Location" /></li>
        	<li><strong>Location:</strong></li>
            <li><a href="#" title="">Manage Users</a></li>
            <li>/</li>
            <li class="current">Delete <?php echo $row['username']; ?></li>
        </ul>
    </div>
    <!-- Top Breadcrumb End -->
     
    <!-- Right Side/Main Content Start -->
    <div id="rightside">
    
    	<!-- Status Bar Start --><!-- Status Bar End -->
        
         <!-- Red Status Bar Start --><!-- Red Status Bar End -->
        
        <!-- Green Status Bar Start --><!-- Green Status Bar End -->
        
        <!-- Blue Status Bar Start --><!-- Blue Status Bar End -->   
    
        <!-- Content Box Start -->
      <div class="contentcontainer">
            <div class="headings alt">
                <h2>Delete <?php echo $row['username']; ?>?</h2>
            </div>
            <div class="contentbox">
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php




If(isset($_POST['delete']))
	{
         $user_id = (int) $_GET['id'];
include 'config.php';
mysql_connect(localhost,$username, $password);
@mysql_select_db($database) or die ("Unable to select Database");
$newquery = "DELETE FROM users WHERE id='$user_id'";
$return = mysql_query($newquery) or die(mysql_error());
}
?>
<tr>
                  <td width="2%"> </td>
                  <td width="98%">You have decided to delete <?php echo $row['username']; ?>, would you like to delete them from the database?</td>
                </tr>
                <tr>
                  <td> </td>
                  <td> </td>
                </tr>
                <tr>
                  <td> </td>
                  <td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <input type="submit" class="btn" value="Delete User" id="delete" name="delete" /><a href="manageusers.php">
                    <input type="submit" class="btnalt" value="Return to last page" id="return" name="return"/>
                  </a></form></td>
                </tr>

              </table>
            </div>
        </div>
       
        
        <!-- Content Box End -->
        
         <!-- Graphs Box Start --><!-- Graphs Box End -->
        
        <!-- Alternative Content Box Start --><!-- Alternative Content Box End -->
  <div style="clear:both;"></div>

        <!-- Content Box Start --><!-- Content Box End -->
      <div id="footer">
       	© Copyright 2011 PS3Panda</div> 
          
    </div>
    <!-- Right Side/Main Content End -->
    
        <!-- Left Dark Bar Start -->
   <?php
   include 'sidebar.php';
   ?>
    <!-- Left Dark Bar End --> 
    
    <!-- Notifications Box/Pop-Up Start --> 
    <div id="notificationsbox">
        <h4>Notifications</h4>
        <ul>
       <?php
   include 'notifypop.php';
   ?>
        </ul>
        <p class="loadmore"><a href="#" title="">Load more notifications</a></p>
    </div>
    <!-- Notifications Box/Pop-Up End --> 
    
    <script type="text/javascript" SRC="http://dwpe.googlecode.com/svn/trunk/_shared/EnhanceJS/enhance.js"></script>	
    <script type='text/javascript' SRC="http://dwpe.googlecode.com/svn/trunk/charting/js/excanvas.js"></script>
<script type='text/javascript' SRC="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type='text/javascript' SRC="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script type='text/javascript' SRC="scripts/jquery.wysiwyg.js"></script>
    <script type='text/javascript' SRC="scripts/visualize.jQuery.js"></script>
    <script type="text/javascript" SRC="scripts/functions.js"></script>
    
<!--[if IE 6]>
    <script type='text/javascript' src='scripts/png_fix.js'></script>
    <script type='text/javascript'>
      DD_belatedPNG.fix('img, .notifycount, .selected');
    </script>
    <![endif]--></body>
</html>

Link to comment
Share on other sites

where is this $_POST['delete'] coming from? I don't even see a form on the code that you posted...

 

Edit: also you are using session_is_registered..which is deprecated..change to

 

session_start();
if(isset($_SESSION['username'])){
header("location:../index.php");
}

Link to comment
Share on other sites

Hello,

It's slightly below that code

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <input type="submit" class="btn" value="Delete User" id="delete" name="delete" /><a href="manageusers.php">
                    <input type="submit" class="btnalt" value="Return to last page" id="return" name="return"/>
                  </a></form>

Link to comment
Share on other sites

Alright let's clean this up...don't capitalize the I's in your if statements..If should be if, simply for eticate..

Do not use $_SERVER['PHP_SELF']; as your form action, this can lead to XSS injection, you can google it if you want to learn more about it...in this case you will want to make the link a dynamic on with the correct id for what you are trying to do, since in your if conditional block you query depends on $_GET['id'], you will need to specify that in your code...you can make the action="script_name.php?id=$id" instead or something that doesn't involve PATH_INFO.. the code that you have should work if you make those changes..if not let us know...most likely what is happening is your query is actually running, but it isn't grabbing $_GET['id'] since you do not specify it in your form action

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.