Jump to content

Recommended Posts

Hello. Small problem I need some help with. I can't fiqure out why my delete function isn't working. I have a listing of rows in my database with an option to delete an individual row at the end:

 

echo "<div id=\"navcontainer\">";
    echo "<ul>";
            echo "<li><a href=#>".$row[2]."</li>";
    echo "<li><a href=#>".$row[3]."</li>";
    echo "<li><a href=#>$".$row[4]."</li>";
            echo "<li><a href=".$_SERVER['PHP_SELF']."?id=".$row[1].">x</a></li>";
            echo "</ul>";
    echo "</div>";

 

The page start up checking to see if an id has been specified (I belive this is where the proble is):

 

// if id provided, then delete that record
if (isset($_GET['id'])) {
    // create query to delete record
    $query = "DELETE FROM purchase WHERE sale_id = ".$_GET['id'];

 

And the content of my database looks like this:

 

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| user_name | varchar(11) | NO   |     |         |                | 
| sale_id   | int(11)     | NO   | PRI | NULL    | auto_increment | 
| category  | varchar(11) | YES  |     | NULL    |                | 
| location  | varchar(11) | YES  |     | NULL    |                | 
| price     | float(7,2)  | YES  |     | NULL    |                | 
+-----------+-------------+------+-----+---------+----------------+

 

Where did I go wrong? All help is greatly appreciated

 

 

Link to comment
https://forums.phpfreaks.com/topic/63804-deleting-a-row-from-a-database/
Share on other sites

You have to run $query through mysql_query in order for the query to execute. PHP wont execute queries with out mysql_query

 

// if id provided, then delete that record
if (isset($_GET['id'])) {
    // create query to delete record
    $query = "DELETE FROM purchase WHERE sale_id = ".$_GET['id'];

    mysql_query($query);

awesome. That got the row deleted but, even though the row disapeared on the page, when I delete a row I get access denied and a link to the server could not be established warnings. How does that work if I just obviously connected to my database to preform those operations? Should I reconnect somewhere?

 

 

Before doing a mysql error I get:

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/spindex/display.php on line 31

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/spindex/display.php on line 31

 

line 31 is:

 

// run the query
mysql_query($query);

 

If you could give me any more instruction on how to form a die(mysql_error()) I'll do that(still a novice). I'm assuming it involves if statments and I'm not sure what I should be testing in those statments...

you're totally right. I logged in through terminal and deleted the row, using the exact same code as the code on the site, with no errors, no problems. However when I tried to grant myself (the root user, I still don't understand why I don't have complete privleges) total privleges I used the code:

 

GRANT ALL ON spindex.* TO root @ localhost;

 

and recieved:

 

ERROR 1064 (42000): 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 'localhost' at line 1

Bump - 'cause I'm still having trouble with this:

 

the row deleted but, even though the row disapeared on the page, when I delete a row I get access denied and a link to the server could not be established warnings. How does that work if I just obviously connected to my database to preform those operations?

 

All assistance is appreciated

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.