Checkbox PHP

hi all,

Can u teach me, how to create table for multi select checkbox on PHPMYADMIN?
i had problem with my database report and html/php, details as bellow:
1.) i create database as a “test_form” and create table as a “form”

2.) under “form” i create :
a.) id >int(20)>AI
b.)name>varchar(50)>latin1_swedish_ci
c.)access>set(hr,account,purchasing,)>latin1_swedish_ci

3.) i create connect.php code as below:
<

?php
$dsn  = "mysql:dbname=test_form;host=localhost";
$user = "root";
$pass = "";
 
try {
    $dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
    echo "cannot connect to database: ".$e->getMessage();
}
?>

4.) i create input.php code as below:

<form action="save.php" method="post">
        <p>
        Full Name
        <input type="text" name="name" required />
        <p>
            <input type="checkbox" name="access" value="hr" id="hr">Human Resources - HR</label><br />
            <input type="checkbox" name="access" value="account" id="account"><label for="account">Account</label><br />
            <input type="checkbox" name="access" value="purchasing" id="purchasing"/><label for="purchasing">Purchasing</label>
        <p>
            <input type="submit" value="submit" onclick="return confirm('Are You Sure To submit this form?')"/>
</fieldset>

5.) and i create save.php code as bellow:

<?php
include'connect.php';

if (isset($_POST)) {
    $sql = "INSERT INTO form VALUES ('', '$_POST[name]', '$_POST[access]' )";

    $dbh->exec($sql);
    

}

header("location:index.php");
?>

6.) lastly i create index.php code as below:

<?php
include 'connect.php';
?>
    <thead>
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>Access</th>
        </tr>
    </thead>
     
    <tbody>
    <?php
    $sql = "SELECT * FROM form ORDER BY id";
    $no  = 1;
    foreach ($dbh->query($sql) as $data) :
    ?>
        <tr>
            <td><?php echo $no++; ?></td>
            <td><?php echo $data['name'] ?></td>
            <td><?php echo $data['access'] ?></td>
            <td align="center">
            </td>
        </tr>
    <?php
    endforeach;

P/s: the problem is, when i select 2 checkbox or 3…only show 1 select on my PHPMYADMIN.
exm: i select “hr” and "account’ . only ‘account’ will show on my database also my index.php.
Help me and teach me,how to solve this issue…
Thanks.

Hi kamarul, welcome to the forum

I like to start with the database tables and then create the form HTML afterwards.

Please run this query and post the result to make sure we’re at the same place.

SHOW CREATE TABLE form;

Hi Mittineague,

Thanks for feedback,
fyi i’m new and now i just try to create form using html and PHPMYADMIN.
can u teach me how to run that?

hi mittineague,

this is my database and table setting
– phpMyAdmin SQL Dump
– version 4.5.4.1deb2ubuntu2
http://www.phpmyadmin.net

– Host: localhost
– Generation Time: Oct 13, 2016 at 03:38 PM
– Server version: 10.0.27-MariaDB-0ubuntu0.16.04.1
– PHP Version: 7.0.8-0ubuntu0.16.04.3

SET SQL_MODE = “NO_AUTO_VALUE_ON_ZERO”;
SET time_zone = “+00:00”;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/
!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/
!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/
!40101 SET NAMES utf8mb4 */;


– Database: test_form



– Table structure for table form

CREATE TABLE form (
id int(255) NOT NULL,
name varchar(50) NOT NULL,
access set(‘hr’,‘account’,‘purchasing’) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


– Indexes for dumped tables


– Indexes for table form

ALTER TABLE form
ADD PRIMARY KEY (id);


– AUTO_INCREMENT for dumped tables


– AUTO_INCREMENT for table form

ALTER TABLE form
MODIFY id int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=141;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/
!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/
!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

How a table is created is a bit different depending on if you do it from the command line, phpMyAdmin, PHP code or another way, but the query syntax is the same.

http://dev.mysql.com/doc/refman/5.7/en/create-table.html

IMHO creating the table is one of the most important steps and well worth the time spent on planning it.

Important things to decide include

  • a good descriptive name for the table
  • which field(s) will be the index - typically an auto-incrementing id field, but not always.
  • good descriptive field names
  • the appropriate datatype for each field
  • which table options to use

Don’t worry about “partition” yet, save that for later.

When you decide on table and field names, it’s best to not use a reserved word, you’ll be glad later.

http://dev.mysql.com/doc/refman/5.7/en/keywords.html

Did you create the database to hold the table(s) yet?

Web browsers only send values for checkboxes that have been check, so if a checkbox isn’t checked, nothing will be sent so there won’t be a value for the un-checked check box on the $_POST array.

Aha, cross-posted the PM and here and missed seeing the dump here.

Anyway …

To me, checkboxes used for query inserts are usually best for boolean values.
The form table has 3 fields, none of which are boolean in nature

id int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=141 
name varchar(50) NOT NULL
access set('hr','account','purchasing') NOT NULL 

I don’t know what’s up with starting the auto increment at 141 but I’m guessing it has to do with the NO_AUTO_VALUE_ON_ZERO and existing rows

In any case, I’m thinking the id field does not need a form input.

“name” is a varchar, so I would expect the form to have a text input for it

“access” is a set so checkboxes would be an appropriate choice for that field. an empty string if none are checked.
(though I question the choice of using the set datatype)

So the form in the OP looks good to me.

What looks NOT GOOD to me is the INSERT code.


if (isset($_POST)) {
    $sql = "INSERT INTO form 
            VALUES ('', '$_POST[name]', '$_POST[access]' )";

    $dbh->exec($sql);

hi…
for the
id int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=141
name varchar(50) NOT NULL
access set(‘hr’,‘account’,‘purchasing’) NOT NULL

id as a number user done submit that form.
141 cause i’ve many time submit to try the result…
yes,name is text input
so what can i replace to change INSERT code?

Thanks

I would use PDO and write it more like

$stmt = $dbh->prepare("INSERT INTO form (name, access) VALUES (:name, :access)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':access', $access);

http://php.net/manual/en/pdo.prepared-statements.php

where $name and $access have values that are returned from functions that validate and otherwise process the raw POST values.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.