-1

我编写了一个程序,用于在数据库表中的列的所有行中插入一个数组,该数组相对于同一个数据库同一个表中已填充的列。

但它似乎不能正常工作。请告诉我哪里出错了!

$query = $sql = "SELECT A FROM `table`";
$result1 = mysql_query($query); 
while ($row = mysql_fetch_row($result1)) {

foreach($output as $value){
$count=sizeof($value); 

for($counter=0;$counter<$count;$counter++)
{

$query= "INSERT INTO `TABLE` (`B`) VALUES ('" . $value . "') WHERE A= $row[$counter] ";

$insert_col= mysql_query($query);

echo $query;
echo '<br/><br/>';
  }} 

} 

其中$output数组包含数组值如下:

$output[0]= RD 3.56 , $output[1]= RD 5.56 , $output[2]= RD 4.86 

当我运行我的程序时,检查我的查询..输出如下:

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_1 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_1 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_1 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_1 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_1 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_1 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_2 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_2 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_2 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_2 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_2 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_2 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_3 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_3 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_3 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_3 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_3 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_3 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_4 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_4 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_4 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_4 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_4 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_4 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_5 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_5 

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_5

请帮忙!我对我的程序中的这个错误快一个多星期了。

输出应如下所示

INSERT INTO 'TABLE' SET B = ' RD: 1.144' WHERE A = '3r_1' 

INSERT INTO 'TABLE' SET B = ' RD: 2.436' WHERE A = '3r_2' 

INSERT INTO 'TABLE' SET B = ' RD: 1.624' WHERE A = '3r_3' 

INSERT INTO 'TABLE' SET B = ' RD: 1.560' WHERE A = '3r_4' 

INSERT INTO 'TABLE' SET B = ' RD: 1.560' WHERE A = '3r_5' 

INSERT INTO 'TABLE' SET B = ' RD: 1.560' WHERE A = '3r_6'

这里的问题是$row值对所有值重复 6 次$output。我该如何解决这个问题?

经过大量试验,我知道问题在于调用数组。因此,如果有人能告诉我如何使用上面编写的代码同时调用两个数组,我将不胜感激。

4

4 回答 4

1

据我了解,你有一个循环问题。

您有 2 个需要同时递增的列表。

尝试这个:

$value = [' RD: 1.144', ' RD: 2.436', ' RD: 1.624', ' RD: 1.560', ' RD: 1.560', ' RD: 1.560'];
$row = ['3r_1', '3r_2', '3r_3', '3r_4', '3r_5', '3r_6'];

$cnt = count($row);//assuming $row and $value are of the same size

for($i = 0; $i < $cnt, $i++) {
    echo "INSERT INTO 'TABLE' SET B = '$value[$i]' WHERE A = '$row[$i]' ";
}

这是一个更简单的示例,但本质上,这是您需要做的。

于 2012-08-30T15:28:45.793 回答
1

您是否正在尝试更多类似的东西

$i = 1;
$query = "SELECT A FROM `table`";
$result1 = mysql_query($query);
while ($row = mysql_fetch_row($result1)) {

    $query = "INSERT INTO `TABLE` (`B`) VALUES ('" . $output[$i] . "') WHERE `A` = '".$row[0]."' ";

    $insert_col = mysql_query($query);

    $i++;

    echo $query;
    echo '<br/><br/>';

}
于 2012-08-30T13:18:51.670 回答
1

试试这个语法:

$query= "INSERT INTO 'TABLE' SET B = '" . $value . "' WHERE A = '$row[$counter]' ";

顺便说一下,表名 5 非常奇怪。

于 2012-08-30T12:04:52.273 回答
0
while ($row = mysql_fetch_row($result1)) {

foreach($output as $key=>$value){

$query = "INSERT INTO `TABLE` (`B`) VALUES ('" . $value . "') WHERE `A` = '".$row[$key]."' ";

$insert_col= mysql_query($query);

echo $query;
echo '<br/><br/>';
  }}

尝试了同样的事情..但得到相同的输出......

于 2012-08-30T13:36:52.893 回答