doubi6215 2012-07-10 06:28
浏览 32
已采纳

MySQL和PHP意外的T_Variable

Okay, so I have a register.php script written and I get an unexpected T Variable when the command tries to execute. The error lies on line 15 at

('$_Post[username]','$_Post[sha_pass_hash]','$_Post[email]','2')";

I also have a second error in my syntax according to Dreamweaver at line 20 for

    die('Error: ' . mysql_error());

If anyone could help it would be greatly appreciated. Thank you in advance.

  • 写回答

5条回答 默认 最新

  • dqyknf4423 2012-07-10 10:22
    关注

    STOP

    Inserting into a database directly from post is always a bad idea. This is the reason PHP is currently stuck with the very un-intuitive magic quotes.

    You should at the very least be using mysql_real_escape_string() to escape your data. For example:

    $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    OR die(mysql_error());
    
    $query = "INSERT INTO users VALUES (
        '" . mysql_real_escape_string($_POST["username"]) . "',
        '" . mysql_real_escape_string($_POST["sha_pass_hash"]) . "',
        '" . mysql_real_escape_string($_POST["email"]) . "',
        '2'
    )";
    
    mysql_query($query);
    

    The reason you have to do this is security based. For example if some malicious set the username field to '); DROP TABLE users; without first escaping your data. You would end up blindly running the following query:

    INSERT INTO users VALUES (''); DROP TABLE users;
    

    Which of course isn't going to end well for your application.

    This is the minimum you should be doing.

    In reality you should really be moving onto MySQLi Which is a much more modern MySQL interface. Here is an example

    $mysqli = new mysqli('mysql_host', 'mysql_user', 'mysql_password', 'mysql_database');
    
    $query = "INSERT INTO users VALUES (
        '" . $mysqli->real_escape_string($_POST["username"]) . "',
        '" . $mysqli->real_escape_string($_POST["sha_pass_hash"]) . "',
        '" . $mysqli->real_escape_string($_POST["email"]) . "',
        '2'
    )";
    
    $mysqli->query($query);
    

    You can even use MySQL in a procedural style. So if Object orientated programing isn't with in your reach yet you will have no problems with MySQLi.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图