简体   繁体   English

我需要帮助来更新特定用户的sql条目

[英]I need help updating sql entry for a specific user

So here is what I'm having problems with... I'll explain the table layout. 所以这是我遇到的问题...我将解释表的布局。

I have a table called "reg_users" 我有一个名为“ reg_users”的表

inside this table i have "email" "uuid" "token" etc.... 在此表中,我有“电子邮件”,“ uuid”,“令牌”等。

When the user creates an account, it posts a row to the reg_users table containing email. 用户创建帐户时,它将在包含电子邮件的reg_users表中发布一行。 However the UUID remains blank, as I would like them to be able to update the UUID field. 但是,UUID保持空白,因为我希望他们能够更新UUID字段。

Using the script below, I can replace the uuid and update it - but it only works if the uuid field isnt empty. 使用下面的脚本,我可以替换uuid并对其进行更新-但它仅在uuid字段不为空时才起作用。

      <?php
require_once 'includes/main.php';

$user = new User();

if(!$user->loggedIn()){
    redirect('index.php');
}


 mysql_connect("sqlhost", "myusername", "password") or die(mysql_error()); 
 mysql_select_db("gl1tchr") or die(mysql_error());
 $uuidold = "$user->uuid";
 // Collects data from "reg_users" table 
 $data = mysql_query("UPDATE `reg_users` SET `uuid` = replace(uuid, '$uuidold', '$newuuid')") 
 or die(mysql_error()); 
 ?>

So I need to know how to go into the table "reg_users" and find what row their email is on - and update the field "uuid" on the specific row. 因此,我需要知道如何进入表“ reg_users”并查找其电子邮件所在的行-并更新特定行上的“ uuid”字段。

Either that or a script to generate a random number in the "uuid" field for that specific user so my search and replace script will work. 在该特定用户的“ uuid”字段中生成一个随机数的脚本或脚本,这样我的搜索和替换脚本将起作用。

Thank you to anyone who can help me - I'm new to SQL. 感谢任何可以帮助我的人-我是SQL的新手。

you should be using something called a primary key and autoincrement for your UUID field, and it should not be updateable. 您应该在UUID字段中使用称为主键和自动递增的内容,并且该内容不可更新。 This is a unique ID so you can identity every row, and make updates to the row. 这是唯一的ID,因此您可以标识每一行,并对该行进行更新。

While I agree that ID numbers should not be updated after creation, and you should be using an auto-incremented key, if this were not a user ID field and was used to store something else, here's how I would do it: 虽然我同意ID号在创建后不应该更新,并且您应该使用自动递增的密钥,但是如果这不是用户ID字段并且用于存储其他内容,则可以按照以下方法进行操作:

$email = $user->email;  // or whatever you store the email in
$data = $mysql_query("UPDATE `reg_users` SET uuid = '$uuid_new' WHERE `email` = '$email'"); 

This would require knowing the email address of the user, which I think you have from your question. 这将需要知道用户的电子邮件地址,我认为您是从问题中获得的。

You may also want to check out PHP's mysqli extension , an object-oriented interface for interacting with MySQL databases. 您可能还想查看PHP的mysqli扩展 ,这是一个用于与MySQL数据库交互的面向对象的接口。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM