views:

63

answers:

3

Hi All,

I'm probably going to make myself look like a fool with my horrible scripting but here we go.

I have a form that I am collecting a bunch of checkbox info from using a binary method. ON/SET=1 !ISSET=0

Anyway, all seems to be going as planned except for the query bit. When I run the script, it runs through and throws no errors, but it's not doing what I think I am telling it tom which is updating the specified fields within the DB.

I've hard coded the desired values into the query and it DOES update the DB. Relying on the variables I believe I've established and am then calling upon in the query does NOT update the DB.

I've also tried echoing all the needed variables after the script runs and exiting right after so I can audit them... and they're all there. Here's an example.

  ####FEATURES RECORD UPDATE
### HERE I DECIDE TO RUN THE SCRIPT BASED ON WHETHER AN IMAGE BUTTON WAS USED
if (isset($_POST["button_x"])) { 

### HERE I AM ASSIGNING 1 OR 0 TO A VAR BASED ON WHTER THE CHECKBOX WAS SET
if (isset($_POST["pool"])) $pool=1;
if (!isset($_POST["pool"])) $pool=0;
if (isset($_POST["darts"])) $darts=1;
if (!isset($_POST["darts"])) $darts=0;
if (isset($_POST["karaoke"])) $karaoke=1;
if (!isset($_POST["karaoke"])) $karaoke=0;
if (isset($_POST["trivia"])) $trivia=1;
if (!isset($_POST["trivia"])) $trivia=0;
if (isset($_POST["wii"])) $wii=1;
if (!isset($_POST["wii"])) $wii=0;
if (isset($_POST["guitarhero"])) $guitarhero=1;
if (!isset($_POST["guitarhero"])) $guitarhero=0;
if (isset($_POST["megatouch"])) $megatouch=1;
if (!isset($_POST["megatouch"])) $megatouch=0;
if (isset($_POST["arcade"])) $arcade=1;
if (!isset($_POST["arcade"])) $arcade=0;
if (isset($_POST["jukebox"])) $jukebox=1;
if (!isset($_POST["jukebox"])) $jukebox=0;
if (isset($_POST["dancefloor"])) $dancefloor=1;
if (!isset($_POST["dancefloor"])) $dancefloor=0;

### I'VE DONE LOADS OF PERMUTATIONS HERE... HARD SET THE 1/0 VARS AND LEFT THE $estab_id TO BE PICKED UP.  SET THE $estab_id AND LEFT THE COLUMN DATA TO BE PICKED UP.  ALL NO GOOD.  IT _DOES_ WORK IF I HARD SET ALL VARS THOUGH

mysql_query("UPDATE thedatabase SET pool_table='$pool', darts='$darts', karoke='$karaoke', trivia='$trivia', wii='$wii', megatouch='$megatouch', guitar_hero='$guitarhero', arcade_games='$arcade', dancefloor='$dancefloor' WHERE establishment_id='22'");

 ###WEIRD THING HERE IS IF I ECHO THE VARS AT THIS POINT AND THEN EXIT(); they all show up as intended. 

header("location:theadminfilething.php");
exit();

THANKS ALL!!!

+4  A: 

I recommend you to use something like:

$fields = array('pool', 'darts', 'karaoke', 'trivia', ...);
foreach ( $fields as $field ) {
    $$field = isset($_POST[$field]) ? 1 : 0;
}

instead of 20 lines of ifs.

Your columns are ENUM or int type ? If int - drop apostrophes.

hsz
whats the double $ do ?
mcgrailm
@mcgrailm: the double $ is what is called a variable variable. It addresses or creates a variable with the name contained in $field. See: http://php.net/manual/en/language.variables.variable.php
fireeyedboy
It creates new variable from string. For example: `$a = 'b'; $$a = 5;` it assigns `5` to `$b`.
hsz
pretty cool thanks for that
mcgrailm
+3  A: 

Your code could really use some error checking. Make sure you have activated the displaying of errors in your script.

In your testing environment add this at the top of your main script for instance (if you haven't done something equivalent already):

error_reporting( E_ALL | E_STRICT );
ini_set( 'display_errors', 1 );

Then (although not dependant on the above) make sure you probe the result of the query with something like:

if( false === mysql_query( 'UPDATE ...etc' ) )
{
   echo 'query failed with error:' . mysql_error();
}

My guess is it will fail with the error that your column name karaoke is mispelled. But there may be more errors.

Also, hsz' suggestions are spot on (though probably not the root of your problem). Makes for easier to maintain code, and significantly reduces code.

fireeyedboy
OMG... thanks for the new set of eyes. I didn't realize that i misspelled it in the colum reference. Well, that solved it. I appreciate EVERYONE's HELP and ADVICE on this. I get better at this from every question I ask on this site. THANK YOU X 100000
rob - not a robber
Glad i could be of help rob. But please consider the activation of error reporting and displaying (the latter only in test environment, not in production) and error checking (!) as your new set of eyes. ;-) Happy coding!
fireeyedboy
Should I delete this question since it boiled down to me having bad proofreading skills and isn't really a tactical type matter?
rob - not a robber
Hmmm, I think it would result in me losing my reputation points. So, I'ld rather you wouldn't. :) Also, for future reference, the advice to start using error detection is valuable for other people experiencing these kinds of problems.
fireeyedboy
OK, not prob. I use notepad++ for all my editing and I like the syntactital highlighting but I wish it were more powerful... Like if I could highlight all variables or whatever in a certain color, that would make typos very easy to discover. Do you know anything that does that? THANKS
rob - not a robber
I use notepad++ my self too actually. You can alter the coloring scheme in notepad++. There are IDE's with much more advanced capabilities out there also (Eclipse, Zend Studio, to name a few). But, I'm not sure how advanced such IDE's are with highlighting inline SQL syntax for instance. Also, I usually consider them a bit bloated. But really, if you take my advice about error reporting and error checking at heart (really a must), you usually get warned about these things very early.
fireeyedboy
cool... lesson learned. thank you
rob - not a robber
+1  A: 

Firstly, construct the sql query string in a variable and then pass it to mysql_query(), comment out the header() line and print out the query for debugging. For example:

...
$sql="UPDATE thedatabase SET pool_table='$pool', darts='$darts', karoke='$karaoke', trivia='$trivia', wii='$wii', megatouch='$megatouch', guitar_hero='$guitarhero', arcade_games='$arcade', dancefloor='$dancefloor' WHERE establishment_id='22'";
print("$sql");
mysql_query($sql);
//header("location:theadminfilething.php");
exit();
...

Secondly, even tho you are exiting the script, its good practice to always match your braces. You are missing the end brace for the if statement at the end of your code.

The value of the $sql variable output you can see if it works by executing it 'manually' thru phpmyadmin or the command line. What happens?

zaf
thanks Zaf, I need to get into the habit of providing better examples. I do in fact have both curlies in my script.
rob - not a robber
No worries. Let us know what mysql says when you 'manually' send the query.
zaf
Stopped with #1054 - Unknown column 'karoke' in 'field list' Due to my TYPO ;)
rob - not a robber
Meh. I only get 10 peanuts.
zaf