如何修复错误“数据库/文件和会话”?

How to fix the error "Database/file and session"?

提问人:Eagles 提问时间:11/19/2019 最后编辑:Lets-c-codeigniterEagles 更新时间:11/19/2019 访问量:47

问:

我的代码有错误。问题是,文件没有插入到数据库表中,而是插入到位置文件夹中。我发现错误是会话没有从第一页获取用户 ID,因此,另一页也发生了同样的事情。你们中的任何人是否给我一些建议或帮助我轻松找到错误(删除,更新或其他东西),这是代码:

第 1 页 - 注册表单页面 (.php)

<?php

include "dbConnection.inc";

if(isset($_POST['signup']))
{
error_reporting(1);

    $Email=$_POST['email'];

    $que1="select * from `users` where Email='$Email'";
    $count1=$que1->num_rows;

    if($count1>0)
    {
        echo "<script>
                alert('There is an existing account associated with this email.');
            </script>";
    }
    else
    {
        $Name=$_POST["first_name"].' '.$_POST["last_name"];
        $gender=$_POST["gender"];
        $country=$_POST["country"];
        $work=$_POST["work"];
        $password=$_POST["password"];


        $query="INSERT INTO `users` (`user_id`, `Name`, `Email`, `gender`, `country`, `profession/occupation`, `Password`) VALUES (NULL, '$Name', '$Email', '$gender', '$country', '$work', '$password');";
        $cmd=$conn->query($query);

        session_start();
        $_SESSION['tempfbuser']=$Email;

        if ($cmd==true) {
            header("Location:completepro.php");
        } else {
            echo "<script>
                alert('Sign Up not successful, please try again');
            </script>";
            header("Location:signin.php");

        }

    }
}
?>

第 2 页 - 注册表单页面 (.php)

<?php

    session_start();
    include "dbConnection.inc";

    error_reporting(1);
    if(isset($_SESSION['tempfbuser']))
    {
        $user=$_SESSION['tempfbuser'];
        $que1="select * from users where Email='$user' ";
        $rec="$que1->fetch_array();";
        $userid=$rec[0];


                if(isset($_POST['file1']) && ($_POST['file1']=='Upload'))
                {
                    $path = "twigp_users/".$user."/Profile/";
                    $path2 = "twigp_users/".$user."/Post/";
                    $path3 = "twigp_users/".$user."/Cover/";
                    mkdir($path, 0, true);
                    mkdir($path2, 0, true);
                    mkdir($path3, 0, true);

                    $img_name=$_FILES['pro_file']['name'];
                    $img_tmp_name=$_FILES['pro_file']['tmp_name'];
                    $prod_img_path=$img_name;
                    move_uploaded_file($img_tmp_name,"twigp_users/".$user."/Profile/".$prod_img_path);

                    $query="INSERT INTO `user_profile_pic` (`profile_id`, `user_id`, `image`) VALUES (NULL, '$userid', '$img_name');";
                    $cmd=$conn->query($query);
                }


                if(isset($_POST['file2']) && ($_POST['file2']=='Upload'))
                {
                    $path = "twigp_users/".$user."/Profile/";
                    $path2 = "twigp_users/".$user."/Post/";
                    $path3 = "twigp_users/".$user."/Cover/";
                    mkdir($path, 0, true);
                    mkdir($path2, 0, true);
                    mkdir($path3, 0, true);

                    $img_name=$_FILES['cvr_file']['name'];
                    $img_tmp_name=$_FILES['cvr_file']['tmp_name'];
                    $prod_img_path=$img_name;
                    move_uploaded_file($img_tmp_name,"twigp_users/".$user."/Cover/".$prod_img_path);
                        $query="INSERT INTO `user_cover_pic` (`cover_id`, `user_id`, `image`) VALUES (NULL, '$userid', '$img_name');";
                        $cmd=$conn->query($query);
                }

  }

?>

请帮我找出错误,谢谢!!

PHP 会话 mysqli

评论

0赞 Jinesh 11/19/2019
你在哪里写文件上传代码?
1赞 M. Eriksson 11/19/2019
警告!您对 SQL 注入攻击敞开了大门!您应该使用参数化的预准备语句,而不是像这样直接在查询中使用完全未转义的用户数据。
2赞 M. Eriksson 11/19/2019
永远不要以纯文本形式存储密码!您应该始终使用 password_hash() 对密码进行哈希处理,并且只存储哈希值。然后,您可以使用 password_verify() 根据哈希来验证密码。
0赞 Eagles 11/19/2019
先生,定义如下(文件上传代码)请滚动
0赞 M. Eriksson 11/19/2019
$rec="$que1->fetch_array();";不会按照你想的去做。将其更改为(不带引号)。不能调用双引号字符串内的方法/函数。$rec = $que1->fetch_array();

答:

-2赞 Jinesh 11/19/2019 #1

检查下面的代码

<?php

    session_start();
    include "dbConnection.inc";

    error_reporting(1);
    if(isset($_SESSION['tempfbuser']))
    {
        $user=$_SESSION['tempfbuser'];
        $que1="select * from users where Email='$user' ";
        $rec="$que1->fetch_array();";
        $userid=$rec[0];


                if(isset($_POST['file1']) && ($_POST['file1']=='Upload'))
                {
                    $path = "twigp_users/".$user."/Profile/";
                    $path2 = "twigp_users/".$user."/Post/";
                    $path3 = "twigp_users/".$user."/Cover/";
                    mkdir($path, 0, true);
                    mkdir($path2, 0, true);
                    mkdir($path3, 0, true);

                    $img_name=$_FILES['pro_file']['name'];
                    $img_tmp_name=$_FILES['pro_file']['tmp_name'];
                    $prod_img_path=$img_name;
                    move_uploaded_file($img_tmp_name,"twigp_users/".$user."/Profile/".$prod_img_path);

                    $query="INSERT INTO `user_profile_pic` (`profile_id`, `user_id`, `image`) VALUES (NULL, '$userid', '$img_name');";
                    $cmd=$conn->query($query);
                }


                if(isset($_POST['file2']) && ($_POST['file2']=='Upload'))
                {
                    $path = "twigp_users/".$user."/Profile/";
                    $path2 = "twigp_users/".$user."/Post/";
                    $path3 = "twigp_users/".$user."/Cover/";
                    mkdir($path, 0, true);
                    mkdir($path2, 0, true);
                    mkdir($path3, 0, true);

                    $img_name=$_FILES['cvr_file']['name'];
                    $img_tmp_name=$_FILES['cvr_file']['tmp_name'];
                    $prod_img_path=$img_name;
                    move_uploaded_file($img_tmp_name,"twigp_users/".$user."/Cover/".$prod_img_path);
                        $query="INSERT INTO `user_cover_pic` (`cover_id`, `user_id`, `image`) VALUES (NULL, $userid, $img_name);";
                        $cmd=$conn->query($query);
                }

  }

?>

评论

1赞 M. Eriksson 11/19/2019
一个好的答案包括一个解释,包括问题是什么,你改变了什么以及为什么它解决了这个问题。我还可以找到至少一个仍然存在的问题,即该行$rec="$que1->fetch_array();";
0赞 Eagles 11/19/2019
@MagnusEriksson感谢您的支持和帮助。更改为$rec="$que1->fetch_array();";$rec = $que1->fetch_array();