[英]Is it possible to update data in my SQL database with 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). 我开始用JavaScript学习一些SQL,我想将变量(“ Val_Points from javascript”)放入用户(例如:Robert)的表(“ Usuarios”)中。 It's this posible via Javascript or there are some other methods ? 通过Javascript可以做到这一点,还是有其他一些方法?
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>
Also i was seeing a script that simplifly this https://hiddentao.github.io/squel/ Maybe could be more readable in scripting with this. 我也看到了一个脚本,可以简化此https://hiddentao.github.io/squel/也许在编写脚本时可能更具可读性。 Any help? 有什么帮助吗?
EDIT 编辑
I tried to do all of this with php via $_SESSION 我试图通过$ _SESSION用php完成所有这些操作
<?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 但它不会更新表,该表保持相同的值
Username Points 用户名积分
Robert 0 罗伯特0
Is it possible to insert data with Javascript to an SQL database 是否可以将使用Javascript的数据插入SQL数据库
Simple answer is no. 简单的答案是没有。 Not just with Javascript. 不只是使用Javascript。
For understanding this you need to know the difference between client-side
and server-side
. 为了理解这一点,您需要了解client-side
和server-side
client-side
之间的区别。
Client-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. 这也意味着客户可以随意修改数据。
Server-side 服务器端
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. 接收数据后,客户端无法对其进行修改。
Your database is on the server-side
its on your server. 数据库位于server-side
位于服务器端。 The client has no direct acces to this database, so you need to send the data from the client-side
to the server-side
. 客户端对此数据库没有直接访问权限,因此您需要将数据从client-side
发送到server-side
。 This can be done with javascript
. 这可以用javascript
完成。
So how to do this. 那么如何做到这一点。
In your case you wanna combine Javascript
(client) with PHP
(server). 在您的情况下,您想将Javascript
(客户端)与PHP
(服务器)结合在一起。 Via PHP
we insert the data to your SQL
database. 通过PHP
我们将数据插入到您的SQL
数据库中。
Example 例
Javascript / Jquery(!) Javascript / Jquery(!)
We going to use $.ajax() . 我们将使用$ .ajax() 。 This is an function out of the jQuery framework and makes an request to your server. 这是jQuery框架之外的功能,向您的服务器发出请求。
$('.btn').click(function() {
var request = $.ajax({
url: "phpfile.php",
data: { label: "value" },
method: "POST"
});
request.done(function() {
// Do something after its done.
});
});
PHP 的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']
}
?>
"Standard" approach “标准”方法
foo.php
is the site which contains a button and includes a script: foo.php
是包含按钮和脚本的站点:
<button id="update-btn">update</button>
...
<script src="foo.js"></script>
foo.js
adds the on click handler: foo.js
添加点击处理程序:
$('#update-btn').click(function() {
$.ajax({
'url': 'fooHandler.php',
'method': 'POST',
'data': {'sid': ..., 'action': 'update', 'value': ...}
/* 'success': function(data) { ... } */
/* 'error': function ... */
});
});
fooHandler.php
handles requests to the server from the clients: fooHandler.php
处理来自客户端的对服务器的请求:
<?php
if (isset($_POST['sid'], $_POST['action'])) {
/* TODO check if valid session */
if ($_POST['action'] == 'update') {
/* check if the rest is in order */
if (isset($_POST['value'] and is_numeric($_POST['value'])) {
/* TODO execute query */
}
} elseif ($_POST['action'] == ...) {
...
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.