dongnuochen9449 2013-02-11 14:03
浏览 7
已采纳

PHP - 表单数据未显示在数据库表中

I am doing an exercise that extracts data from a form, then inputs the data into a table in the database.

I have total of 6 fields on the form, but only first 3 fields are registered in the table.

Data type in the table

first_name  varchar(30)
last_name   varchar(30)
pd          varchar(10)
b_month     varchar(2)
b_day       varchar(2)
b_year      varchar(4)

HTML Code for the form

    <form action="handle_reg2.php" method="post">
    <p>first name: <input type="text" name="first_name" size="20" /></p>
    <p>last name: <input type="text" name="last_name" size="20" /></p>
    <p>Password: <input type="password" name="pwd" size="10" /></p>
    <p>confirm password: <input type="password" name="confirm" size="10" /></p>
     <select name="month">
    <option value="">Month</option>
    <option value="1">January</option>
     </select> 
     <select name="day">
    <option value="">Day</option>
    <option value="1">1</option>
  </select>
  <input type="text" name="year" value="yyyy" size="4"/>
  </p>

PHP Code to handle the form data

$db_connect = mysql_connect("localhost", "$db_user", "$db_pass");
 $sql = "insert into reg_data (first_name, last_name, pd, b_month, b_day, b_year) 
 values 
 ('$first_name', '$last_name', '$password', '$b_month',
 '$b_day', '$b_year')";

$insert_data = mysql_query ($sql, $db_connect );

Please take a look at my code fragment and advise me what is the cause and how to resolve the issue.

  • 写回答

1条回答 默认 最新

  • doushangan3690 2013-02-11 14:07
    关注

    You altered the name of the variables ... EG

    $byear = $_POST['year'];
    

    TO

    $b_year
    

    EDIT:

    You've changed the Q and removed the code showing how you named the incoming variable $byear and then tried to reference it as $b_year.

    Stick to a naming convention and you'll have less of these daft errors. EG from your form to your db:

    <input type =text name="b_year" />
    

    THEN

    $b_year = (int)$_POST['b_year'];
    

    THEN

    "insert into mytable (b_year) values ($b_year )";
    

    Except of course you will have listed to what everyone has said and be using prepared statements or have found another way to protect your db.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突