如何插入数组值到数据库表中的列? [英] how to insert array values into the column of a database table?
本文介绍了如何插入数组值到数据库表中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个值的红色,绿色和黄色的阵列。那么现在我
I have an array with three values red, green and yellow. Now i should
insert red into column3 row1
insert green into column3 row2
insert yellow into column3 row3
我
如何能做到这一点,我试图写code
How can i do that i tried writing the code
foreach ($output as $value)
{
echo ($value.'<br>');
$tstring = implode(',' , $output);
$insert_col= "UPDATE INTO `5` (B) VALUES ('$tstring')";
$insert_result = mysql_query($insert_col);
if ($insert_result)
{
echo ("RECORDED!")|
exit();
}
}
,但它不工作。它填补额外行到现有的表用值R
but it does not work. it is filling extra rows to the existing table with a value R.
请帮帮忙!
推荐答案
如果我没有记错,UPDATE语句应该有一个WHERE子句。
if I remember correctly, an UPDATE statement should have a WHERE clause.
我爱:
UPDATE table SET column_name='value' WHERE condition;
You can find examples at w3schools.
至于插入正确的值:
foreach($output as $value){
$tstring = $value;
$insert_col = "UPDATE `5` SET B='" . $tstring . "' WHERE insert a condition here";
$insert_result = mysql_query($insert_col);
if ($insert_result) {
echo ("RECORDED!") |
}
}
您不想插入所有的阵列中的一个排,这意味着破灭是没用的。
You don't want to insert all your array in one row, which means that the implode is useless.
和我再说一遍,您需要一个WHERE子句在这里。如果没有它,你会更新相同的值的所有行。
And I repeat, you need a WHERE clause here. Without it, you'll update all your rows with the same values.
这篇关于如何插入数组值到数据库表中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文