PDA

View Full Version : PHP/MYSQL Simple query


notaflasher
09-15-2011, 06:49 AM
$query = "DELETE FROM table WHERE index = '34'";
does NOT delete the row...index is an int and the primary key

$query = "DELETE FROM table WHERE key = '34'";
does NOT delete the row...key is an int with no attributes

$query = "DELETE FROM table WHERE type = '2'";
DOES delete the row...type is an int with no attributes

$query = "DELETE FROM table WHERE firstname = 'John'";
DOES delete the row...firstname is text with no attributes

I need to reference the primary key (index) in order to update records in the appropriate row. I created "key" and populated it manually with the values of "index" just to test and see if I could delete the row by referencing it instead of the primary key.

Any ideas as to why I am having problems referencing "index" and "key" but not "firstname" and "type"?

Thanks in advance

audiopro
09-15-2011, 02:31 PM
if index is an INT, it should be:-
$query = "DELETE FROM table WHERE index = 34";

notaflasher
09-15-2011, 11:40 PM
if index is an INT, it should be:-
$query = "DELETE FROM table WHERE index = 34";

I've seen it done both ways, with and without the single quotes. But if what you say is a must, then how does the query with "type" work seeing as how type is also an int?

Look again at all my examples. "index", "key", and "type" are all int, "index" being slightly different as a primary key and auto increment. "key" and "type" are indentical, save their respective names and record values. And yet "index" and "key" will not work (with or without the single quotes around the number) and "type" will.

I didn't go into much detail before, as I believed the examples were sufficient to paint the correct picture. But the live .php code won't have a static number as a primary key reference. And it won't even be deleting a row. I'm just using these examples because they are the most straight forward.

The live .php file will be updating existing records with the data coming from actionscript. Actionscript will also be passing the index/primary key to .php in order to update the correct set of values.

My point in all this is that I can neither update nor delete the appropriate records as I seem to have no way to pass the primary key value from php into mysql. I have no problems retrieving information from the database, and as my examples show, I have no problems deleting rows based on other records. I am just not able to update or delete based on the primary key.

audiopro
09-16-2011, 07:13 AM
Does the query work if you run it in PHP directly?
DELETE FROM table WHERE index = 34

If you are sending dynamic vars to a PHP script, print the whole query to a log file and see what the vars in the query actually are. It is very easy to change the value, add a line feed, add a space etc. during the transfer process.

peptobismol
09-22-2011, 06:36 PM
make sure what flash is sending into the php is correct... You never know... echo the passed variable. Audio pro is correct, keep the data type consistent. Anything in quotes is a string so you wouldn't want '34', you'd want 34. It will work either way though but to be correct you should use the quotes properly.

Since you didn't show any code, I'm guessing this...

don't do this

$query = "DELETE FROM table WHERE type = $myvar";


do this


$query = "DELETE FROM table WHERE type =" . $myvar;

notaflasher
09-23-2011, 04:07 AM
Thanks for the responses. After much lamenting, I actually remembered something I read a long time ago. There is an issue with using "index" as a column title. Changing the column title from "index" to "id" immediately solved the problem. While using "index" as the column title, even running the php directly didn't work.