根据复选框值从表中显示href [英] Display href from a table based on checkbox values
问题描述
我是PHP的新手。我知道我使用mysql *函数,但我会继续使用它们为这个项目。我正在寻找如何根据插入到我的数据库中的复选框值显示视频的指导。我有14个复选框,其名为1,2,3,4,5等...(插入到eNISATID列)。每个复选框的名称将插入到我的eNISATID(从enisatanswer)列(1-14),以及值为1或0的eNISAT_watch(取决于是否已选中该复选框)。
I am new to PHP. I am aware I am using mysql*functions, but I will continue to use them for this project. I am looking guidance on how to display videos based on checkbox values inserted into my database. I have 14 checkboxes, which are named 1, 2, 3, 4, 5 etc...(which insert into eNISATID column). The name of each checkbox inserts into my 'eNISATID' (from enisatanswer) column (1-14), as well as value of 1 or 0 into 'eNISAT_watch' depending if the checkbox has been selected or not.
我的第一段代码工作正常,并插入到我的数据库。其中如下:
My first piece of code works fine and inserts into my database. Which is as follows:
<?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']; ?> <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(来自enisatquest的PRIMARY KEY,用于唯一标识视频的行)匹配为外键。
我的表格为我的复选框答复具有以下结构:
My table for my checkbox responses has the following structure:
-
eNISATanswersID
INCREMENT) - 'user_id'(登录的用户ID)
-
eNISATID
我想这是我的复选框的名称,1-14) -
eNISAT_watch
(值为1或0,已选择或未选择)
eNISATanswersID
(AUTO-INCREMENT)- 'user_id' (Users id who is logged on)
eNISATID
(I want this to be the name of my checkbox, 1-14)eNISAT_watch
(value of 1 or 0 depending if the checkbox has been selected or not)
我的视频表格结构如下:
My table for videos table has the following stucture:
- 'eNISATID'(主键 - 值1-14)
-'eNISATQuestion'(形成我的href上的文字)
-'eNISATVideo'我的服务器)
-'eNISATID' (Primary Key - values 1-14) -'eNISATQuestion' (Text that forms the wording on my href) -'eNISATVideo' (URL for my video stored on my server)
**当我使用简单的查询来显示我的表格中的所有视频时,它们显示很好,但我希望只显示与该复选框被选中,并取决于登录用户的user_id。我觉得我的SELECT查询是问题,任何人都可以告诉。
** When I use a simple query to display all of the videos from my table, they display fine, but I wish to only dispay the videos relating to the checkbox selected and depending on the user_id of the logged on user. I feel that my SELECT query is the issue, can anyone advise.
这里是我的代码显示我的enisatquestion表基于插入的复选框值的视频。我已经尝试了两个不同的查询,如图所示。一个用//注释掉。 **
Here is my code to display my videos from my enisatquestion table based on the checkbox values inserted. I have tried two different queries as shown. One is commented out with //. **
<?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 WHERE enisatanswer.eNISATID = enisatquestion.eNISATID AND user_id = $userID AND eNISAT_watch = 1";
/* 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']; ?> <a href="home.php?home">Return to Homepage</a> <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>
推荐答案
我得到它的工作,我得到的错误是从我的查询中的SESSION变量。我使用我的初始查询并更新如下:
I got it working, the error I was getting was from the SESSION variable in my query. I used my initial query and updated as follows:
$query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion INNER JOIN enisatanswer WHERE enisatanswer.eNISATID = enisatquestion.eNISATID AND user_id = $userID AND eNISAT_watch = 1";
这篇关于根据复选框值从表中显示href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!