2015-04-15 46 views
-1

我将问题简化为简单状态。为什么更新命令不能执行?

mysql> select * from `table`; 
+--------+---------+ 
| f1  | f2  | 
+--------+---------+ 
| hallo | welcome | 
| string | array | 
+--------+---------+ 
2 rows in set (0.00 sec) 

我想将表格更改为以下结果。

mysql> select * from `table`; 
+--------+------- --+ 
| f1  | f2   | 
+--------+---------+ 
| hallo | welcomehaha | 
| string | arrayhaha | 
+--------+------- --+ 
2 rows in set (0.00 sec) 

这是我的代码。

<?php 

    header("Content-Type: text/html; charset=gbk"); 
    $db=new PDO("mysql:host=localhost;dbname=test","root",""); 

$statement1=$db->prepare("select f1,f2 from `table`"); 
$statement1 -> execute(); 
while($row=$statement1->fetch()){ 
    $new_content=$row["f1"]; 
    $new_f2=$row['f2'].'haha'; 
    echo $new_f2.'</br>'; 
    $statement2=$db->prepare("update `table` set f2=$new_f2 where f1={$row['f1']}"); 
    $statement2 -> execute(); 
    }  
?> 

输出是:

welcomehaha 
arrayhaha 

f2值没有改变,为什么不update 'table' set f2=$new_f2 where f1={$row['f1']}正在执行?

+2

因为,你有没有引用你的价值;他们是字符串。另外,你不检查错误。 –

+0

'...设置f2 = 123haha' ...你的桌子上有一个123haha字段吗? –

回答

0

你都必须由单引号之间“”你的价值,因为它是文字可能是类似的东西

$statement2=$db->prepare("update `table` set f2='$new_f2' where f1='{$row['f1']}'"); 
0
$statement2=$db->prepare("update `table` set `f2`=concat(`f2`,'haha');"); 
+0

请提供更多解释来帮助理解,而不是仅仅给他答案。 – Johnride

相关问题