将复选框数据插入mysql

时间:2016-11-24 04:30:56

标签: php html mysql

我的问题如下。 This is my mySQL table, and HTML form。我需要找到一个PHP代码来将复选框数据插入到mySQL表中。

如果选中了复选框,我想将该特定列填充为" 1"否则填写为" 0"。

一次添加一行。

我正在使用2个PHP文件。

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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

如果db列是INT,请替换为以下代码

 $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;

并像这样查询

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