我对PHP脚本有以下jQuery ajax调用:actualHtml = $('div').html(); // could this line be causing an issue?$.ajax({ type: 'POST', url: 'save-html-css-action.php', data: { 'htmlTextToSave': htmlTextToSave, 'actualHtml': actualHtml, 'userId':userId }, success: function(msg){ alert(msg); }});的PHP:$htmlCssToSave = $_POST['htmlTextToSave'];$userId = $_POST['userId'];$actualHtml = $_POST['actualHtml'];$mysqli = new mysqli($servername, $sqlusername, $sqlpassword, $dbname);/* check connection */if (mysqli_connect_errno()) { //printf("Connect failed: %s\n", mysqli_connect_error()); echo "Connection failed: ".mysqli_connect_error(); exit();} $mysqli->query("INSERT INTO user_saved_data (user_html_css_code, dd_id, actual_html) values ('".$htmlCssToSave."',".$userId.",'".$actualHtml."')"); echo "success"; /* close connection */ $mysqli->close();但是当我检查数据库时,数据不存在。我在jquery/php组合中做错了什么吗(意味着ajax调用)?我收到了一个JavaScript“成功”警报,因此它正在命中脚本,但是我不确定为什么未插入信息。表数据类型是htmlcsstosave和actualhtml列的中间文本,而userid是int(不是主键,这是另一个表的外键)所以我添加了一个错误警报,这是输出      您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册,以在'margin-0'附近使用正确的语法>                                                                                     (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 正如我们所讨论的,问题在于转义和清理数据。如果您使用准备好的语句,它将为您处理。让我们看看它如何工作:准备语句:$stmt = $mysqli->prepare("INSERT INTO user_saved_data (user_html_css_code, dd_id, actual_html) values (?,?,?)");绑定您的参数:$stmt->bind_param('sis', $htmlCssToSave, $userId, $actualHtml);然后执行您的语句:$stmt->execute();那你应该很好。准备好的语句现在应该为您处理数据清理。资源:mysqli preparemysqli bind_parammysqli execute (adsbygoogle = window.adsbygoogle || []).push({});
08-05 07:02