php 如何使用php脚本将组合框和单选按钮值存储到mysql数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20834818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:35:43  来源:igfitidea点击:

How to store combo box and radio button values to mysql database using php script?

phphtmlmysql

提问by usminuru

I'm trying to insert data into mysql database using php script. I have inserted text box values like: fname, lname, email and pwd.In this case I have 2 problems. The First one is I can't to assign for variable and store the combo box and radio button values. The Second one is how can to store the apart values of combo box into one like: Month, Day and Year into the database. How can I do that? My Databaseattributes are: "fname, lname, userpasswd, username, bdate, sex" Thanks!

我正在尝试使用 php 脚本将数据插入到 mysql 数据库中。我插入了文本框值,如:fname、lname、email 和 pwd。在这种情况下,我有两个问题。第一个是我不能为变量赋值并存储组合框和单选按钮值。第二个是如何将组合框的分离值存储到数据库中,例如:月,日和年。我怎样才能做到这一点?我的数据库属性是:“ fname, lname, userpasswd, username, bdate, sex” 谢谢!

This is snapshot of my Registration Form

这是我的注册表的快照

  <form name="signup" action="actions/registeruser.php" method="POST" onsubmit="return validate()">
    <input type="text" placeholder="First Name" name="fname">
    <input type="text" placeholder="Last Name" name="lname">
    <input type="text" placeholder="Your Email" maxlength="30" size="45" name="email">
    <input type="password" placeholder="password" maxlength="15" size="45" name="pwd">
    <select name="month" size="">
        <option>Month</option>
        <option>jan</option>
        <option>feb</option>
        <option>mar</option>
    </select>
    <select name="Day" size="">
        <option>Day</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
    <select name="Year" size="">
        <option>Year</option>
        <option>2013</option>
        <option>2012</option>
        <option>2011</option>
    </select><br /><br />
    <input type="radio" name="sex" checked="checked">Male
    <input type="radio" name="sex" >Female
    <input type="submit" value="Sin Up" >
    </form><hr width="2">

This is snapshot of the 'registeruser.php' file

这是“registeruser.php”文件的快照

<?php
    session_start();

$dbhost="localhost";
$dbuser="root";
$dbpass="";
$tablename="users";
//connect the server & select database
$conn=mysql_connect($dbhost, $dbuser, $dbpass)or die("cannor connect");
mysql_select_db('fb')or die("cannort select DB");

// user details sent from FORM
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $password=$_POST['pwd'];
    $mail=$_POST['email'];
//Register Students 
$sql="INSERT INTO $tablename(fname, lname, userpasswd, usermail)values('$fname','$lname', '$password','$mail')";
$result=mysql_query($sql);

    if($result){
    echo "<font size='+1' color='blue'> $fname, Successfully Registered.</font>";

    }
    else
    {

    echo "<font size='+1' color='red'>Registration Failed.</font>"; 

    }
?>

回答by Tun Zarni Kyaw

You need to add 'value' property to your 'option's. For "Month" options, it should be like this

您需要将 'value' 属性添加到您的 'option's 中。对于“月”选项,应该是这样的

<option value="">Month</option>
<option value="01">jan</option>
<option value="02">feb</option>
<option value="03">mar</option>

For "Day" options, it should be like this

对于“日”选项,它应该是这样的

<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>    

And also, for "Year" options, it should be like this

而且,对于“年份”选项,它应该是这样的

<option value="">Year</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>

In the case of radio button, you also need to add 'value' property

在单选按钮的情况下,您还需要添加 'value' 属性

<input type="radio" name="sex" value="male" checked="checked">Male
<input type="radio" name="sex" value="female" >Female

At the PHP side, you can get POST data like this

在PHP端,可以这样获取POST数据

$date = $_POST["Year"] . "-" . $_POST["month"] . "-" . $_POST["Day"];
$sex = $_POST["sex"];

回答by Alexander van Oostenrijk

To store a date in your MySQL database, you'll need to concatenate the individual year, month and day fields using something like this:

要将日期存储在 MySQL 数据库中,您需要使用以下内容连接各个年、月和日字段:

$date = sprintf("%04d%02d%02d", $year, $month, $day);

This will produce a date with a structure like 20130805which is how MySQL expects a date, and you can subsequently store it in your bdatefield.

这将生成一个日期,其结构类似于20130805MySQL 期望日期的方式,您随后可以将其存储在您的bdate字段中。

As for passing along the values of your selectelements to PHP - don't forget to set a valueattribute for each optionelement.

至于将select元素的值传递给 PHP - 不要忘记value为每个option元素设置一个属性。

回答by shgnInc

For fields fname, lname, password and email choose nvarchar in table and store them by they're real value. But For the fields bdate, sex, I suggest to define datetime and tinyint.

对于字段 fname、lname、password 和 email,在表中选择 nvarchar 并按它们的实际值存储它们。但是对于 bdate、sex 字段,我建议定义 datetime 和 tinyint。

In your application, you can use JQuery jui datetime pickerfor your bdate, and for fields sex you did right. After submitting the form by user, you have to convert the datetime by:

在您的应用程序中,您可以将JQuery jui 日期时间选择器用于您的 bdate,以及您做对的字段 sex。用户提交表单后,您必须通过以下方式转换日期时间:

$bdate=date(Y-m-d h:m:s,$_POST['bdate']);

and for the sex field just store its value as an integer value as so:

对于 sex 字段,只需将其值存储为整数值,如下所示:

$sex=$_POST['sex'];

回答by Sony Mathew

Dear friend the answer to the first question is that you club together the three seperate fields into one variable like this :

亲爱的朋友,第一个问题的答案是您将三个单独的字段组合成一个变量,如下所示:

$date = $_POST['Year'].':'.$_POST['month'].':'.$_POST['Day'];

Now you will get the date in the format as, for eg : '2013:jan:1' . You can store it in a text field or string field in mysql database. For you to store it in date type column in mysql you need some formatting to be done to the date variable.

现在您将获得格式为的日期,例如: '2013:jan:1' 。您可以将其存储在 mysql 数据库中的文本字段或字符串字段中。为了将其存储在 mysql 的日期类型列中,您需要对日期变量进行一些格式化。

Now inorder to get the gender its very simple :

现在为了让性别变得非常简单:

$gender = $_POST['sex'];

Now make sure that the 'bday' ans 'sex' columns in your mysql databse to be of string type or character type. (with at least size 10 for bday and size 6 for sex).

现在确保 mysql 数据库中的 'bday' ans 'sex' 列是字符串类型或字符类型。(生日至少为 10 码,性爱至少为 6 码)。

So the registeruser.php could be modified to :

所以 registeruser.php 可以修改为:

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$password=$_POST['pwd'];
$mail=$_POST['email'];
$bdate = $_POST['Year'].':'.$_POST['month'].':'.$_POST['Day'];
$gender = $_POST['sex'];

//Register Students 
$sql="INSERT INTO $tablename(fname, lname, userpasswd, usermail,bdate,sex)values('$fname','$lname', '$password','$mail','$bdate','$gender')";

$result=mysql_query($sql);