是否可以使用Javascript更新我的SQL数据库中的数据? [英] Is it possible to update data in my SQL database with Javascript?

查看:96
本文介绍了是否可以使用Javascript更新我的SQL数据库中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用javascript学习一些SQL,我想将变量(来自javascript的Val_Points")放入用户(例如:Robert)的表("Usuarios")中.通过Javascript可以做到这一点,或者还有其他一些方法吗?

I'm starting to learn some of SQL with javascript and i want to put my variable ("Val_Points from javascript") into a table ("Usuarios") of a user (ex : Robert). It's this posible via Javascript or there are some other methods ?

var Val_Points = 0; /* Variable */
$('.btn').click(function() {
  Val_Points += 5; /* value +5 */
  /* Update individual SQL Data
  var UpdateSQL = dbConn.executeUpdate('UPDATE Usuarios SET Points="$Val_Points" WHERE Username="$_SESSION[username]"'); 
*/
  $('#exp_bar').val(Val_Points);
});

<?php
	session_start();
	require 'connect.php';
$username_session = $_SESSION['username']; /* "Robert" Session */
$query = "SELECT * FROM `Usuarios` WHERE usuario='$username_session'";
$result = mysqli_query($connection, $query);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Points</p>
<progress id ="exp_bar" value="0" max="100"></progress><br>
<button class="btn">+1</button>

我还看到了一个脚本,可以简化 https://hiddentao.github.io/squel/也许在使用脚本编写脚本时更具可读性.有帮助吗?

Also i was seeing a script that simplifly this https://hiddentao.github.io/squel/ Maybe could be more readable in scripting with this. Any help?

编辑

我试图通过$ _SESSION用php完成所有这些操作

I tried to do all of this with php via $_SESSION

<?php
session_start();
require 'connect.php';
$username_session = $_SESSION['username'];
$query = "SELECT * FROM `Usuarios` WHERE username='$username_session'";
$result = mysqli_query($connection, $query);

if(empty($_SESSION['Val_Points'])){

    $Val_Points_Session = $_SESSION['Val_Points'] = 0; 
}


 echo "<form action='' method='POST'>
   <progress id ='exp_bar' value='".$_SESSION['Val_Points']++."' max='100'></progress>
   <input class='btn' type='submit'  value='Increase val' /> 
 </form>";

 echo $_SESSION['Val_Points']; /* Show current variable */
$Update_SQL = "UPDATE Usuarios SET Points='$Val_Points_Session' WHERE username='$username_session'";

?>

但是它不会更新表,该表保持相同的值

But it doesn't update the table, the table keeps with the same value

用户名积分
罗伯特0

Username Points
Robert 0

推荐答案

是否可以将带有Javascript的数据插入SQL数据库

简单的答案是否定的.不是公正使用Javascript.

Simple answer is no. Not just with Javascript.

要了解这一点,您需要了解client-sideserver-side之间的区别.

For understanding this you need to know the difference between client-side and server-side.

客户端

客户端执行的操作.该操作由用户的计算机/软件进行.这也意味着客户可以随意修改数据.

Operations that are performed by the client. The operations are made by the computer/software of the user. This also means the client can modify the data how ever he want.

服务器端

服务器执行的操作.这些操作在服务器上进行,仅使用客户端发送的信息.接收到数据后,客户端无法对其进行修改.

Operations that are performed by the server. The operations are made on an server and uses just the information that is send by the client. After the data is recived the client can not modify it.

您的数据库位于服务器上的server-side中.客户端对此数据库没有直接访问权限,因此您需要将数据从client-side发送到server-side.这可以通过javascript完成.

Your database is on the server-side its on your server. The client has no direct acces to this database, so you need to send the data from the client-side to the server-side. This can be done with javascript.

那要怎么做.

在您的情况下,您想将Javascript(客户端)与PHP(服务器)组合在一起.通过PHP,我们将数据插入到您的SQL数据库中.

In your case you wanna combine Javascript (client) with PHP (server). Via PHP we insert the data to your SQL database.

示例

Javascript/Jquery(!)

我们将使用 $ .ajax().这是jQuery框架之外的功能,并向您的服务器发出请求.

We going to use $.ajax(). This is an function out of the jQuery framework and makes an request to your server.

$('.btn').click(function() {
    var request = $.ajax({
        url: "phpfile.php",
        data: { label: "value" },
        method: "POST"
    });

    request.done(function() {
        // Do something after its done.
    });
 });

PHP

<?php

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      // do your database stuff.
      // the data you send from the client is in the $_POST array.
      // for example $_POST['label']
    }

?>

这篇关于是否可以使用Javascript更新我的SQL数据库中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