[英]How do I update my database with mysqli and javascript
I have a website which basically is an audioplayer and an integrated lyricviewer on screen, which the user should be able to sync with the music they hear playing.我有一个网站,它基本上是一个音频播放器和一个屏幕上的集成歌词查看器,用户应该能够与他们听到的音乐同步。 I only have one problem, and that is;
我只有一个问题,那就是; How on earth do I, from a javascript function, call a mysqli update statement?
我到底如何从 javascript 函数调用 mysqli 更新语句? When the user clicks a save button, content gets thrown into a div, which I want the PHP after the JavaScript has been run to take that content and put it into a database.
当用户单击保存按钮时,内容被扔到一个 div 中,我希望在运行 JavaScript 后 PHP 获取该内容并将其放入数据库中。
What is the best way to do that?最好的方法是什么?
Why doesn't this work?为什么这不起作用?
function saveinPHP() {
//alert("Came here");
//var superstr = $( "#savelyric" ).text();
var superstr = 'lol';
$.ajax({
type: "POST",
url: "includes/sendlyrics.php",
data: superstr,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert("---"+data);
alert("Settings has been updated successfully." + data + "~~~" + superstr);
//window.location.reload(true);
}
});
}
And then the PHP:然后是PHP:
<?php
include ('db_connect.php');
$data = $_POST['data'];
$query = "UPDATE song SET time=". $data ." WHERE id='1'";
mysqli_query($query);
?>
Write PHP in a totally separate dedicated file that takes POST variables, constructs an SQL query, and inserts them into a database.将 PHP 编写在一个完全独立的专用文件中,该文件采用 POST 变量、构造 SQL 查询并将它们插入到数据库中。 Then have your JavaScript function send the data to this PHP file using a POST request.
然后让您的 JavaScript 函数使用 POST 请求将数据发送到此 PHP 文件。
JavaScript in the browser cannot interact with the database.浏览器中的 JavaScript 无法与数据库交互。 It can only send GET/POST requests to the server which can catch those requests and put the attached data into the database.
它只能向可以捕获这些请求并将附加数据放入数据库的服务器发送 GET/POST 请求。
First , when you specify a string as data
, jQuery will send it as is.首先,当您指定一个字符串作为
data
,jQuery 将按原样发送它。
The string you are using "lol"
is not formatted in either of the standard formats for POST data that are understood by PHP.您使用的字符串
"lol"
没有以 PHP 能够理解的 POST 数据的任何一种标准格式进行格式化。
$_POST
, therefore, has no data in it.因此,
$_POST
中没有数据。
Pass jQuery an object instead:改为向 jQuery 传递一个对象:
data: { data: superstr }
Second , false
is not a content-type of either of the standard formats mentioned above.其次,
false
不是上述任何一种标准格式的内容类型。 jQuery will use an appropriate content-type by default.默认情况下,jQuery 将使用适当的内容类型。 Remove this override:
删除此覆盖:
contentType: false,
Third , processData: false,
will break the conversion of the object into form encoded data.第三,
processData: false,
将对象转换为表单编码数据。 Remove it.去掉它。
Fourth , strings in SQL must be quoted.第四,SQL 中的字符串必须被引用。 You aren't quoting
data
.你没有引用
data
。
$query = "UPDATE song SET time='$data' WHERE id='1'";
Note this is still vulnerable to SQL injection and you should fix that .请注意,这仍然容易受到 SQL 注入的影响,您应该修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.