0

I want to edit a table from my database.That table have many data. I will show sample.Do I need to write many mysql update statement?Have other method to write a only one statement? I am beginner for php? Thank all my friend.Sorry for my english.

        mysql_query("UPDATE `mytablename` SET `mobiletitle` = '$mobiletitle1',
        `mobilepublished` = '1',
        `mobiletext` = '$mobilefulltext1',
         WHERE `id` ='$id1';");

           mysql_query("UPDATE `mytablename` SET `mobiletitle` = '$mobiletitle2',
        `mobilepublished` = '1',
        `mobiletext` = '$mobilefulltext2',
         WHERE `id` ='$id2';");

        mysql_query("UPDATE `mytablename` SET `mobiletitle` = '$mobiletitle3',
        `mobilepublished` = '1',
        `mobiletext` = '$mobilefulltext3',
         WHERE `id` ='$id3';");
   etc.............
        mysql_query("UPDATE `mytablename` SET `mobiletitle` = '$mobiletitle30',
        `mobilepublished` = '1',
        `mobiletext` = '$mobilefulltext30',
         WHERE `id` ='$id30';");
Wai Yan
  • 427
  • 2
  • 6
  • 23

3 Answers3

1

You want to update multiple rows from one table with specific data, so bad news you have to do it one by one.... but you can improve your code. If I where you I will create a function and I just call it, something like

function update_my_data($movilefilltex1,$id1){

mysql_query("UPDATE `mytablename` SET `mobiletitle` = '$mobiletitle1',
        `mobilepublished` = '1',
        `mobiletext` = '$mobilefulltext1',
         WHERE `id` ='$id1';");
.......


}

So to make the multiple insert you can call update_my_data(value1,valu2) the times that you need. for example in a loop or just whenever you need it.

jcho360
  • 3,724
  • 1
  • 15
  • 24
0

If there is a UNIQUE index on id (and there will be if it's your PRIMARY KEY), you could use INSERT ... ON DUPLICATE KEY UPDATE:

INSERT INTO mytablename (id, mobiletitle, mobilepublished, mobiletext) VALUES
  ('$id1', '$mobiletitle1', 1, '$mobilefulltext1'),
  ('$id2', '$mobiletitle2', 1, '$mobilefulltext2'),
  -- etc.
ON DUPLICATE KEY UPDATE
  mobiletitle     = VALUES(mobiletitle),
  mobilepublished = VALUES(mobilepublished)
  mobiletext      = VALUES(mobiletext);

Note that this will, of course, insert new records if they don't already exist; whereas your multiple-UPDATE command approach will not (it would raise an error instead).

In either case, you could build/execute the SQL dynamically from within a loop in PHP.

I would also caution that you would be well advised to consider passing your variables to MySQL as parameters to a prepared statement, especially if the content of those variables is outside of your control (as you might be vulnerable to SQL injection). If you don't know what I'm talking about, or how to fix it, read the story of Bobby Tables.

Putting it all together (using PDO instead of the deprecated MySQL extension that you were using):

for ($i = 1; $i <= 30; $i++) {
  $sqls[] = '(?, ?, 1, ?)';
  $arr[] = ${"id$i"};
  $arr[] = ${"mobiletitle$i"};
  $arr[] = ${"mobilefulltext$i"};
}

$sql = 'INSERT INTO mytablename (id, mobiletitle, mobilepublished, mobiletext)
        VALUES
   ' .    implode(',', $sqls)
     . 'ON DUPLICATE KEY UPDATE
          mobiletitle     = VALUES(mobiletitle),
          mobilepublished = VALUES(mobilepublished)
          mobiletext      = VALUES(mobiletext)';

$db = new PDO("mysql:dbname=$db", $user, $password);
$qry = $db->prepare($sql);
$qry->execute($arr);

Note that you might also consider storing your 1..30 variables in arrays.

Community
  • 1
  • 1
eggyal
  • 122,705
  • 18
  • 212
  • 237
-1
update table1,table2 SET table1.column1 = 'valueX',table2.coulumn2 = 'valueX' where table1.column = table2.coulumn2 ;
user1485518
  • 233
  • 3
  • 11