dongzhong7443 2010-11-23 20:55
浏览 27
已采纳

更新mysql并为现有值添加值

I am trying to edit this code so that instead of just inserting the info - it checks to see if the file already exists in the database, if it does it inserts as it does now. If it does exist... it should just update the info, adding the "value" amount to the value amount already in there instead of replacing it. But this is very new to me and I am lost so any help with really be appreciated!

<?php
 define('JPATH_BASE',$_SERVER['DOCUMENT_ROOT']);
 require_once($_SERVER['DOCUMENT_ROOT']."/b2/configuration.php");

 require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/factory.php");
 require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/base/object.php");
 require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/database/database.php");
 require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/database/database/mysql.php");

 $config = new JConfig;
 $options = array();
 $options['host'] = $config->host;
 $options['user'] = $config->user;
 $options['password'] = $config->password;
 $options['database'] = $config->db;
 $options['prefix'] = $config->dbprefix;
 $options['select'] = true;


 $db = new JDatabaseMySQL($options);

 //$user = JFactory::getUser();
 $userId = 0;
 //($user->id > 0) ? $user->id : 0;
 $numbervote = $_REQUEST['w'];
 $folder = $_REQUEST['w2'];
 $name = $_REQUEST['w1'];
  $date = date('Y-m-d H-i-s');
  $sql = "INSERT INTO jos_image_ratetting (id,userid,datecreated,value,filename,folder,md5name) VALUES(NULL,'".$userId."','".$date."','".$numbervote."','".$name."','".$folder."',MD5('".$name."'))";
  $db->setQuery($sql);
  if($db->query()){
   $msg = "Thanks for rate!";
  }else{
   $msg = mysql_error();
  }
  echo $msg;
 //echo 'Hello';

?>
  • 写回答

1条回答 默认 最新

  • dongyin0628 2010-11-23 20:59
    关注

    Take a look at the ON DUPLICATE KEY UPDATE syntax for MySQL

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?