sermed
sermed

Reputation:

save check box name and value into a table

please i need come help i have a form of check boxes i want to save the name and value of the boxes that registered user selects into a table ,now i have registered users information saved in a table ,how the table that saves the registered users selection is to be created to have a relation with the table of users????

my registered users table

<?php
$connect=mysql_connect("localhost","root") or die (mysql_error());
mysql_select_db("login") or  die (mysql_error());

$query="CREATE TABLE users(
    userid        int not null  auto_increment primary key,
    username      varchar(20), 
    password      varchar(40),
    usertype      varchar(20),
    firstname     varchar(30),
    lastname      varchar(30),
    street        varchar(50),
    city          varchar(50),
    country       varchar(50),          
    postcode      varchar(10),
    gender        varchar(6),
    dateofbirth   date,
    telnumber     varchar(50),           
    email         varchar(50),
    insertdate    date,
    vals          text,
    comment       text
)"; 

$result=mysql_query($query,$connect) or die('Cannot Create User');

mysql_close();
print " DataBase created well" ;
?>

Upvotes: 1

Views: 916

Answers (2)

Josef S&#225;bl
Josef S&#225;bl

Reputation: 7742

Let's say you have table of users like this:

**users**
------------------------------
userid  username etc...
1       billy    ...
2       joe      ...

Now you have to create table like this:

**users_checkboxes**
------------------------------
id  user_id  name              value
1   1        newsletter        1
2   1        send me spam      0
3   2        newsletter        0
4   2        send me spam      0

This information means that billy chosen to receive newsletter and no spam while joe does not want to receive anything.

The key is to have userid from the first table here, this is called foreign key. You can read more about it at Wikipedia.

Upvotes: 3

Ross
Ross

Reputation: 46997

You might like to reword your question so it's easier to read, here's what I think you meant:

How can I update user's data in the database using a form of checkboxes?

Checkboxes are inputs and will come up like text inputs in your GET or POST array (you're more likely to use POST for this).

E.g.:

<form action="update.php" method="post">
    <p><input type="checkbox" name="showDate" /> Show the date on every page.</p>
    <p><input type="submit" value="Update user record" /></p>
</form>

Then in your PHP you would use something a little like this:

<?php
    // Check form has been submitted         
    if (isset($_POST['showDate'])) {
        // Assuming the user is updating their own profile and their 
        // user id is registered in a session
        $update = 'UPDATE users SET showDate = %d WHERE userid = %d';
        // It's better to use prepared statements for this, see http://php.net/mysqli
        mysql_query(sprintf($update, $_POST['showDate'], $_SESSION['userid']));
    } else {
        // show the form
        echo $form;
    }

Hope this makes sense (and is what you were looking for) - your question was unclear to me.

Upvotes: 1

Related Questions