2013-10-09 41 views
-3

所以我有一个PHP脚本,应该将所有通过表单进入的东西插入到数据库中。我所做的是将所有的值存储在一个数组中,然后我试图在插入表时将它们内爆,以便我可以一次处理它们。插入VALUES不能正常工作(以及implode功能)

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

错误:在“字段列表”未知列“测试”

什么似乎正在发生的事情是破灭功能给关闭在表单中输入的实际值(而不是列名)和插入函数试图将它们插入到列中,但实际上这不应该发生,因为$ 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); 
?> 
+3

在您提供的任何代码中没有列'test'。你也开放SQL注入。 – Kermit

+0

你很容易受到SQL注入攻击:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Lorenz

+0

多数民众赞成我说什么,即时通讯不试图把测试进入柱!测试将是我正在进入一个表格 – Philip

回答

0

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

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

真的很重要,因为我的代码现在写的方式我认为它会好的 – Philip

+0

试试看。你的代码现在不起作用了吗? – AbraCadaver

+0

您已将一些引号编辑到您的代码中,但是您放置的位置意味着列名单将失败,并且整个值列表将被视为单个值,除非有人在其文本中包含单引号这种情况下它会失败并出现语法错误(如果你幸运的话)。 – 2013-10-09 21:00:42