插入VALUES无法正常工作(以及内爆功能)

时间:2013-10-09 20:43:16

标签: php mysqli

所以我有一个php脚本应该将通过表单输入的所有内容插入到数据库中。我所做的是将所有值存储在一个数组中,然后我试图在插入表时插入它们,这样我就可以一次处理它们。

但是我一直收到这个错误,我不知道为什么:

错误:“字段列表”中的未知列'test'

似乎正在发生的事情是,implode函数释放在表单中输入的实际值(而不是列名),并且insert函数试图将它们插入列中,而实际上这不应该正在发生,因为$ profileCols只是一个表示列名的字符串数组。

有人可以帮助我,在这里你可以找到表格和错误。

<?php
$profile = $_POST["profile"];
$requestedAmount = $_POST["requestedAmount"];
$currentBalance = $_POST["currentBalance"];
$creditScore = $_POST["creditScore"];
$timeInBusiness = $_POST["timeInBusiness"];
$avgMonthly = $_POST["avgMonthly"];
$noBankDeposits = $_POST["noBankDeposits"];
$avgBalance = $_POST["avgBalance"];
$monthlyNSF = $_POST["monthlyNSF"];
$industryType = $_POST["industryType"];
$endingBalance = $_POST["endingBalance"];

$profileValues = array("$profile", "$requestedAmount", "$currentBalance",       "$creditScore", "$timeInBusiness", "$avgMonthly", "$noBankDeposits", "$avgBalance", "$monthlyNSF", "$industryType", "$endingBalance");

 $profileCols = array('profile', 'requestedAmount', 'currentBalance', 'creditScore', 'timeInBusiness', 'avgMonthly', 'noBankDeposits', 'avgBalance', 'monthlyNSF', 'industryType', 'endingBalance');

if (isset($profileValues))
{
$entry = 'INSERT INTO profileBuilder (' . implode(",", $profileCols) .') VALUES (' . implode (",", $profileValues) . ')';
} else {
echo "failure buddy!";
}

if (!mysqli_query($con,$entry))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:0)

您需要检查/转义进入查询的所有数据。使用mysqli_real_escape_string()。您可以将它与$ _POST变量上的array_map()/ array_walk()一起使用。如果您的任何值是文本,则需要引用它们:

VALUES (\'' . implode ("','", $profileValues) . '\')';