php - 将多个复选框插入表中的两列中

标签 php mysql sql checkbox

更新 - 我已经更新了我的问题并添加了我的代码。我知道 mysql* 函数在 PHP 版本 7 中已被弃用,我正在运行版本 5.5。我希望继续使用这些功能。我的代码目前将 14 个复选框插入到每个复选框的单独列中(不包括存储登录用户 ID 的 userID)。我希望能够将复选框的名称插入到我的 eNISATID 列中,将复选框的值(1 或 0)插入到我的 eNISAT_watch 列中,以及 session 中的用户 ID。

我是 PHP 新手。我正在寻找有关如何将每个复选框的两个值插入表中单独行的指南。我有 14 个复选框,我希望将其命名为 1、2、3、4、5 等...(将插入到 eNISATID 列中)。我希望将复选框的每个名称插入“eNISATID”列 (1-14),并将 1 或 0 值插入“eNISAT_watch”,具体取决于是否选中该复选框。

我的表具有以下结构:

  • eNISATanswersID(自动递增)
  • 'user_id'(登录的用户 ID)
  • eNISATID(我希望这是我的复选框的名称,1-14)
  • eNISAT_watch(值或 1 或 0,具体取决于是否选中该复选框)

任何人都可以帮忙解决这个问题吗?

谢谢

<?php  
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

if(isset($_POST['submit']))
{  
header("Location: eNISATVids.php");

    $userID=$_SESSION['user'];
    $cb_names = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14');
foreach ($cb_names as $cb) {
    $cb_val = isset($_POST['$cb']) ? 1 : 0;
    $sql = "INSERT INTO enisatanswer (user_id, eNISATID, eNISAT_watch) VALUES ('$userID', '$cb', $cb_val)";
    mysql_query($sql) or die(mysql_error());
}
if($query==true)
   {  
      echo'<script>alert("Your choices have inserted Successfully \n \n Please click on Display eNISAT Tutorials at the buttom of the page to view your videos ")</script>';  
   }  
else  
   {  
      echo'<script>alert("Failed To Insert")</script>';  
   }  
}  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
/<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
</div>
<br>
<p align="center"><img src="title.jpeg" width="400"height="100" alt="title.jpeg">
<br>
<br>
<center>
<h2>Please select the tasks you require assistance with, before clicking DISPLAY ENISAT TUTORIALS:<h2>
<br>
<table align="center" height="0" width="70%" border="1" bgcolor = "white">
   <form  action="" method="post"

   <tr> 
      <td colspan="2">Tick each relevant box:</td>  
   </tr>  
   <tr>  
      <td>How to login</td>  
      <td><input type="checkbox" name="1" value="1"></td>   
   <tr>  
      <td>How to manage your worktray</td>  
      <td><input type="checkbox" name="2" value="1"></td>  
   <tr>  
      <td>How to change your visual settings (Colours and text size)</td>  
      <td><input type="checkbox" name="3" value="1"></td> 
   </tr>  
   <tr>  
      <td>How to change your own password on the system</td>  
      <td><input type="checkbox" name="4" value="1"></td>  
   </tr>  
   <tr>  
      <td>How to logout of the system</td>  
      <td><input type="checkbox" name="5" value="1"></td> 
   </tr>  
   <tr>  
      <td>How to search for a client on the system</td>  
      <td><input type="checkbox" name="6" value="1"></td> 
   </tr>  
   <tr> 
      <td>How to start an assessment</td>  
      <td><input type="checkbox" name="7" value="1"></td> 
   </tr>  
   <tr>
      <td>How to finalise an assessment</td>  
      <td><input type="checkbox" name="8" value="1"></td>  
   <tr>  
      <td>How to print an assessment</td>  
      <td><input type="checkbox" name="9" value="1"></td>  
   </tr>  
   <tr>  
      <td>How to create a client and referral manually through Find on H+C</td>  
      <td><input type="checkbox" name="10" value="1"></td>  
   </tr>  
   <tr>  
      <td>How to submit a referral from LCID (LCID Users only)</td>  
      <td><input type="checkbox" name="11" value="1"></td> 
   </tr>  
   <tr>  
      <td>How to submit a referral from Soscare (Soscare Users only)</td>  
      <td><input type="checkbox" name="12" value="1"></td> 
   </tr>  
   <tr>  
      <td>How to reassign a referral on eNISAT</td>  
      <td><input type="checkbox" name="13" value="1"></td> 
   </tr>  
   <tr>  
      <td>How to close a referral on eNISAT</td>  
      <td><input type="checkbox" name="14" value="1"></td>
   </tr> 
   <tr>  
      <td  <td><button name="submit" type="submit" onclick="window.location.href='eNISATVids.php'">Display eNISAT Tutorials</button></td>
