Topic awaiting preservation: PHP/MySQL and deleting rows? (Page 1 of 1) |
|
---|---|
Maniac (V) Inmate From: there...no..there..... |
posted 10-05-2005 22:39
I have a database that will enter info from a form just fine. However, I can't get the editing or deleting part working right. Here is the delete.php script that I am working with just for testing purposes and it's not working. code: <? //connect to mysql $link = mysql_connect("localhost","username","password"); if (!$link) { die('Could not connect: ' . mysql_error()); } //select database mysql_select_db("newsy",$link); //If cmd is not hit if(!isset($cmd)){ //display all the news $result = mysql_query("SELECT * FROM news"); //run the while loop that grabs all the news scripts while($r=mysql_fetch_array($result)){ //grab the title and the ID of the news table $title=$r["title"];//grab the title $id=$r["id"];//grab the id echo "<a href='delete.php?cmd=delete&id=$id'>$title - Delete</a>"; echo "<br>"; } } if($cmd=="delete"){ $sql = "DELETE FROM news WHERE id=$id"; $result = mysql_query($sql,$link); echo "Row deleted!"; } ?>
|
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-05-2005 23:49
What's the ID that's going into the delete command? code: if($cmd=="delete"){ $sql = "DELETE FROM news WHERE id=$id"; echo "SQL: $sql"; // Use this for testing in phpmyadmin $result = mysql_query($sql,$link); if ( !$result ) { echo mysql_errno($link) . ": " . mysql_error($link) . "\n"; } else { echo "Row deleted" }
|
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-05-2005 23:51
Also note SQL errors no longer send warnings in php you need to explicitly echo the error messages |
Maniac (V) Inmate From: there...no..there..... |
posted 10-06-2005 00:00
well I ran this query in phpMyAdmin and it worked fine: DELETE FROM news WHERE id=4 |
Maniac (V) Inmate From: under the bed |
posted 10-06-2005 00:06 |
Bipolar (III) Inmate From: Australia |
posted 10-06-2005 01:20
Only if it's a varchar etc. If its an integer or other number type puting single quotes would cause errors. |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-06-2005 01:59
quote:
|
Bipolar (III) Inmate From: Australia |
posted 10-06-2005 02:43
It doesnt seem to care these days actually, but adding quotes to a number doesnt make a difference. I did some tests and if its type varchar etc, you can put numbers in with or without quotes - however if its a word "testing" it doesnt work unless its quoted. |
Maniac (V) Inmate From: under the bed |
posted 10-06-2005 04:02
Unless I'm misunderstanding (which is certainly possible), the issue is not what the data actually is, it's a matter of it being a variable...isn't it? |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-06-2005 04:31
Not really |
Maniac (V) Inmate From: there...no..there..... |
posted 10-06-2005 15:43
still nothing. I used everyones method that was posted here and nothing happens. code: //connect to database $dbcnx=mysql_connect("localhost","######","######"); //if it doesn't connect, die if(!$dbcnx){ die("Could not connect : " . mysql_error()); } //select database $db=mysql_select_db("newsy",$dbcnx); //make a query to delete the entry $query="DELETE FROM news WHERE id = 8"; //send the query to the database $result=mysql_query($query,$dbcnx); if(!$result){ echo mysql_errno($link) . ": " . mysql_error($link) . "\n"; }else{ echo "Row deleted"; }
|
Maniac (V) Inmate From: under the bed |
posted 10-06-2005 18:14
Looking through the code a little more closely, two things come to mind - |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-06-2005 18:14
That sounds like it could be the problem. If the query is never getting run then you are going to have an issue. |
Maniac (V) Inmate From: there...no..there..... |
posted 10-08-2005 17:30
OK....got that one going. You guys were right. $cmd was never being passed...now it is. Thanks. |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-08-2005 19:39
why are the headers already sent? |
Maniac (V) Inmate From: there...no..there..... |
posted 10-08-2005 21:07
well I just have it as a full page so it has the doc type and everything in it already. Or am I just confused on what that is doing? quote:
|
Maniac (V) Inmate From: under the bed |
posted 10-08-2005 23:36
you'll need to detect what state you are in before outputting anything. code: <?php ob_start(); if (empty($cmd)) { ?> html goes here... <?php } elseif($cmd == "delete") { header(location:...); } ob_end_flush(); ?> |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-09-2005 00:56
Yeah a good habit to get in is to place all of your business logic at the top of your page before outputting any content to the page. |
Bipolar (III) Inmate From: Australia |
posted 10-09-2005 02:57
code: Also another common mistake is that you can't have anything (including a single space) before your first <?php tag.
|