php 将单选按钮值插入到 mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25295516/
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
Insert radio button value to mysql
提问by Elit3d
I have created a test form just to try to send my radio button value to mysql. I am having problems with it at the moment. The code below is just a test, I want the radio button to submit the value but it isn't.
我创建了一个测试表单只是为了尝试将我的单选按钮值发送到 mysql。我现在有问题。下面的代码只是一个测试,我希望单选按钮提交值,但它不是。
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>TEST </strong></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Case</td>
<td>:</td>
<td><input name="case" type="radio" id="case1"> <input name="case" type="radio" id="case2"> <input name="case" type="radio" id="case3"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
And here is the connection part of the database
这是数据库的连接部分
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="123"; // Mysql password
$db_name="store"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$name=$_POST['name'];
$case=$_POST['case'];
$email=$_POST['email'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, case, email)VALUES('$name', '$case', '$email')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
回答by koosie
This is done by adding values to radio button input. For instance:
这是通过向单选按钮输入添加值来完成的。例如:
<form method="post">
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
<input type="submit">
</form>
回答by paolo
You should start with closing all of your <input>
tags with </input>
or at least a slash at the end (like <input name="case" type="radio" id="case1"></input>
).
您应该首先关闭所有<input>
标签,</input>
或者至少在末尾有一个斜线(如<input name="case" type="radio" id="case1"></input>
)。
You should set values to your radios (like this they always return 'on'
), whereas the submit button needs neither name nor value.
你应该为你的收音机设置值(像这样它们总是返回'on'
),而提交按钮既不需要名称也不需要值。
EDIT:
编辑:
Define a default radio with selected
in yout input tag! If none is selected, there's no case
getting transmitted and PHP will throw Undefined index: case
when accessing $_POST['case']
.
selected
在你的输入标签中定义一个默认的收音机!如果没有选择,则不会case
被传输,并且 PHPUndefined index: case
在访问$_POST['case']
.
A good way to prevent such errors is to check if all necessary indices are set. You can do the following:
防止此类错误的一个好方法是检查是否设置了所有必要的索引。您可以执行以下操作:
if(isset($_POST['name']) and isset($_POST['case']) and isset($_POST['email'])) { ... }
回答by Yasir Baloch
<!-- Once you have created Mysql connection and column in specified database table,-->
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "website";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else{
echo "Connected successfully";}
$sql="INSERT INTO Registration(Name,FatherName,CNIC,FCNIC,Email,Password,Contact,Gender) VALUES ('$_POST[Name]','$_POST[FatherName]','$_POST[CNIC]','$_POST[FCNIC]','$_POST[Email]','$_POST[Password]','$_POST[Contact]','$_POST[Gender]')";
if (!mysqli_query($conn,$sql))
{
die('Error:'.mysqli_error($conn));
}
echo " & 1 record added";
mysqli_close($conn);
?>
<!--after that you just need to write this code and make sure to adjust this code because i'm posting some portion of my code."-->
<h4>Gender</h4> <input type="radio" value="Male" name="Gender"> Male
<input type="radio" value="Female" name="Gender" >Female <br><br>
<button type="Submit">Submit</button><br>
//This answer is for you.