Advertisement
linesh

Untitled

Feb 12th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2. /*
  3.     table:------------
  4.     CREATE TABLE IF NOT EXISTS `user_meta` (
  5.       `id` int(11) NOT NULL AUTO_INCREMENT,
  6.       `user_id` int(11) NOT NULL,
  7.       `option_name` varchar(50) NOT NULL,
  8.       `option_value` varchar(50) NOT NULL,
  9.       PRIMARY KEY (`id`)
  10.     ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
  11.  
  12.  // default insert -------------->
  13.     INSERT INTO `user_meta` (`id`, `user_id`, `option_name`, `option_value`) VALUES (1, 1, 'name', 'Joe'),(2, 1, 'age', '28'),(3, 1, 'gender', 'male'),(4, 2, 'name', 'jane'),(5, 2, 'age', '26'),(6, 2, 'gender', 'female');
  14. */
  15.  
  16.  
  17. ini_set('display_errors', 1);
  18. error_reporting(1);
  19. $mysql= new mysqli('localhost', 'root', 'root', 'test');
  20.  
  21. // new row values  ---------------->
  22. $new_row=array(
  23.         'name'=>'Donny',
  24.         'age'=>'30',
  25.         'gender'=>'male',
  26.         'country'=>'us'
  27.     );
  28. $user_id='3';
  29.  
  30. // get old rows from DB ------------>
  31. $old_rows=$mysql->query("SELECT user_id from user_meta where user_id='".$user_id."'");
  32.  
  33. // if there are no rows or "$old_rows->num_rows" row count is not equal to "" count for user id ----------------->
  34. if( $old_rows->num_rows==0 || count($new_row)!=$old_rows->num_rows )
  35. {
  36.     // if current row count is not equal to new row count removing old rows from table --------------> 
  37.         if($old_rows->num_rows>0){
  38.             $result=$mysql->query("DELETE from user_meta where user_id='".$user_id."'");
  39.         }
  40.  
  41.         // inserting new rows ------------------>
  42.         $qry= "INSERT INTO user_meta  (user_id,option_name,option_value)";
  43.         foreach($new_row as $index=>$value){
  44.                 $values[]="'".$user_id."', '".$index."','".$value."'";
  45.         }
  46.         $qry .=' VALUES ('.implode('),(',$values).')';
  47.         if($result=$mysql->query($qry)){
  48.             echo 'inserted';
  49.         }else{
  50.             echo $mysql->error;
  51.         }
  52.  
  53. // if rows are same update ------------->
  54. }else{
  55.         $qry .= "UPDATE `user_meta` SET option_value = CASE option_name";
  56.             foreach($new_row as $index=>$value){
  57.                 $qry .= " WHEN '".$index."' THEN '".$value."'  ";
  58.                 $indexes[]=$index;
  59.             }
  60.         $qry .= "END WHERE  user_id ='".$user_id."'  AND `option_name` IN ( '".join($indexes,"','")."') ";
  61.         if($result=$mysql->query($qry)){
  62.             echo 'updated';
  63.         }else{
  64.             echo $mysql->error;
  65.         }
  66. }
  67. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement