0

i try to solve a problem.

enter image description here

and this is my code.

<?php

require 'connectDB.php';
print<<<_A_
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
_A_;
$mysql = new mysql();
$mysql->connect();

$dbResult = mysql_query("select * from profiles");






echo "<form action='#' method='post'>";

$dbResult = mysql_query("select * from profiles");


while ($info = mysql_fetch_array($dbResult)) {

    if ($info['isPremium'] == 0)
        echo "<input type=checkbox name='check2[]' id='check2' value=".$info['id'].">";
    else
        echo "<input type=checkbox name='check1[]' id='check1' value=".$info['id']." checked>";

    echo $info['profileName'] . "<br />";
}

echo "<p><input type='submit' name='btnPremium' /></p>";
echo "</form>";

If user click to a checkbox i send db a query in order to make it 1.However, when a user uncheck a checkbox i take this error: Undefined index: check2 in C:\xampp\htdocs\googleAnalytics\GAPI\choosePremium.php on line 43 I try to keep unchecks in arrPremium2 :S

if (isset($_POST['btnPremium'])) {

    $arrPremium = $_POST['check1'];
    foreach($arrPremium as $result)
    {
        mysql_query("UPDATE profiles set isPremium=1 where id=".$result."");
    }

    $arrPremium2 = $_POST['check2'];
    print_r($arrPremium2);
}
?>

3 Answers 3

2

Your error looks like it's coming from:

$arrPremium2 = $_POST['check2'];

Because if there are no checkboxes 'checked' that are named 'check2', there will be no element of $_POST there (hence undefined index). Check it exists by doing something like:

if(isset($_POST['check2'])) {
    $arrPremium2 = $_POST['check2'];
    // do something
} else {
    // no check2's, do something else
}

Some bad practices you should get out the habit of:

e.g. checked="checked" is the correct way to mark a checkbox as checked.

And you should really have:

 ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php

instead.

$arrPremium = $_POST['check1'];
    foreach($arrPremium as $result)
    {
        mysql_query("UPDATE profiles set isPremium=1 where id=".$result."");
    }

Don't do this, you make yourself susceptible to MySQL injection. Escape $result before updating your database with it, or use prepared statements.

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

Comments

1

The checkboxes only give you a value if they are checked. Otherwise, there is no value sent from the form. You can either have some defaults set up or use the hidden input trick.

The dafaults are easy:

$foo = isset($_POST['foo']) ? $_POST['foo'] : 0;

This way you get all your values. The thing it, you must know what checkboxes there are.

I'd recommend going for the hidden input. Your HTML, for each checkbox, will look like this:

<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" />

This way, if you check the checkbox, the browser will send foo=1, overwriting the hidden field's value. Otherwise, the checkbox will send no value at all, falling back to the hidden input's foo=0.

Comments

0

First of all, please sanitize your database queries, your current code can be easily exploited. Put mysql_real_escape_string() around $result variable in the mysql_query() call.

Secondly, if user has unchecked the value then it is not sent as a POST variable. Instead you need to check if it exists with isset().

if(isset($_POST['check2'])){ echo 'user checked this box'; }

Always check if checkboxes are set with isset().

1 Comment

if (isset($_POST['check2'])) { $arrPremium = $_POST['check2']; foreach ($arrPremium as $result) { mysql_query("UPDATE profiles set isPremium=1 where id=" . $result . ""); } } else { mysql_query("UPDATE profiles set isPremium=0 where id=" . $result . ""); } else does not works :(

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.