一切正常,除非我提交数据后,会话数据消失。我已经在页面顶部(在useracc-test.php中)启动了会话,这是用户页面。在upload.php中,我不需要启动会话,因为它已经在useracc-test.php页面中链接(require)。如果我这样做,将会话启动放在upload.php中,它会提示错误,会话已经启动。我在互联网上到处浏览,我尝试的一切都不适合我。我发现不同的问题需要不同的方法。有些人有同样的问题,但解决每个问题的方法与我在网上通过各种案例阅读的方法不一样。我好几天都坚持了下来。它真的让我头晕目眩。请帮助.tq。任何帮助都将非常感激。
以下是用户登录后的用户页面(useracc-test.php)。
<?php
//useracc-test.php
//start session
session_start();
//error_reporting(E_ALL);
//ini_set("display_errors",1);
//run the db connection
require 'connect-test.php';
//run the upload images etc script
require 'upload.php';
//if true, execute below
if(isset($_POST['username'])){
//define variable
$userName = $_POST['username'];
//fetch data from table users
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();
// place in session to echo in html later
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
$_SESSION['telno'] = $row['telno'];
//*******************************************************
//create session for id field
$id=($_SESSION['id']);
//joined table- fetch all rows based on same id in child table useradvert
$query="SELECT ua.* FROM useradvert ua INNER JOIN users u ON ua.id =u.id
WHERE ua.id='".$id."'";
$stmt = $conn->prepare($query);
$stmt->execute();
$res2 = $stmt->get_result();
}
?>
<html>
<head>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
</head>
<body>
<div id="apDiv3">
<p> </p>
<p> </p>
<p> </p>
<p><span class="TabbedPanelsContent">
<?php
//display record from table- users (parent table)
echo $_SESSION['id']."<br/>";
echo $_SESSION['name']."<br/>";
echo $_SESSION['username']."<br/>";
echo $_SESSION['telno']."<br/>";
?>
</p>
<p> </p>
<form name="form2"
action="useracc-test.php" method="post" enctype="multipart/form-data">
<p> </p>
<table width="500" border="0">
<tr>
<td>category</td>
<td><select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
<option value="useracc-test.php" selected>Category</option>
<option value="useracc-test.php">Members</option>
<option value="useracc-test2-jumpmenu.php">Non-members</option>
</select></td>
</tr>
<tr>
<td>ID:</td>
<td><input name="id" type="text" id="id" value="<?php echo $_SESSION['id']; ?>" ></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name2" id="name2"></td>
</tr>
<tr>
<td>Color</td>
<td><input type="text" name="color2" id="color2"></td>
</tr>
<tr>
<td>Hobby</td>
<td><input type="text" name="hobby2" id="hobby2"></td>
</tr>
<tr>
<td>Sex</td>
<td>male
<input type="radio" name="radiobtn" id="radio" value="male">
female
<input type="radio" name="radiobtn" id="radio2" value="female"></td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="image" id="image"></td>
</tr>
<tr>
<td>Image2</td>
<td><input type="file" name="image2" id="image2"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" id="submit" value="submit"></td>
</tr>
</table>
<div align="center"></div>
<p> </p>
</form>
<p> </p>
<p>
<ul>
<?php
if (isset($res2)){
while ($row2 = $res2->fetch_array(MYSQLI_ASSOC)){
echo "<li>".$_SESSION['name2'] = $row2['name2'];
echo $_SESSION['color2'] = $row2['color2'];
echo $_SESSION['hobby2'] = $row2['hobby2'];
echo $_SESSION['radiobtn'] = $row2['radiobtn'];
echo $_SESSION['kupon'] = $row2['kupon'];
echo $_SESSION['image'] = $row2['image'];
echo $_SESSION['image2'] = $row2['image2'];}
}
?>
</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>
</body>
</html>
当我按提交
时,以下部分会消失 $id = isset($_POST['id']);
$name2 = isset($_POST['name2']);
$color2 = isset($_POST['color2']);
$hobby2 = isset($_POST['hobby2']);
$radiobtn = isset($_POST['radiobtn']);
$image = isset($_FILES['image']);
$image2 = isset($_FILES['image2']);
下面是upload.php页面,它运行上传脚本。
<?php
//echo var_dump($_POST);
//echo var_dump($_FILES);
require 'connect-test.php';
if(isset($_POST["submit"])) {
//define variable
$id = $_POST['id'];
$name2 = $_POST['name2'];
$color2 = $_POST['color2'];
$hobby2 = $_POST['hobby2'];
$radiobtn = $_POST['radiobtn'];
$image = $_FILES['image'];
$image2 = $_FILES['image2'];
//target file for image
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//target file2 for image2
$target_dir = "uploads/";
$target_file2 = $target_dir . basename($_FILES["image2"]["name"]);
$uploadOk = 1;
$imageFileType2 = pathinfo($target_file2,PATHINFO_EXTENSION);
//script for targetfile -image
// Check if image or not
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
//script for targetfile2 -image2
// Check if image or not
$check2 = getimagesize($_FILES["image2"]["tmp_name"]);
if($check2 !== false) {
echo "File is an image - " . $check2["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file2)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image2"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType2 != "jpg" && $imageFileType2 != "png" && $imageFileType2 != "jpeg"
&& $imageFileType2 != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image2"]["tmp_name"], $target_file2)) {
echo "The file ". basename( $_FILES["image2"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
//insert record
$stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2,radiobtn,image,image2) VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param("issssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file,$target_file2);
$stmt->execute();
}
?>
答案 0 :(得分:1)
$ ID =($ _ SESSION [&#39; ID&#39;]);
我不确定你为什么要写这样的代码。将括号括在变量force周围以返回值与引用的值,但它仅对return语句有用,即返回$ var vs return($ var)
<?php
$q=1 ;
$r=$q;
$q=2;
echo $r, "\n"; // prints 1
$t =&$q ;
$q=3;
echo $t, "\n"; // prints 3
其次,我猜您的意思是该值会从您的数据库中消失?因为你这样做
$stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2,radiobtn,image,image2) VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param("issssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file,$target_file2);
但是上面的代码执行以下操作
$id = isset($_POST['id']);
$name2 = isset($_POST['name2']);
$color2 = isset($_POST['color2']);
$hobby2 = isset($_POST['hobby2']);
$radiobtn = isset($_POST['radiobtn']);
$image = isset($_FILES['image']);
$image2 = isset($_FILES['image2']);
isset()将返回true或false,而不是值。因此,当你最终使用bind_param()时,你会使用true / false与值绑定 你最好这个
$id = isset($_POST['id']) ? $_POST['id'] : <a default value> ;
etc...
最后“在upload.php中,我不需要启动会话,因为它已经在useracc-test.php页面中链接(require)。”:只有你不是这样才是真的直接调用该脚本。
对于ml2_1jzsinglecam 这是我用来测试的link to the code
答案 1 :(得分:1)
我已经解决了我的问题。对于那些100%不使用像我这样的cookie的人来说,当会话丢失时,解决方案是重新声明变量(这些变量主要是某些脚本的一部分)。在同一页面上重新声明。它实际上与标题和退出或白线无关,我已经深入研究了一个多月。我在我的所有终端和远程终端都完全禁用了cookie,无论是否有cookie,它都可以100%罚款。最后但同样重要的是,您必须定义会话ID,并在页面上方启动会话。这就是全部。与标题,退出或白线无关。 (此方法仅适用于那些不使用cookie的人 - 但两者都可以正常工作)。 Cheerio。