2017-02-09 43 views
1

当我在输入中插入字符串时,我遇到更新问题。我的列“位置”我想更新的是一个数据类型:varchar()在我的数据库中。我可以插入一个数字,如“3”,它将在数据库中更新。但是,当我插入像“SS14”字符串它不会更新数据库,我会得到一个错误。我做错了什么?不能更新值作为字符串

<?php 
try { 
    // verify request 
    if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') throw new Exception ('No Xrequest'); 


     require_once('db.php'); 

     $q= 'UPDATE prestashop.ps_product SET location = %s WHERE id_product = %d'; 

     $sql = sprintf($q, $_REQUEST['value'], $_REQUEST['id']); 

     $rs=$ib->exec($sql); 
     if (PEAR::isError($rs)) throw new Exception ("DB Request: ". $rs->getMessage()); 

     die("1"); // exit and return 1 on success 
    } catch (Exception $e) { 
     print $e->getMessage(); 
    } ' 


<td class="test" id="'.$r["product_id"].'"><font size=+2> 

<b>'.$r["product_s_desc"].'</b></font></td> 

     <script> 
    var x; 
    var h = 0; // blur fires multiple times, use h to count and fire once 
    var url = 'up.php'; 

    $(".test").on('click', function(d) { 
     d.stopPropagation(); 
     var x = $(d.target); 
     x.html('<input id="edit" style="width: 40px;" value="'+x.text()+'">'); 
     var this_id = x.context.id; 

     $("#edit").on('blur', function(d) { 
      if (h < 1) { 
       h = h+1; 
       d.stopPropagation(); 

       $(d.target.parentElement).text(d.target.value); 

       $.get(url, {id: this_id, value: d.target.value}) 
        .done(function(d){ if(d == undefined || d != 1) { alert('Something went wrong, check your data and results. '+d);}}) 
        .fail(function(d){ alert('error');}); 
       $(".edit").off('blur'); 
      } 

      return false; 
     }); 
     h = 0; 
    }); 
    </script> 
+0

该代码不是在“代码块”中的正确顺序,但我只是显示我使用的代码 – Somepub

回答

1

在一个SQL语句字符串值必须用引号引用,这样

$q= "UPDATE prestashop.ps_product SET location = '%s' WHERE id_product = %d"; 

注意周围的%s

单引号你可以把周围的所有数据值的报价,但他们围绕文本数据类型的限制。

+0

我做到了!但它仍然不会在数据库中更新,并显示一个错误 – Somepub

+0

哦,我发现这个问题,我用单一的qoutes而不是双重!谢谢 – Somepub

相关问题