开发者

Variables are "lost" somewhere, then reappear... all with no error thrown

开发者 https://www.devze.com 2022-12-27 06:38 出处:网络
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

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!!!


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.


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号