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条)

报告相同问题?

悬赏问题

  • ¥20 关于#r语言#rda分析作图的问题,请各位专家解答!
  • ¥50 找回 股票行情系统的资源问题
  • ¥15 gwas 分析-plink 检查人口分层出现下面问题
  • ¥15 关于#matlab#的问题:需要 MATLAB 运行程序,对比四个多址通信方式(TDMA,FDMA,CDMA,OFDMA)的抗噪声性能
  • ¥15 有没有复现过PFENet的,如何解决?
  • ¥15 vue3使用swiper轮播图循环失败
  • ¥15 FDMA,OFDMA,TDMA 和 CDMA 系统容量对比仿真
  • ¥15 图神经网络的入侵检测
  • ¥15 工业数据采集技术+存储架构推荐
  • ¥15 GPT-SOVITS如何训练少数民族语言支持合成语音