Javascript 表单提交后会话丢失-php

Javascript 表单提交后会话丢失-php,javascript,php,forms,session,Javascript,Php,Forms,Session,一切正常,除了在我提交数据之后,会话数据消失。我已经在页面顶部(在useracc test.php中)设置了session start,这就是userpage。在upload.php中,我不需要启动会话,因为它已经是useracc-test.php页面中的链接(require)。如果我这样做,将会话开始放在upload.php中,它将提示错误,会话已经开始。我在互联网上到处浏览,我尝试的一切都不适合我。我意识到不同的问题需要不同的方法。有些人也有同样的问题,但解决每个问题的方法与我在网上通读的各

一切正常,除了在我提交数据之后,会话数据消失。我已经在页面顶部(在useracc test.php中)设置了session start,这就是userpage。在upload.php中,我不需要启动会话,因为它已经是useracc-test.php页面中的链接(require)。如果我这样做,将会话开始放在upload.php中,它将提示错误,会话已经开始。我在互联网上到处浏览,我尝试的一切都不适合我。我意识到不同的问题需要不同的方法。有些人也有同样的问题,但解决每个问题的方法与我在网上通读的各种案例不同。我被这件事缠住了好几天。这真的让我头疼。请帮忙。任何帮助都将不胜感激

下面是用户登录后的用户页面(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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</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>&nbsp;</p>
<form name="form2" 
      action="useracc-test.php" method="post" enctype="multipart/form-data">
  <p>&nbsp;</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>&nbsp;</td>
      <td><input type="submit" name="submit" id="submit" value="submit"></td>
    </tr>
  </table>
  <div align="center"></div>
  <p>&nbsp;</p>
</form>
<p>&nbsp;</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>&nbsp;</p>
<p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>
</body>
</html>
下面是运行上载脚本的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();
}
?>

$id=($_会话['id'])

我不知道你为什么这样写代码。在变量force周围加上括号以返回值与引用,但这只对return语句重要,即return$var与return$var

但是上面的代码执行以下操作

$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,而不是值。因此,当您最终绑定_param()时,您将使用true/false与值进行绑定 你最好还是这样

$id = isset($_POST['id']) ? $_POST['id'] : <a default value> ;
etc...

$id=isset($\u POST['id'])$_POST['id']:我曾经测试过

我已经解决了我的问题。对于像我这样100%不使用cookie的人来说,当会话丢失时,解决方案是重新声明变量(这主要是某些脚本的一部分)。在同一页上重新声明。实际上,这与头球和退场,或者白线无关,我已经深入研究了一个多月了。我已经完全禁用了我所有终端和远程终端上的cookie,无论有没有cookie,它都可以100%正常工作。最后但并非最不重要的一点是,您必须定义一个会话id,并在页面上方设置启动会话。这就是全部。与标题、出口或白线无关。(此方法仅适用于不使用Cookie的用户-但两者都适用)。Cheerio.

isset
返回布尔值(true或false),因此在以下代码中:
$id=isset($\u POST['id'])
$id
将等于true或false。再也不会是别的了。如果要将id的值设置为
$\u POST['id']
的内容,则代码应为
$id=$\u POST['id']
@Steve..tq,用于回复..我尝试了,但问题仍然存在-(请澄清什么对你不起作用-你建议一些
会话
数据丢失/覆盖,但实际上不要提及该数据是什么。你说
下面的部分消失了…
,但下面的代码根本没有显示任何
会话
数据,只是试图访问
post
数据。我有点在我的第一篇评论中,我甚至试图重新声明$id=isset($\u POST['id']);在会话中。但仍然是同一个问题。@steve..哦,对不起,steve。我会把它拉下来。那个不会消失的。愚蠢的我..对不起..这是因为我睡眠不足..谢谢你的帮助..我很感激。关于会话开始,我曾尝试将它放在upload.php中,但每当我尝试时,它都会提示错误“会话已启动”。其次,数据并没有从数据库中消失,在我按“提交”后,数据只是从浏览器中消失。我尝试了上面的建议。但现在我有一个错误“注意:未定义变量:target\u dir in.....................................................第63行“$target\u file=$target\u dir.basename($\u FILES[“image”;问题仍然存在。一切正常,但当我按下“提交”按钮时,所有显示的记录都消失了。但是,我插入数据没有问题。为了清楚起见,每次它重定向或提交表单时,会话数据都消失了。可能是因为我的登录表单吗?在操作方法中,我使用“?php echo htmlspecialchars($_SERVER[“php_SELF”]);”。登录后,url将显示“”。当我按submit时,url将更改为“”。因为在登录页面中。我有另一个“require”页面,即“connect run.php”,它将运行登录脚本。其中有一个包含“useracc test.php”的出口()在那之后。希望你明白我的意思。也许,因为我刚刚测试了你的代码,在我的机器上进行了几次小的编辑之后,会话数据工作正常。如果你愿意,我可以将我用于测试的编辑发送给你。
$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']);
$id = isset($_POST['id']) ? $_POST['id'] : <a default value> ;
etc...