如何插入user_from SESSION以及多个复选框

时间:2016-01-21 15:35:16

标签: php mysql checkbox userid

我已经更新了这个问题,因为我有一部分工作。我的复选框值不会插入但我的user_id会插入。谁能告诉我为什么我的复选框没有插入我的数据库?

我已更新了我的eNISATExec.php文件。我是php的新手,我需要帮助我正在进行的项目。我知道不推荐使用mysql函数,但我会将它们用于此项目。我有多个复选框,我希望在我的数据库表(enisatanswer)中插入值,虽然我也希望使用会话登录的用户存储user_id(我的用户表的主键)。

我确实在没有user_id的情况下插入了复选框,尽管这对于我能够在我的这部分工作后能够在下一页上显示内容是必不可少的。

我的表(Log,Worktray,Visual)等中的每个复选框都有一个单独的列,以及user_id的列。我尝试在INSERT语句的末尾添加WHERE语句,尽管我收到错误:

  

解析错误:语法错误,意外')'in   第31行的C:\ wamp \ www \ Login \ eNISATExec.php。

在此之前我收到一个错误:第1行的列数不匹配。在eNISATExec.php中我的$查询显然存在问题,尽管我无法解决它。

非常感谢任何帮助。这是我的文件:

dbconnect.php

<?php
if(!@mysql_connect("localhost","root","#########"))
{
     die('There was connection problem ! --> '.mysql_error());
}
if(!mysql_select_db("dbtest"))
{
     die('There was database selection problem ! --> '.mysql_error());
}
?>

的index.php

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

if(isset($_SESSION['user'])!="")
{
 header("Location: home.php");
}
if(isset($_POST['btn-login']))
{
 $username = mysql_real_escape_string($_POST['username']);
 $upass = mysql_real_escape_string($_POST['pass']);
 $res=mysql_query("SELECT * FROM users WHERE username='$username'");
 $row=mysql_fetch_array($res);
 if($row['password']==md5($upass))
 {
  $_SESSION['user'] = $row['user_id'];
  header("Location: home.php");
 }
 else
 {
  ?>
        <script>alert('User name taken or in the wrong format');</script>
        <?php
 }

}
?>
<!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>NHSCT E-Learning Portal</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="username" placeholder="Your User Name" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-login">Sign In</button></td>
</tr>
<tr>
<td><a href="register.php">Sign Up Here</a></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

home.php

<?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);
?>
<!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 E-Learning Portal</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>
<center>
<h1> Select an E-Learning Module<h1>
<br>
<table align="center" height="200" width="30%" border="0">
<tr>
<td><button name="eNISAT" onclick="window.location.href='eNISATExec.php'">eNISAT Tutorials</button></td>
</tr>
<td><button name="Email" "window.location.href='email.php'">Email Tutorials</button></td>

<tr>
</tr>
</table>
</body>
</html>

eNISATExec.php

    <?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']))
{      
@$userID=$_SESSION['user'];
@$checkbox1=$_POST['Log'];
@$checkbox2=$_POST['Worktray'];
@$checkbox3=$_POST['Visual'];
@$checkbox4=$_POST['ChangePd']; 
@$checkbox5=$_POST['Logout'];
@$checkbox6=$_POST['ClientSearch'];
@$checkbox7=$_POST['StartAssessment'];
@$checkbox8=$_POST['Finalise'];
@$checkbox9=$_POST['Print'];
@$checkbox10=$_POST['Hcn'];
@$checkbox11=$_POST['Lcid'];
@$checkbox12=$_POST['Soscare'];
@$checkbox13=$_POST['Reassign'];
@$checkbox14=$_POST['Close'];

    $query="INSERT INTO enisatanswer (user_id,Log,Worktray,Visual,ChangePd,Logout,ClientSearch,StartAssessment,Finalise,Print,Hcn,Lcid,Soscare,Reassign,Close) VALUES 
    ('$userID', '$checkbox1', '$checkbox2','$checkbox3', '$checkbox4', '$checkbox5', '$checkbox6','$checkbox7', '$checkbox8','$checkbox9', '$checkbox10','$checkbox11', '$checkbox12', '$checkbox13', '$checkbox14')";  
    mysql_query($query) or die (mysql_error() );
if($query==1)
   {  
      echo'<script>alert("Inserted Successfully")</script>';  
   }  
else  
   {  
      echo'<script>alert("Failed To Insert")</script>';  
   }  
}  
?>

0 个答案:

没有答案