使用 PHP 将单选按钮值添加到 MYSQL 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15868935/
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
Adding radio button values to MYSQL table using PHP
提问by Steven Trainor
I am using PHP/MYSQL to create a form that includes radio buttons. I am trying and add the value of checked radio buttons to a table within a database. At the minute I can't get anything to add to the database. The table is called assessment.
我正在使用 PHP/MYSQL 创建一个包含单选按钮的表单。我正在尝试将选中的单选按钮的值添加到数据库中的表中。目前我无法将任何内容添加到数据库中。该表称为评估。
QUESTION1.PHP
问题1.PHP
<?php
include 'core/init.php';
include 'includes/overall/overall_header.php';
protect_page();
include 'includes/menu.php';
include 'includes/overall/navigate.php';
include 'includes/widgets/loggedin.php';
?>
<h1>Assessment</h1>
<form action="save.php" method="post">
<p class="p1">
Question 1</p>
<p class="p4">
Are you tall or short?</p>
<p class="p3">
<input type="radio" name="q1" value="1" />
1
<input type="radio" name="q1" value="2" />
2
<input type="radio" name="q1" value="3" />
3
<input type="radio" name="q1" value="4" />
4
<input type="radio" name="q1" value="5" />
5
</p><br><br>
</form>
<img src="Images/image1.png" alt="Submit" class="thumbnail" align="right" width="58" height="52" id="question2">
<img src="Images/save.png" alt="Submit" class="thumbnail" align="right" width="65" height="52">
<?php
}
include 'includes/overall/overall_footer.php';
?>
SAVE.PHP
保存.PHP
<?php
session_start();
include('connection.php');
$q1=$_POST['q1'];
mysql_query("INSERT INTO `assessment` (q1) VALUES ('$q1')");
header("location: question2.php?");
mysql_close($con);
?>
采纳答案by Bharanikumar
i would like to know, what is the purpose of these two image TAG. i just changed those img tag into button image .
我想知道,这两个图像标签的目的是什么。我只是将那些 img 标签更改为按钮图像。
<h1>Assessment</h1>
<form action="save.php" method="post">
<p class="p1"> Question 1</p>
<p class="p4"> Are you tall or short?</p>
<p class="p3">
<input type="radio" name="q1" value="1" />
1
<input type="radio" name="q1" value="2" />
2
<input type="radio" name="q1" value="3" />
3
<input type="radio" name="q1" value="4" />
4
<input type="radio" name="q1" value="5" />
5 </p>
<br>
<br>
<input type="image" src="Images/image1.png" />
<input type="image" src="Images/save.png" />
</form>
save.php
保存.php
<?php
if (isset($_POST['q1'])){
$q1 = $_POST['q1'];
mysql_query("INSERT INTO assessment (q1) VALUES ('$q1')");
}
?>
回答by Deepak Yadav
if (isset($_POST['q1'])) {
$q1 = $_POST['q1'];
$stmt = $db->prepare("INSERT INTO members (q1) VALUES (:q1)");
$stmt->execute(array(':q1' => $_POST['q1']));
}
回答by Av3ng34
There is a radio button of gender to choose from male and female
有一个性别单选按钮可以选择男性和女性
<?php
$gender=$_POST['radiobutton_name'];
$sql="INSERT INTO table_name(column_name)
VALUES('$gender')";
?>
回答by Prabhu Nandan Kumar
I am just follow your code and giving solution. You need to just few changes in save.phpas follow:
我只是按照您的代码并提供解决方案。您只需要在save.php 中进行一些更改,如下所示:
<?php
if (isset($_POST['q1'])) {
$q1 = $_POST['q1'];
$sql = mysql_query("INSERT INTO assessment (q1) VALUES ('$q1')") or die("MySQL Error : ".mysql_error($conn));
if ($sql) {
echo "Data Saved";
} else {
echo "Data not saved";
}
}
If find any issue please let me know.
如果发现任何问题,请告诉我。
Important Note :Please avoid mysql_* kind of function its deprecated in PHP 5.5.0 and onwards.
重要说明:请避免在 PHP 5.5.0 及更高版本中弃用的 mysql_* 类型的函数。