更新mysql并为现有值添加值

时间:2010-11-23 20:55:48

标签: php mysql joomla

我正在尝试编辑此代码,以便不仅仅插入信息 - 它会检查文件是否已经存在于数据库中,如果是,它会像现在一样插入。如果它确实存在......它应该只更新信息,将“值”数量添加到其中已有的值,而不是替换它。但这对我来说是非常新的,我很失落所以任何帮助真的很值得赞赏!

<?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 个答案:

答案 0 :(得分:2)

查看MySQL的ON DUPLICATE KEY UPDATE语法