【问题标题】:how to maintain Uncaught exception 'ErrorException' in php如何在php中维护未捕获的异常'ErrorException'
【发布时间】:2014-03-03 07:23:48
【问题描述】:

$cuntRs = $this->db->query("SELECT count(*) as cunt from " . DB_PREFIX . "vendhq_product");
if($cuntRs->row["cunt"]==0) {
    foreach ($json["products"] as $dept){
        echo "<strong> Product ". $i++ ."</strong><br/>";

        $passval=$i;

        echo $dept["name"]."<br/>"; echo $dept["id"]."<br/>";

        $this->addVendHQproducts($dept,$passval);
    }
}

function addVendHQproducts($dept,$pro_id) {
     $this->db->query("INSERT INTO " . DB_PREFIX . "vendhq_product SET id = '" . $pro_id  . "', vendhq_id = '" . $dept["id"]. "', name = '" . $dept["name"]. "', description = '" . $dept["description"]. "',image = '" . $dept["image"] . "', image_large = '" . $dept["image"]. "', tag = '" . $dept["tags"]. "', price = '" . $dept["price"] . "', supplier_name = '" . $dept["supplier_name"] ."'");
}

?> 输出:

Product 0
.........1
 ...........3..

Fatal error: Uncaught exception 'ErrorException' with message 'Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's server</p>',image = 'http://mohamedkaremullasha.vendhq.com/images/placeholder/' at line 1<br />Error No: 1064<br />INSERT INTO oc_vendhq_product SET id = '5', vendhq_id = 'bbeef777-9ac0-11e3-a0f5-b8ca3a64f8f4', name = 'TradeSender', description = '<p>trade sender iphone and ipod application to receive instant updates from ambibroker's server</p>',image = 'http://mohamedkaremullasha.vendhq.com/images/placeholder/product/no-image-white-thumb.png', image_large = 'http://mohamedkaremullasha.vendhq.com/images/placeholder/product/no-image-white-thumb.png', tag = 'share market, share updates', price = '100', supplier_name = 'Hibrise Tech -Suppliers'' in /Applications/XAMPP/xamppfiles/htdocs/mks/opencart-1.5.6.1/upload/system/database/mysqli.php:40 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/mks/opencart-1.5.6.1/uplo in 

/Applications/XAMPP/xamppfiles/htdocs/mks/opencart-1.5.6.1/upload/system/database/mysqli.php 第 40 行

//请告诉我如何处理php中的错误异常

【问题讨论】:

  • 你应该把你的专栏的别名改成一个好听的
  • 可能是错字@ShankarDamodaran 也许 OP 的意思是“计数”哈哈
  • 我收回 @ShankarDamodaran 这不是一个错字。
  • @Fred-ii- 我不这么认为,错别字可能不会出现两次。 OP可能甚至不知道这意味着什么。 @ OP 接受我的两分钱建议,并真正改变这个别名。
  • 确实,看完第 4 行后,是一个死的让步 @ICanHasCheezburger (疲倦的眼睛)

标签: php mysql phpmyadmin opencart


【解决方案1】:

如下更改您的INSERT 查询:

$this->db->query("INSERT INTO " . DB_PREFIX . "vendhq_product SET id = '" .(int)$pro_id  . "', vendhq_id = '" . (int)$dept["id"]. "', name = '" . $this->db->escape($dept["name"]). "', description = '" . $this->db->escape($dept["description"]). "',image = '" . $this->db->escape($dept["image"]) . "', image_large = '" . $this->db->escape($dept["image"]). "', tag = '" . $this->db->escape($dept["tags"]). "', price = '" . $dept["price"] . "', supplier_name = '" . $this->db->escape($dept["supplier_name"]) ."'");

祝你有美好的一天!

【讨论】:

    【解决方案2】:

    您需要确保您的内容正确转义

    'trade sender iphone and ipod application to receive instant updates from ambibroker's server' 
    

    由于ambibroker's 中的' 会导致问题

    使用mysqli_real_escape_stringmysql_real_escape_string php 函数

    【讨论】:

      【解决方案3】:

      在“from ambibroker's”的描述中有一个单引号导致查询混乱。

      【讨论】:

        【解决方案4】:

        正如您的其他值所指出的那样,其值中有'。因此您需要在插入之前对其进行转义

        将值插入数据库时​​,最好使用mysqli_real_escape_stringmysql_real_escape_string。因此,请将此函数用于您用于插入 DB 的所有变量

        喜欢这个

        $this->db->query("INSERT INTO " . DB_PREFIX . "vendhq_product SET id = '" . mysql_real_escape_string($pro_id) . "', vendhq_id = '" . mysql_real_escape_string($dept["id"]). "', name = '" . mysql_real_escape_string($dept["name"]). "', description = '" .mysql_real_escape_string( $dept["description"]). "',image = '" . mysql_real_escape_string($dept["image"]) . "', image_large = '" . mysql_real_escape_string($dept["image"]). "', tag = '" . mysql_real_escape_string($dept["tags"]). "', price = '" . mysql_real_escape_string($dept["price"] ). "', supplier_name = '" . mysql_real_escape_string($dept["supplier_name"]) ."'"); }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-09-06
          • 2015-03-16
          • 2014-05-11
          • 1970-01-01
          • 2017-09-26
          • 1970-01-01
          • 2013-02-03
          相关资源
          最近更新 更多