4

I am trying to be lazy (or smart): I have 7 checkboxes which correlate with 7 columns in a MySQL table.

The checkboxes are posted in an array:

$can = $_POST['can'];

I've created the following loop to dump the variables for the MySQL insert:

for($i=1;$i<8;$i++){
    if($can[$i] == "on"){
        ${"jto_can".$i} = 'Y';
    }
    else{
        ${"jto_can".$i} = 'N';
    }
}
print_r($jto_can1.$jto_can2.$jto_can3.$jto_can4.$jto_can5.$jto_can6.$jto_can7);

This correctly outputs:

YYNYYYY

However, when I attempt to use those variables in my MySQL update, it doesn't accept the changes.

mysqli_query($db, "UPDATE jto SET jto_can1 = '$jto_can1', jto_can2 = '$jto_can2', jto_can3 = '$jto_can3', jto_can4 = '$jto_can4', jto_can5 = '$jto_can5', jto_can6 = '$jto_can6', jto_can7 = '$jto_can7' WHERE jto_id = '$id'")or die(mysqli_error($db));

Can anyone explain why the print_r displays the variables whereas MySQL update does not?

12
  • 1
    Stick with the array, there's no point or benefit in creating all of those variables. Commented Feb 25, 2013 at 18:09
  • Also, you're missing a space in between the "WHERE" here: jto_can7 = '$jto_can7'WHERE jto_id = '$id' Commented Feb 25, 2013 at 18:09
  • How should I go about loading the array into MySQL then? Commented Feb 25, 2013 at 18:10
  • 2
    You'd be better off making your update statement in the loop you have, and appending the values to the end. Have an array of the column names, and then those values, and going from there. Commented Feb 25, 2013 at 18:13
  • 1
    @thebarless can you echo the query to know what is printing... Commented Feb 25, 2013 at 18:37

2 Answers 2

2

Stick with the array, and form the query dynamically:

$sql = 'UPDATE jto SET ';

$cols = array();
foreach( range( 1, 7) as $i) {
    $value = $_POST['can'][$i] == 'on' ? 'Y' : 'N'; // Error check here, $_POST['can'] might not exist or be an array
    $cols[] = 'jto_can' . $i . ' = "' . $value . '"'; 
}

$sql .= implode( ', ', $cols) . ' WHERE jto_id = "' . $id . '"';

Now do a var_dump( $sql); to see your new SQL statement.

Sign up to request clarification or add additional context in comments.

Comments

1

this is not a mysql problem. mysql will only see what you put into that string. e.g. dump out the query string BEFORE you do mysql_query. I'm guessing you're doing this query somewhere else and have run into scoping problems. And yes, this is lazy. No it's not "smart". you're just making MORE work for yourself. What's wrong with doing

INSERT ... VALUES jto_can1=$can[0], jto_can2=$can[1], etc...

1 Comment

The only difficulty I have with this method is that I'm not seeing how to control that $can[0] get's a "N" value when the box is unchecked. The array will only pass the checked ones (unless I'm missing something).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.