简体   繁体   English

如何使用 mysqli 和 javascript 更新我的数据库

[英]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.

相关问题 如何使用数据库中正在更改的新值更新javascript变量? - How do I update javascript variables with new values which are changing in my database? 如何使弹出日期选择器日历javascript更改其旁边的文本日期并更新数据库? - How do I make my popup date picker calendar javascript change the text date next to it and update the database? 如何使用更新的mysql数据库信息更新javascript中的php变量? - How do I update my php variables in javascript with updated mysql database information? 从 PHP 中的 MySQLI 数据库中获取特定单词时,如何设置特定单词的样式 - How do I style specific words when it gets fetched from my MySQLI database in PHP 如何在实时数据库中更新项目中的字段? - How do I update field in my item in realtime database? 如何使用基本javascript更新任务列表? - How do I update my task list with basic javascript? 如何使用数据库中的更新来更新UI - How do i update the UI with an update in the database 如何通过Javascript更新数据库,然后重新加载 - How do update a database through Javascript, then reload Javascript-如何将用于查询数据库的诺言将变量发送到前端? - Javascript - How do I send my variable to my frontend from the promise I use to query the database? 如何在预先输入的Bootstrap中使用通过MySQLi从数据库中获取的数据? - How can I use the data I get from my database using MySQLi, in Bootstrap typeahead?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM