我正在尝试使用$ _SESSION将我的ID从php文件传递到另一个php文件,但它无效。似乎ID没有被传递。基本上,我在dbinsert.php
中使用该ID来知道是否只更新或插入数据库中的数据。
的index.php:
<html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="static/css/bootstrap.min.css" media="screen" />
<link rel="stylesheet" type="text/css" href="viewscorescss.css" media="screen" />
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Offsourcing Philippines Inc.</title>
<?php session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "offsourcing";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if((!empty($_POST['firstname'])) &&
(!empty($_POST['lastname'])) &&
(!empty($_POST['middlename']))) {
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mname = $_POST['middlename'];
$sql = mysqli_query($conn, "SELECT * FROM applicants WHERE lastName='$lname' AND fistName='$fname' AND midName='$mname';");
$searchid = mysqli_fetch_assoc($sql);
$id = $searchid['applicantID'];
$_SESSION['id'] = $id;
} ?>
<!--NAVIGATION TAB-->
<div class="row" align="center">
<img src="images/logo2.png" syle="margin-top:20px">
</div>
<hr>
<div class="container" id="testTabs">
<ul class="nav nav-tabs" role="tablist">
<li class= "active"><a href="#Home" class="btn-sm" role="tab" style="color: rgb(0, 0, 0);" data-toggle="tab">Home</a></li>
<li><a href="#GrammarTab" class="btn-sm" role="tab" style="color: rgb(0, 0,0);">Grammar Test</a></li>
<li><a href="#CustomerServiceTab" class="btn-sm" role="tab" style="color: rgb(0, 0,0);">Customer Service Test</a></li>
<li><a href="#LogicalReasoningTab" class="btn-sm" role="tab" style="color: rgb(0, 0,0);">Logical Reasoning Test</a></li>
<li><a href="#IdiomsTab" class="btn-sm" role="tab" style="color: rgb(0, 0,0);">Idioms Test</a></li>
<li><a href="#CallListeningTab" class="btn-sm" role="tab" style="color: rgb(0, 0,0);">Call Listening Test</a></li>
<li><a href="#ViewScoresTab" class="btn-sm" role="tab" style="color: rgb(0, 0,0);">View Scores</a></li>
</ul>
</div>
<div class="tab-content col-md-10" style="margin-left:105px; margin-top:10px">
<div style="background-color: #E9E9E9" class="tab-pane active" id="Home">
<div class="list-group">
<div id="fillup" class="col-md-12" >
<h4><span class="label label-default">Fill in all fields. </span></h4>
<form class="form-horizontal" method="post">
<div class="form-group" >
<label class="col-sm-2 control-label"><small>First name:</small></label>
<div class="col-sm-4">
<input type="text" name="firstname" autocomplete="off" placeholder="Firstname" id="firstname" class="form-control">
</div>
<div class="col-sm-6">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-2 control-label"><small> Last name:</small></label>
<div class="col-sm-4">
<input type="text" name="lastname" autocomplete="off" id="lastname" placeholder="Lastname" class="form-control">
</div>
<div class="col-sm-6">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-2 control-label"><small>Middle name:</small></label>
<div class="col-sm-4">
<input type="text" name="middlename" autocomplete="off" id="middlename" placeholder="middlename" class="form-control">
</div>
<div class="col-sm-6">
</div>
</div>
<button id="proceedToTest" name="proceedToTest" type="submit" class="btn btn-primary">Proceed</button>
</form>
</div>
<div id="tests" style="display:none">
<br>
<button style="margin-left:10px; width: 15%" class= "btn btn-success"><a role="button" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#startGrammar" style="color: rgb(255,255,255); text-decoration: none">Start Online Assessment</a></button>
<br>
<br>
<!-- comment out this part later
<a class="btn btn-primary" role="button" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#startCustomer">Start Customer Service Test</a>
<br>
<a class="btn btn-primary" role="button"data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#startLogical">Start Logical Reasoning Test</a>
<br>
<a class="btn btn-primary" role="button" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#startIdioms">Start Idioms Test</a>
<br>
<a class="btn btn-primary" role="button" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#startCall">Start Call Listening Test</a>
-->
<button style="margin-left:10px; width: 15%" class= "btn btn-primary"><a role="button" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#viewScores" style="color: rgb(255,255,255); text-decoration: none">View Scores</a></button>
<br>
<br>
<img src="images/border.png" width= "100%" height= "65%">
<div class="modal fade bs-example-modal-sm" id="startGrammar" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body">
<b><p> Instructions:</p></b>
<p>You have 8 minutes to answer all questions. You can only take the test once. Click "Start" if you're ready</p>
</div>
<div class="modal-footer">
<a href="#GrammarTab" id="gtest" class="btn-sm" role="tab" data-dismiss="modal" data-toggle="tab" style="color: rgb(91, 55, 119);">Start Grammar Test</a>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<!-- need to link the view scores -->
<div class="modal fade bs-example-modal-sm" id="viewScores" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">View score?</h4>
</div>
<div class="modal-body">
<p>For authorized person only.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" data-backdrop="static" data-keyboard="false" data-toggle="modal" data-target="#passwordcl">Proceed</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<!-- Modal -->
<div class="modal fade" id="passwordcl" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Enter password</h4>
</div>
<div class="modal-body">
<input type="password" name="pass" placeholder="Enter Password here" id="passcl" class="form-control">
</div>
<div class="modal-footer">
<a href="#ViewScoresTab" id="gtest" class="btn-sm" role="tab" data-dismiss="modal" data-toggle="tab" style="color: rgb(91, 55, 119);"><button type="button" class="btn btn-default" data-dismiss="modal" onClick="passCheckcl()">Yes</button></a>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
dbinsert.php:
<?php
session_start();
$id = $_SESSION['id'];
//DATABASE CONNECTION HERE
?>
答案 0 :(得分:1)
您必须将session_start()
和数据库连接放在html代码之上。删除<head>
中连接的PHP代码,然后将其放在文件的顶部。
所以它会这样开始:
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "offsourcing";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
....
您也拼错了fistname
,它应该是firstname
。 请参阅下面有关此拼写错误的评论
$sql = mysqli_query($conn, "SELECT * FROM applicants WHERE lastName='$lname' AND firstName='$fname' AND midName='$mname';");
您可能希望查看自己的输入,它容易受到SQL Injections的攻击。使用
mysqli_real_escape_string
或prepare()
语句(推荐)来转义输入。