简体   繁体   English

当我将javascript变量存储在mysql中时,如何将其链接到当前用户的ID?

[英]When I store a javascript variable in mysql, how does it get linked to the current user's id?

Let's say there is a site that has a button that uses javascript to increase the value of the javascript variable "javascriptx" by +1 each time it is clicked. 假设有一个站点带有一个按钮,该按钮每次使用时都使用javascript将javascript变量“ javascriptx”的值增加+1。 UserA (id=1 on the MySql table) logs in to the website and presses the button 3 times, so javascriptx = 2. UserA(MySql表上的id = 1)登录到网站并按3次按钮,因此javascriptx = 2。

The next time UserA logs in, I want javascriptx to still equal 2 (assuming the button hasn't been pressed any more times), so I need to convert that javascript variable to a php variable and send it to MySql. 下次UserA登录时,我希望javascriptx仍等于2(假设尚未再次按下按钮),因此我需要将该javascript变量转换为php变量并将其发送到MySql。 As far as I understand, this is the correct way to do that: 据我了解,这是正确的方法:

Add the following to the javascript code: 将以下内容添加到javascript代码中:

var xhr = new XMLHttpRequest();
xhr.open("get","/path/to/php/page.php?phpx=javascriptx");
xhr.send();

Now, the php variable "phpx" has the same value as the javascript variable "javascriptx" 现在,php变量“ phpx”具有与javascript变量“ javascriptx”相同的值

Then, that php variable needs to be sent to the MySql table, so the following is added to the php code: 然后,该php变量需要发送到MySql表,因此将以下内容添加到php代码中:

($_GET['phpx'])

From here, I get lost. 从这里,我迷路了。 Where does phpx get stored in the MySql table? phpx哪里存储在MySql表中? More specifically, how does it know to put this in the same row as user with id=1? 更具体地说,如何知道将其与id = 1的用户放在同一行?

You should connect to the MySQL database using something like mysqli : 您应该使用mysqli之类的东西连接到MySQL数据库:

$database = new mysqli(db_host, db_username, db_pass, db_name);

Then you can make queries to your database with prepared statements to read/write data. 然后,您可以使用准备好的语句对数据库进行查询以读取/写入数据。 In this rough example, the data will return a JSON response to your web application or wherever you make the request from: 在此粗略示例中,数据将返回JSON响应到您的Web应用程序,或者无论您从何处发出请求:

//set user id from post variable
$uid = $_POST['uid'];

//store sql query as string
$sql = "SELECT phpx FROM users WHERE uid=?";

//initiate new prepared statement
$stmt = $database->stmt_init();
$stmt->prepare($sql);

//pass uid var to statement
$stmt->bind_param('s', $uid);

//store result as var
$stmt->bind_result($phpx);

$stmt->execute();

//catch errors
if ($stmt->errno) {
    die("error: " . $stmt->error);
} else {

//store result and send as response
    $stmt->store_result();
    if($stmt->num_rows){
        while($stmt->fetch()){
            $phpx_packet[] = array(
                "uid" => $uid,
                "phpx" => $phpx
            );
        }
        echo json_encode($phpx_packet);
    }
}

//close statement
$stmt->close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:如何在 javascript 中访问当前登录用户的 ID? - Django : How to access current logged in user's id in javascript? Angular 如何存储未链接到变量的 observable? - How does Angular store an observable not linked to a variable? 如何使用 PHP 和/或 JavaScript 将用户的分数存储在我网站上的变量中? - How can I store a user's score in a variable on my website using PHP and/or JavaScript? Shopify:在 JavaScript 中获取当前登录的用户 ID - Shopify: get current logged in user ID in JavaScript 如何在 Javascript 的 Keycloak 中获取当前登录用户的密码? - How can I get the current logged in user's password in Keycloak in Javascript? 如何在我的软删除组件的 SoftCrudRepository 方法中获取当前用户或用户 ID? - How can I get current user or user-id in my soft-delete-component's SoftCrudRepository method? 如何更好地在Loopback中获取当前用户ID - How do I better get current user id in Loopback 将当前页面的php变量传递到XMLHttpRequest2(即$ user_id) - Pass a current page's php variable through to XMLHttpRequest2 (i.e. $user_id) 当我尝试对它们进行排序时,如何在javascript中获取当前ul li的长度? - How to get the current ul li's length in javascript , when i tried to sort them? 如何获取javascript变量的当前DNN页面名称? - How do I get current DNN page name for javascript variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM