2017-02-09 109 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

相關問題