</tr> 
</table>  
</div>  
</form>  
</body>  
</html>

更新 - 谁能帮我解决我的第二个查询。我已将 eNISATID(来自 enisatanswer 的复选框名称)与 eNISATID(来自 enisatquestion 的主键,用于唯一标识视频行)作为外键。

这是我的代码,用于根据插入的复选框值显示 enisatquestion 表中的视频。我尝试了两个不同的查询,如图所示。其中一个用//注释掉。

<?php  

    session_start();
    include_once 'dbconnect.php';

    if( !isset( $_SESSION['user'] ) ) header("Location: index.php");

    $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
    $userRow=mysql_fetch_array( $res );

    $userID=$_SESSION['user'];

    //$query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion INNER JOIN enisatanswer ON eNISATID = eNISATID WHERE user_id = $_SESSION['user'] AND eNISAT_watch = 1";
    $query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion, enisatanswer.eNISATID = enisatquestion.eNISATID AND user_id = $userID";
    $result = mysql_query( $query );
    /* A default message if the query fails or there are no records */
    $enisatquestion='<h2>Sorry, there are no records</h2>';


    if( $result ) {/* if there is a recordset, proceed and generate html table */
        $enisatquestion = "<table >";
        while ( $row = mysql_fetch_assoc($result) ) {
            $enisatquestion .= "<tr><td><a href='{$row['eNISATVideo']}'>{$row['eNISATQuestion']}</a></td></tr>";
        }
        $enisatquestion .= "</table>";    
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="home.php?home">Return to Homepage</a>&nbsp;&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
<p align="center"><img src="title.jpeg" width="400"height="100" alt="title.jpeg">
<br>
<br>

    <center>
   <h2>Click on the each link to open your tutorial in Windows Media Player<h2>
   <br>
    <?php
        /* output the html table here, below your header */
        echo $enisatquestion;
        /*
            If the query failed then the default gets displayed
        */
    ?>  
</div> 
</body>  
</html>

最佳答案

使用包含所有复选框名称的数组。循环遍历名称,测试对应的复选框是否提交(仅提交选中的复选框)。

$cb_names = array('Log', 'Worktray', 'Visual', ...);
foreach ($cb_names as $cb) {
    $cb_val = isset($_POST[$cb]) ? 1 : 0;
    $sql = "INSERT INTO tableName (user_id, eNISATID, eNISAT_watch)
            VALUES ('$userID', '$cb', $cb_val)";
    mysql_query($sql) or die(mysql_error());
}

关于php - 将多个复选框插入表中的两列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141038/

相关文章:

php - 将隐藏元数据添加到产品 Woocommerce

PHP 循环 : Bootstrap Slider Add div with item class every 3 item and the first one will have active

php - php中的URL混淆器

php - Moodle 启动 URL 中的网址而不是 ip 地址或本地主机

php - 从 post 到单个 mysql 行的数组

PHP MySQL内容保存到按id排序的数组

mysql - 如果mysql查询结果为空,我如何将默认值设置为0

mysql - sql查询实现二合一

mysql - 这个 SQL 语法有什么错误?

php - 使用 PHP 和 SQL 的购物车