doumianfeng5065 2014-02-17 04:48 采纳率: 0%
浏览 36

PHP MySQL插入表单不起作用

I have a MySQL database named "culvers" with a user_id INT(4) auto incrementing, a full_name varchar(20) and a user_name varchar(20). I am trying to use this HTML form to add values to the table, but it is not working. I have explored dozens of tutorials and help sites, and it still isn't working. I even put the code on another hosting provider to see if that was the problem. When I click "add" I am taken to a blank page (which is expected, since I don't have a success/error message) but the form data does not insert into the database table.

Also, I know I should sanitize my inputs, but that's not the issue right now. (At least I don't think so)

Here's the form.html code:

<html>
    <head>
        <title>Add User to Table</title>
    </head>
    <body>
        <h1>Add User</h1>
        <form action="adduser.php" method="POST">
            <label>Full name:</label>
            <input id="postname" type="text" name="fullname">
            <label>Username:</label>
            <input id="postuser" type="text" name="username">
            <input type="submit" name="submit" value="Add">
        </form>
    </body>
</html>

And here's the adduser.php code:

<?php
if(isset($_POST['submit'])){
    $connection =  mysql_connect("localhost", "xxxx", "xxxxxxxxxx");
    mysql_select_db("culvers");
    $fullnameOfUser = $_POST['fullname'];
    $usernameOfUser = $_POST['username'];
    $sql = "INSERT INTO users (full_name, user_name) VALUES ('$fullnameOfUser', '$usernameOfUser');
    $result = mysql_query($sql, $connection);
    mysql_close($connection);
}else{
    echo "Error no form data";
}
?>

Thank you very much for your help!

  • 写回答

5条回答 默认 最新

  • dpleylxzx47207117 2014-02-17 04:50
    关注

    You forgot to close the double quotes !

    '$fullnameOfUser', '$usernameOfUser')"; 
                                     ----^ // Add one there
    

    The right code.

    $sql = "INSERT INTO `users` (`full_name`, `user_name`) VALUES ('$fullnameOfUser', '$usernameOfUser')";
    

    You need to switch to PreparedStatements seriously as the above code of yours is directly prone to SQL Injection.

    评论

报告相同问题?

悬赏问题

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