PHP中怎样把表单单选按钮的值写入数据库中
p代ç ä¸è·å表åä¸åéæé®çå¼ï¼ï¼åéæé®åªè½è®©æ们éæ©ä¸ä¸ªï¼è¿éæä¸ä¸ªâcheckedâå±æ§ï¼è¿æ¯ç¨æ¥é»è®¤éåçï¼æ们æ¯æ¬¡å·æ°æ们ç页é¢æ¶å°±é»è®¤ä¸ºè¿ä¸ªå¼ãï¼
ä¾ï¼<form name="myform" action="" method="post">
æ§å«:
<input type="radio" name="sex" value="ç·" checked />ç·<input name="sex" type="radio" value="女" />女
<input type="submit" name="submit" value="æ交" />
</form>
<?php
echo "æ¨çéæ©æ¯ï¼";
echo $_POST["sex"];
?>
å¦æä½ éæ©çæ¯ç·ï¼ååºæ¥çå¼å°±æ¯âç·âï¼è¦æ¯ä½ éæ©çæ¯å¥³ï¼ååºæ¥çå¼å°±æ¯â女âã
ä¾ï¼<form name="myform" action="" method="post">
æ§å«:
<input type="radio" name="sex" value="ç·" checked />ç·<input name="sex" type="radio" value="女" />女
<input type="submit" name="submit" value="æ交" />
</form>
<?php
echo "æ¨çéæ©æ¯ï¼";
echo $_POST["sex"];
?>
å¦æä½ éæ©çæ¯ç·ï¼ååºæ¥çå¼å°±æ¯âç·âï¼è¦æ¯ä½ éæ©çæ¯å¥³ï¼ååºæ¥çå¼å°±æ¯â女âã
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-05-23
举例如下:
file1.php:
<form action="file2.php" method="post">
<input type=radio name=gender value="男" checked></input>
<input type=radio name=gender value="女"></input
<input type=submit value="提交"></input>
</form>
file2.php:
<?php
echo $_POST[gender]; //显示前一页单选按钮传过来的值
//以下是写入数据库操作
$db=mysql_connect("hostname","username","password"); //连接数据库服务器
mysql_select_db("dbname",$db); //选择具体的数据库
mysql_query("set names gbk"); //统一字符集到 gbk
$myinsert="insert into 表名 (gender) values ('$_POST[gender]')"; //构造插入语句
$myresult=mysql_query($myinsert,$db); //执行插入操作
if($myresult)
{
echo "插入成功!";
}
else
{
echo "插入失败"."<br>".mysql_error();
}
?>本回答被网友采纳
file1.php:
<form action="file2.php" method="post">
<input type=radio name=gender value="男" checked></input>
<input type=radio name=gender value="女"></input
<input type=submit value="提交"></input>
</form>
file2.php:
<?php
echo $_POST[gender]; //显示前一页单选按钮传过来的值
//以下是写入数据库操作
$db=mysql_connect("hostname","username","password"); //连接数据库服务器
mysql_select_db("dbname",$db); //选择具体的数据库
mysql_query("set names gbk"); //统一字符集到 gbk
$myinsert="insert into 表名 (gender) values ('$_POST[gender]')"; //构造插入语句
$myresult=mysql_query($myinsert,$db); //执行插入操作
if($myresult)
{
echo "插入成功!";
}
else
{
echo "插入失败"."<br>".mysql_error();
}
?>本回答被网友采纳
第2个回答 2010-05-23
和表单的TEXT操作一样.
<input type="text" name="text1" value="" /> /*文本框*/
<input type="button" name="button1" value="button1" /> /*按钮*/
<?php
$button=$_POST['button1'];
....
?>
细节部分我不写了!本回答被提问者采纳
<input type="text" name="text1" value="" /> /*文本框*/
<input type="button" name="button1" value="button1" /> /*按钮*/
<?php
$button=$_POST['button1'];
....
?>
细节部分我不写了!本回答被提问者采纳