2

My problem is as follows. This is my mySQL table, and HTML form. I need to findout a PHP code to insert the checkbox data to mySQL table.

If a checkbox is checked I want to fill that particular column as "1" else fill it as "0".

A single row is added in one time.

I am using 2 PHP files.

1) regForm.php

<html>

<form method="get" action="regCode.php">

ID: <input type="text" name="id"/></br>

Select Available dates: </br>
Monday <input type="checkbox" value="monday"></br>
Tuesday <input type="checkbox" value="tuesday"></br>
Wednesday <input type="checkbox" value="wednesday"></br>
Thursday <input type="checkbox" value="thursday"></br>
Friday <input type="checkbox" value="friday"></br>
<input type="submit" value="Submit"/><br></br>

</html>

2) regCode.php

<html>

<body bgcolor="#FFFCC">
<?php
$dbhost='localhost';
$dbuser='username';
$dbpass='password';
$conn=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$conn)
{
    die('could not connect'.mysql_error());
}


$sql="INSERT INTO available_days (monday, tuesday, wednesday, thursday, friday) VALUES
()";



mysql_select_db('testdb');
$retval=mysql_query($sql,$conn);

if(!$retval)
{
    die('could not add data'.mysql_error());
}

$message="Successfully Added ID No: ".mysql_insert_id();
echo "<script type='text/javascript'>alert('$message'); window.location.href='regForm.php';</script>";

mysql_close($conn);
?>
</body>
</html>
5
  • 3
    Without any attempt at coding it this question is too broad. SO is not a code writing service, so you need to show some attempt at solving the problem and then ask when you have a specific issue with it. Commented Nov 24, 2016 at 4:34
  • Just use isset(). It returns a boolean, but if cast to an integer it will give your 1 or 0. $monday = (int)isset($_POST['monday']) - now you just need to insert it ;-) Commented Nov 24, 2016 at 4:49
  • @SamiKuhmonen, I edited and put my code there. Commented Nov 24, 2016 at 5:09
  • @Qirel, I've edited my question and added my code. Can you tell me something to do with that code? Thanks in advance Commented Nov 24, 2016 at 5:10
  • Look on answer section Commented Nov 24, 2016 at 6:09

2 Answers 2

2
    # Please Try again below code. #  

      <?php 
    if($_SERVER['HTTP_HOST'] == 'localhost:8080') {
        $servername = "localhost";
        $username = "root";
        $password = "";
        $db = 'test';
    }

    $conn = mysql_connect($servername, $username, $password);
    if ($conn) {
        mysql_select_db($db, $conn) or die('Could not select database.');
    }

    if(isset($_POST['submit'])){
        $monday = isset($_POST['day_name']['monday']) ? $_POST['day_name']['monday'] : 0;
        $tuesday = isset($_POST['day_name']['tuesday']) ? $_POST['day_name']['tuesday'] : 0;
        $wednesday = isset($_POST['day_name']['wednesday']) ? $_POST['day_name']['wednesday'] : 0;
        $thursday = isset($_POST['day_name']['thursday']) ? $_POST['day_name']['thursday'] : 0;
        $friday = isset($_POST['day_name']['friday']) ? $_POST['day_name']['friday'] : 0;

        //echo "INSERT INTO `available_day` (`monday`, `tuesday`, `wednesday`, `thursday`, `friday`) VALUES ('".$monday."', '".$tuesday."', '".$wednesday."', '".$thursday."', '".$friday."')"; 

        $sql_add = mysql_query("INSERT INTO `available_day` (`monday`, `tuesday`, `wednesday`, `thursday`, `friday`) VALUES ('".$monday."', '".$tuesday."', '".$wednesday."', '".$thursday."', '".$friday."')");

        if(!$sql_add)
        {
            die('could not add data'.mysql_error());
        }

        $message="Successfully Added ID No: ".mysql_insert_id();
        echo "<script type='text/javascript'>alert('$message');   window.location.href='index6.php';</script>";

    }

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact_form" enctype='multipart/form-data'>
  <div class="form-group" >
       <input type="checkbox" name="day_name[monday]" value="1">Mon<br>
       <input type="checkbox" name="day_name[tuesday]" value="1"> Tues<br>
       <input type="checkbox" name="day_name[wednesday]" value="1"> Wed<br>
       <input type="checkbox" name="day_name[thursday]" value="1"> Thu<br>
       <input type="checkbox" name="day_name[friday]" value="1"> Fri<br>

    <input name="submit" type ="submit" class="btn btn-default" style="border-radius: 0px; background-color: midnightblue; box-shadow: 2px 2px 2px #888888; color: white;">
  </div> 

</form>
Sign up to request clarification or add additional context in comments.

1 Comment

method="get" in the form, but $_POST in PHP, doesn't quite work that way. Besides, that indexing doesn't match the names of the HTML form provided.
1

1) regForm.php

<form method="post" action="regCode.php">
ID: <input type="text" name="id"/></br>

Select Available dates: </br>
Monday <input type="checkbox" value="1" name='monday'></br>
Tuesday <input type="checkbox" value="1" name='tuesday'></br>
Wednesday <input type="checkbox" value="1" name='wednesday'></br>
Thursday <input type="checkbox" value="1" name='thursday'></br>
Friday <input type="checkbox" value="1" name='friday'></br>
<input type="submit" value="Submit"/><br></br>
</html>

2) regCode.php

<html>

<body bgcolor="#FFFCC">
<?php
$dbhost='localhost';
$dbuser='username';
$dbpass='password';
$conn=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$conn)
{
    die('could not connect'.mysql_error());
}

if(isset($_POST['submit'])){
   $monday = isset($_POST['monday']) ?  $_POST['monday'] : 0;
   $tuesday = isset($_POST['tuesday']) ? $_POST['tuesday'] : 0;
   $wednesday = isset($_POST['wednesday']) ? $_POST['wednesday'] : 0;
   $thursday = isset($_POST['thursday']) ? $_POST['thursday'] : 0;
   $friday = isset($_POST['friday']) ? $_POST['friday'] : 0;

   $sql="INSERT INTO available_days (monday, tuesday, wednesday, thursday, friday) VALUES ('".$monday."', '".$tuesday."', '".$wednesday."', '".$thursday."', '".$friday."')";

  mysql_select_db('testdb');
  $retval=mysql_query($sql,$conn);

  if(!$retval)
  {
    die('could not add data'.mysql_error());
  }
  $message="Successfully Added ID No: ".mysql_insert_id();
  echo "<script type='text/javascript'> alert('$message') window.location.href='regForm.php';</script>";
  mysql_close($conn);
  }
  ?>
</body>

If the db columns are INT please replace the with following code

 $monday = isset($_POST['monday']) ?  1 : 0;
 $tuesday = isset($_POST['tuesday']) ? 1 : 0;
 $wednesday = isset($_POST['wednesday']) ? 1 : 0;
 $thursday = isset($_POST['thursday']) ? 1 : 0;
 $friday = isset($_POST['friday']) ? 1 : 0;

And query like this

$sql="INSERT INTO available_days (monday, tuesday, wednesday, thursday, friday) VALUES ($monday, $tuesday, $wednesday, $thursday, $friday)";

5 Comments

It adds all "0" to the database whether I check the check boxes or not. Can you help me fixing it please?
method="get" in your form, but $_POST['monday'] in PHP - that's why.
@NSJ the method was 'get' thats why its added all 0, please change method = 'post'
Hi @Qirel, still it adds "0" for all columns despite checking or not the check boxes. :(
@NSJ if the db columns are INT change code accordingly in the answer.

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.