douhuo0884 2016-02-08 13:34
浏览 16
已采纳

尝试阅读表时问题 - MySQL

I believe that this is a really basic problem but i am starting my coding at PHP so i really can't do it all alone:

I've been for 2 days trying to solve a strange problem. I am using XAMPP for my database and its tables with the objective of creating a login and register system.

This is my user table:

CREATE TABLE `playware`.`users` (
`user_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 25 ) NOT NULL ,
`surname` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 35 ) NOT NULL ,
`telephone` VARCHAR( 9 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
UNIQUE (`email`)
) ENGINE = MYISAM ;

The register .php file is working (it's adding the user to the table correctly) but when i try to make login i just can't.

So I've made this on my login.php file just to test if it gets the row:

<?php
include_once 'connect.php';
session_start();
if(isset($_SESSION['user'])!="")
{
    header("Location: index_home.php");
}
$email = $_POST["email"];
$upass = $_POST["password"];
$sql="SELECT email FROM users";
$res=$conn->query($sql);
if($res->num_rows>0)
{
    while($row=$res->fetch_assoc());
    {
        if($row["email"]==$email)
        {
            echo"Writting email: ".$row["email"]." - done writting.";
        }
    }
}
?>

All time i try to do this results on a white screen, nothing more, when it's supposed to appear the user email. Someone knows how to fix this?

================================UPDATE================================== The error was:

while($row=$res->fetch_assoc());
{
    //Stuff here ...
}

It can't take the ";" there because it cancels the outputting. So the fix is:

while($row=$res->fetch_assoc())
(...)

As I expected, it was a basic error ... Thank you for people that supported me.

  • 写回答

1条回答 默认 最新

  • dpr77335 2016-02-08 13:39
    关注

    You have an error here:

    if(isset($_SESSION['user'])!="")
    

    You cannot check if a variable is set and not equal to blank in this way. Instead you should do this:

    if(isset($_SESSION['user']) && $_SESSION['user']!="")
    

    You have another error here, which stops the script from outputting anything:

    while($row=$res->fetch_assoc());
    ---------remove the -----------^
    

    Since that is valid syntax you may not have seen anything in error reporting.


    You really should use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack.

    In addition make sure you're using prepared statements for PDO and/or MySQLi and consider using PDO, it's really pretty easy.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。