-1

I simplify my problem into a simple status.

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

I want to change the table into the following result.

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

Here is my code.

<?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();
    }    
?>

The output is:

welcomehaha
arrayhaha

The value of f2 isn't changed, why isn't the update 'table' set f2=$new_f2 where f1={$row['f1']} being executed?

Smern
  • 18,746
  • 21
  • 72
  • 90
showkey
  • 482
  • 42
  • 140
  • 295

2 Answers2

0

you must but your value between single quotation '' because it is text may be it is something like that

$statement2=$db->prepare("update  `table` set f2='$new_f2' where f1='{$row['f1']}'");
Islam Zedan
  • 656
  • 4
  • 21
0
$statement2=$db->prepare("update `table` set `f2`=concat(`f2`,'haha');");
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46