javascript - 无法将值更新为字符串

标签 javascript php jquery mysql

当我将字符串插入输入时,更新值时出现问题。我要更新的列“位置”是一种数据类型: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>

最佳答案

在 SQL 语句中,字符串值必须像这样用引号括起来

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

注意 %s

周围的单引号

您可以在所有数据值周围放置引号,但必须在文本数据类型周围加上引号。

关于javascript - 无法将值更新为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137934/

相关文章:

javascript - 如何在javascript中删除对象文件或数组文件?

javascript - 保持内容居中并允许在 d3 中平移/缩放

javascript - asp.net mvc 使用 Javascript 渲染局部 View

jquery - 向 jquery 插件添加方法的最佳方法

javascript - jquery 对象调用自身内部的函数

javascript - 传单:使用鼠标悬停和不透明度会产生冲突

php - 检查一天字段中两个日期之间的可用性

php - 一些 php mysql 错误

php - mysql,如何从数据库中删除选定的数据?

javascript - 如何保存部分网址中的字符串?