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

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

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数