Jump to content

deleting a row from a database


woodsonoversoul

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

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.