我有一个名为checklog.php的单独文件,其中包含我的会话详细信息

<?php
        session_start();
        if (!isset($_SESSION['logged'])){
            $_SESSION = array();
            header('location: login.php');
        }
?>

我的主上传页面上菜谱被上传到数据库中,我试图将用户id发布到数据库中,但没有效果
<?php
require_once ("checklog.php");
require_once ("function.php");
include_once ("home_start_logged.php");
require_once ("db_connect.php");
//get form data//
$_SESSION['userid']== $_POST['userid'];
$upload = trim($_POST['Upload']);
$mealname = trim($_POST['mealname']);
$ingredients = trim($_POST['ingredients']);
$hours = trim($_POST['hours']);
$minutes = trim($_POST['minutes']);
$recipe = trim($_POST['recipe']);

echo $_SESSION['userid'];
if(trim($_POST['Submit']) =="Upload"){
if($db_server){
    //clean the input now we have a db connection//
    $mealname = clean_string($db_server, $mealname);
    $ingredients = clean_string($db_server, $ingredients);
    $hour = clean_string($db_server, $hour);
    $minutes = clean_string($db_server, $minutes);
    $recipe = clean_string($db_server, $recipe);
    $ingredients = clean_string($db_server, $ingredients);
    $image = clean_string($db_server,$image);
    mysqli_select_db($db_server, $db_database) ;


//check whether the recipe exists//
$query= "SELECT mealname FROM `recipename` WHERE mealname='$mealname'";
$result = mysqli_query($db_server, $query);
if ($row = mysqli_fetch_array($result)){
        $message = "Meal already exists. Please try again.";
    }else{
        //upload recipe to database//
        $query = "INSERT INTO `recipename` (
                      mealname, ingredients, hours, minutes, recipe,
                      imagepath, userID) VALUES ('$mealname',
                      '$ingredients','$hours','$minutes','$recipe',
                      '$image','" . $_SESSION['userid'] . "')";
        echo query;
    mysqli_query($db_server, $query) or
        die("Insert failed. ". mysqli_error($db_server));
    }

我的表格是这样的:
<form method="post" action="upload.php" enctype="multipart/form-data">
    <li>
Meal Name
    <input type="text" name="mealname" />
    </li>
    <li>
Ingredients
    <input type="text" name="ingredients" />
    </li>
    <li>
Cooking Time
    <input type="number" name="hours" placeholder="Hours" />
    <input type="number" name="minutes" placeholder="Minutes" />
     </li>
     <li>
 Recipe
 <input type="text" name="recipe"/>
     </li>
     <li>
Have you got a photo?
    <input type="file" name="image" id="image" size="10"/>
    </li>
  <input type="submit" id="submit" name="Submit" value="Upload" />
  </form>

这是我定义$会话的登录表单
<?php
require_once ("function.php");
    //get form data//
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    //start session//
    if ($username&&$password) {
        session_start();
        require_once("db_connect.php");
        //clean the input now we have a db connection//
        $username = clean_string($db_server, $username);
        $password = clean_string($db_server, $password);
        $repeatpassword = clean_string($db_server, $repeatpassword) ;
        mysqli_select_db($db_server, $db_database) ;
        //check whether the username exists//
        $query="SELECT * FROM Users WHERE Username='$username'";
        $result=mysqli_query($db_server, $query) ;
        if ($row = mysqli_fetch_array($result)){
            $db_username = $row['Username'];
            $db_password = $row['Password'];
            $db_id = $row['userid'];

            if ($username==$db_username&&salt($password)==$db_password){
                $_SESSION['username']=$username;
                $_SESSION['userid']=$db_id;
                $_SESSION['logged']="logged";
                header ('Location: phpdatabase.php');
            }else{
                $message = "<h1>Incorrect Password!</h1>";
            }
        }else{
            $message = "<h1>That user does not exist!</h1>" .
                            "Please <a href='login.php'>try again</a>";
        }
        mysqli_free_result($result);
        require_once ("db_close.php");
    }else{
        $message = "<h1>Please enter a valid username/password</h1>";
    }
?>

<!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>Fud.</title>
<link rel="stylesheet" type="text/css" href="site.css" />

</head>
    <h1>Login</h1>
    <form action='login.php' method='post'>
    Username:<input type='text' name='username'><br />
    Password: <input type='password' name='password'><br />
   <input type='submit' name='submit' value='Login' />
   <input name='reset' type='reset' value='reset' />

   <h4><a href='register.php'>Register</a></h4>

</form>
       <?php echo $message; ?>

最佳答案

在名为userid的窗体中不会出现任何内容,因此当您设置:

$_SESSION['userid']= $_POST['userid'];

$_POST['userid']是空的,可能您已经在$会话中设置了用户id,如果您不需要设置任何新的用户id,请使用它。

关于php - 尝试将用户ID上传到数据库,Session返回用户ID为null?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20456895/

10-14 00:23