douyi8315 2014-02-22 16:54
浏览 7

HTML / PHP表单问题

I'm relatively new to web coding, but I know quite a bit, I was making a registration form earlier, which also checks if the fields are empty. It comes up OK, but nothing seems to work, when the fields are empty, the message doesn't come up, and when the fields are fine, it just loads but doesn't actually insert it into the database. Also, once I press the submit button, the form just disappears as well. Any help would be much appreciated.

<form method="post">
<?php
if(isset($_POST['submit'])){
    $username=$_POST['username'];
    $password=$_POST['password'];
    $password=md5(hash("sha512",$password));
    if(empty($username) or empty($password)){
        $message="Please enter information into the fields.";
    } else {
        mysql_query("INSERT INTO `users` VALUES('',$username,$password)");
        $message="Register successful!";
    }
    echo "<div id='box>$message</div>";
}
?>
Username: <input type="text" name="username" /><br>
Password: <input type="password" name="password" /><br>
<input type="submit" name="submit" value="Sign Up">
</form>
  • 写回答

5条回答 默认 最新

  • douchui3933 2014-02-22 16:59
    关注

    It looks like you are not printing out mysql errors, you just need to add or die(mysql_error()); at the end of the query as in my example i did. Anyway you should surround your strings with quotes. The page came out blank when you press because if condition is met. So change as follow

    mysql_query("INSERT INTO `users` VALUES('','$username','$password')") or die(mysql_error());
    

    As side note i would stop using mysql_ function since they are derecated and use instead either PDO or mysqli with prepared statements to avoid any risk of mysql injections since your code is highly vulnerable. LEARN MORE HERE

    As CodeBird correctly stated

    md5 of an empty string will return a 32 chars string, so the password you are testing will never be empty

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?