dongshiru5913 2013-01-16 22:25
浏览 40
已采纳

无法从html表单中将数据插入MYSQL [关闭]

I want to do a simple registration form that stores data in the database but it doesn't seem to be working for me.

This is my html code:

<html>
<body>
    <form method="post" name="register" action="register.php">
    <table>
    <tr>
    <td>User Name:</td>
    <td><input type="text" name="name"></td>
    </tr>
    <tr>
    <td>User Email:</td>
    <td><input type="text" name="email"></td>
    </tr>
    <tr>
    <td>User Role:</td>
    <td><input type="text" name="role"></td>
    </tr>
    <tr>
    <td><input type="submit" name="SubmitForm" value="Save"></td>
    </tr>

</form>
</body>
</html>

And this is my PHP code:

<?php
// Make a MySQL Connection

$user="root";
$password="my password";
$database="playtime";
$host="localhost";
$table="ttuser";

mysql_connect($host, $user, $password) or die("Connection Failed");
mysql_select_db($database) or die("Connection Failed");

$name = $_POST['name'];
$email = $_POST['email'];
$role = $_POST['role'];

// Insert a row of information into the table "ttuser"

$query = "INSERT INTO ttuser (uemail, uname, urole) VALUES('$email', '$name', '$role')";
if(mysql_query($query)){
echo "inserted";}
else{
echo "fail";}

?>

it connects to the database fine, but when I click submit on the html page it goes to php but doesn't show anything. Can someone please help ?

  • 写回答

2条回答 默认 最新

  • dpr81047 2013-01-17 03:04
    关注

    If the table ttuser has any fields that do not have default values and you are not providing any data to those fields (e.g., there is a field called "usalary" with no default value that was not included in your INSERT INTO statement), the MySQL statement will fail.

    Heed the many sql injection warnings :)

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

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?