douqiao8370 2014-12-22 16:34
浏览 72

AJAX将多个变量(包括JSON)发送到php以进行数据库插入

I am trying to send two variables to my PHP file to insert them into a database. One variable is called 'skuLabel', which is just text and the other is 'jsonString', which is a JSON representation of a canvas design. When I just try to send the 'skuLabel', it works fine and inserts it like I would expect. But, when I send either just the 'jsonString' or both, my PHP errors that the two indexes do not exist when I try to access them. I've been working on this for several days now, trying many different methods, all with the same result. This is my first attempt at using AJAX, so I am sure I am doing something simple wrong. I appreciate any help anyone can give!

Here is my code:

var skuLabel = document.getElementById('sku').value;
var jsonString = JSON.stringify(canvas.toDatalessJSON());
$.ajax({
    type:   'POST',
    url:    'uploadDesign.php', 
    dataType: 'json',
    data:   {sku: skuLabel, json: jsonString},
    success: 
            function(data){
                alert(data);
            }
});

And PHP:

$sku = $_POST['sku'];
$jsonData = $_POST['json'];
if($mysqli->query("INSERT INTO designs (sku, jsonData) VALUES($sku, $jsonData)")===TRUE){
    printf("Inserted successfully.
");
}
  • 写回答

1条回答 默认 最新

  • doudula1974 2014-12-22 17:05
    关注
    if($stmt = $mysqli->prepare("INSERT INTO designs (sku, jsonData) VALUES(?, ?)")){
        $stmt->bind_param('ss', $sku, $jsonData);
        $stmt->execute();
        $stmt->close();
    }
    

    This fixed it. Thanks, Quentin!

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程