PHP数组_u$POST到SQL
我正在尝试获取当前的PHP代码并将其插入数据库。目前我可以保存名字、姓氏和电子邮件,但无法保存其余的表单数据“性别”和“控制台”。这是密码PHP数组_u$POST到SQL,php,sql,arrays,post,radio,Php,Sql,Arrays,Post,Radio,我正在尝试获取当前的PHP代码并将其插入数据库。目前我可以保存名字、姓氏和电子邮件,但无法保存其余的表单数据“性别”和“控制台”。这是密码 <!Doctype html public> <html> <body> fill out the following form: <table border="1" cellpadding="10"> <td> <h1> Devices owned Survey </h1&
<!Doctype html public>
<html>
<body>
fill out the following form:
<table border="1" cellpadding="10">
<td>
<h1> Devices owned Survey </h1>
<form action="submit_answer.php" method = "POST">
First Name: <br /> <input type="text" name="first" /><br />
<br />
Last Name: <br /> <input type="text" name="last" /> <br />
<br />
Email: <br /> <input type="text" name="email" /> <br />
<br />
<u>Gender</u>: <br />
<br />
<input type="radio" name="gender" value="male" /> Male<br />
<input type="radio" name="gender" value="female" /> Female <br />
<br />
<u>I Have The Following:</u> <br />
<br />
<input type="checkbox" name="console" value="Playstation3" /> Playstation 3<br />
<input type="checkbox" name="console" value="Xbox360" /> Xbox 360 <br />
<input type="checkbox" name="console" value="Wii" /> Wii <br />
<input type="checkbox" name="console" value="Iphone" /> Iphone <br />
<input type="checkbox" name="console" value="MacBook" /> MacBook <br />
<br />
<input type="submit"/>
</form>
</td>
</table>
</body>
</html>
PHP //SUMBMIT FORM
<?php
define('DB_NAME', 'survey');
define('DB_USER', 'root');
define('DB_PASSWORD', 'XXXX');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link)
{
die('Could NOT Connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected)
{
die ('Cant\'t use' . DB_NAME. ':' . mysql_error());
}
echo 'Connected Sucessfully';
$first = $_POST["first"]; // Since method=”post” in the form
$last = $_POST["last"];
$email = $_POST["email"];
$gender = $_POST["gender"];
$console = $_POST["console"];
$sql = "INSERT INTO survey (first, last, email) VALUES
( '$_POST[first]','$_POST[last]','$_POST[email]','$_POST[gender]','$_POST[console]')";
$result = mysql_query($sql);
$result = mysql_query($sql) or die ("could not save record");
mysql_close();
?>
//Also trying to validate the form so each question is answered
填写以下表格:
设备拥有调查
名字:
姓氏:
电子邮件:
性别:
男性
女性
我有以下几点:
Playstation 3
Xbox 360
Wii
Iphone
MacBook
PHP//SUMBMIT表单
将数据保存到数据库的方法非常危险。您很容易受到SQL注入攻击。也就是说,您应该通过mysql\u query
了解SQL注入攻击
我不会完全重写您的代码来修复SQL注入漏洞,但要修复您当前遇到的问题
在代码中,您有:
$sql = "INSERT INTO survey (first, last, email) VALUES
( '$_POST[first]','$_POST[last]','$_POST[email]','$_POST[gender]','$_POST[console]')";
您指定了3列,但传入了5列。您需要添加其他两列
$sql = "INSERT INTO survey (first, last, email, gender, console) VALUES
( '$_POST[first]','$_POST[last]','$_POST[email]','$_POST[gender]','$_POST[console]')";
但说真的,改变你的代码
编辑:
如果要将$\u POST['console']
数组转换为具有逗号分隔值的字符串,请尝试以下操作:
将数组括号添加到名称
属性:
<input type="checkbox" name="console[]" value="Playstation3" /> Playstation 3<br />
<input type="checkbox" name="console[]" value="Xbox360" /> Xbox 360 <br />
<input type="checkbox" name="console[]" value="Wii" /> Wii <br />
<input type="checkbox" name="console[]" value="Iphone" /> Iphone <br />
<input type="checkbox" name="console[]" value="MacBook" /> MacBook <br />
Playstation 3
Xbox 360
Wii
Iphone
MacBook
迭代数组并将值附加到字符串:
<?PHP
$consoleArray = $_POST['console'];
$consoleCommaString = "";
if ($consoleArray != null && is_array($consoleArray)) {
foreach ($consoleArray as $consoleValue) {
$consoleCommaString .= $consoleValue .", ";
}
}
$sql = "INSERT INTO survey (first, last, email, gender, console) VALUES ('$_POST[first]','$_POST[last]','$_POST[email]','$_POST[gender]','$consoleCommaString')";
?>
首先,不要使用不推荐使用的mysql函数,而是使用PDO或mysqli。第二,这有一个高风险的SQL漏洞。提交前你检查过无线电吗?-编辑-处理injection@reik:没有“高风险”。这完全是在乞求注入攻击。@Marc B。是的,直接使用来自$\u POST或$\u GET的未验证数据是一个很大的错误。我想TS必须意识到这一点,否则这将是一场灾难。看起来你想让他们选择多个控制台吗?然后您需要将名称设置为“console[]”而不是console,然后$\u POST['console']将是所选控制台的数组。从那里,您可以将它们作为CSV插入数据库,或者在一个连接的表上执行。我目前正在尝试学习PHP,并且经常读到这方面的内容,但我只是想让它发挥作用。当您的意思是添加两个列时,这是在数据库中完成的。这已更改,但当我尝试单击控制台的多个选项时,我只能在数据库中保存一个答案,而不能保存多个答案。您的调查表是否有列名称性别和控制台?如果没有,则需要将它们添加到表结构中。在INSERT语句中定义列列表,然后定义列中的值列表。在列列表中,只列出了3列,但列出了5个值。我修复了它,因此列出了5列和5个值。我的表调查中保存了性别和控制台。当我运行表单时,我试图将多个选项保存到数据库中,例如console=playstation3、xbox360、iphone、wii、macbook。目前性别保存为女性或男性。请看我编辑的答案。我想那会满足你的要求。它将复选框作为一个数组,然后创建一个值以逗号分隔的字符串。