应该发生什么
发生了什么
问题在哪里
我的问题示例
field1 = 123
field2 = 321
查询:成功
field1 = 123 a
field2 = 321 a
查询:失败
Ajax代码
<script text="text/javascript">
$('#button_save').click(function(){
var edited_message_id = document.getElementById('id_message_hidden').value;
var edited_title = document.getElementById('user_event_title').value;
var edited_message = document.getElementById('user_message_input').value;
alert(edited_title.concat(edited_message));
if(confirm("Deseja editar esta mensagem?")){
$.ajax({
url: 'edit.php',
type: 'post',
data: {idmensagem:edited_message_id,
newtitle:edited_title,
newmessage:edited_message,}
});
};
return false;
});
</script>
edit.php
<?php
session_start();
include 'connect.php';
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(!isset($_SESSION['pass'])){ //if login in session is not set
header("Location:index.php");
}
$message_id = $_POST['idmensagem'];
$new_title = $_POST['newtitle'];
$new_message = $_POST['newmessage'];
$query_update = mysqli_query($con, "UPDATE tbl_mensagens SET title_text = $new_title, txt_mensagem = $new_message WHERE id_mensagem = $message_id");
?>
答案 0 :(得分:1)
您需要在SQL中将字符串放在引号中:
$query_update = mysqli_query($con, "UPDATE tbl_mensagens SET title_text = '$new_title', txt_mensagem = '$new_message' WHERE id_mensagem = $message_id");
但最好使用预准备语句,以避免SQL注入问题。
$query_update = mysqli_prepare($con, "UPDATE tbl_mensagens SET title_text = ?, txt_mensagem = ? WHERE id_mensagem = ?");
mysqli_stmt_bind_param($query_update, "ssi", $new_title, $new_message, $message_id);
mysqli_stmt_execute($query_update);