views:

1432

answers:

5

I'm new to PHP, and I am creating a basic CMS using PHP and MySQL. I'm struggling to get the checkbox information from my HTML page across into the database.

How can I make the values to appear as binary 0 or 1 values?

The HTML document is written as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
    <head>
        <title>Create your news page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
        <fieldset>
            <legend>Checked components will show in the page</legend>
            <form method="POST" action="http://*********.php"&gt;
                <span class="label">Header</span>
                <input type="checkbox" name="header" value="HEADER">
                <br>
                <span class="label">Footer</span>
                <input type="checkbox" name="footer" value="FOOTER">

                <hr>
                <span class="label">Local news</span>
                <input type="checkbox" name="local" value="LOCALNEWS">
                <br>
                <span class="label">National news</span>
                <input type="checkbox" name="national" value="NATIONALNEWS">
                <br>
                <span class="label">International news</span>
                <input type="checkbox" name="international" value="INTERNATIONALNEWS">
                <p>
                <input type="submit">
            </form>
        </fieldset>
    </body>
</html>

And the PHP document is written as follows:

<?php
    $user="user_***";
    $password="*********";
    $database="dbxyz";
    mysql_connect("localhost", $user, $password);
    mysql_select_db($database, $db_handle);
    mysql_select_db("dbxyz");
    if(isset($_POST['layout']))
    {
        foreach($_POST['layout'] as $value {
            $insert="INSERT INTO layout (header, footer, local, national, international) VALUES ('$value')";
            mysql_query($insert);
        }
    }
?>
A: 

I think you're looking for the MySQL 'BOOL'-datatype...

Edit: Your code is actually loaded with syntax/code errors... I don't believe this actually runs. Can you paste the original here?

JorenB
A: 

Make the field which will store the values in your table TINYINT(1), check the status of the checkbox upon submit by comparing $_POST['nameOfCheckboxField'] with 'on', if it evaluates to true, put 1 in your db field, otherwise put 0.

code_burgar
+1  A: 

You are supposed to put all the values in the query at once.
It can be done like that:

$values = array(
    'header'        => null,
    'footer'        => null,
    'local'         => null,
    'national'      => null,
    'international' => null
);

foreach (array_intersect_key($_POST, $values) as $key => $value) {
    $values[$key] = mysql_real_escape_string($value);
}

mysql_query("INSERT INTO layout (header, footer, local, national, international) VALUES ('$values[header]', '$values[footer]', '$values[local]', '$values[national]', '$values[international]')");

Also, notice the mysql_real_escape_string call. It escapes all the characters of its parameter that can be used for SQL injections.

Ignas R
And yes, like the previous posters have said, you should convert the checkbox values to 1 or 0 and then save in a tinyint field.
Ignas R
A: 

If checkboxes aren't checked in a form, then the key/value pair isn't sent when the form is submitted, thus you need to check if each is set. So:

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

This variable will be 1 if the 'header' box was checked, else it will be 0.

$header        = isset($_POST['header']) ? 1 : 0;
$footer        = isset($_POST['footer']) ? 1 : 0;
$local         = isset($_POST['local']) ? 1 : 0;
$national      = isset($_POST['national']) ? 1 : 0;
$international = isset($_POST['international']) ? 1 : 0;

$insert = "INSERT INTO layout (header, footer, local, national, international)
           VALUES ($header, $footer, $local, $national, $international)";

mysql_query($insert);
A: 

I have a similar query.

Can I ask, what would I need to type into the mysql database to store this data into my table?

Daniel