简体   繁体   English

更新SQL表中的数据

[英]Updating data in an SQL table

I have created a webpage that gets the user to login, if they havent got an account then they can register and is creates a new user for them. 我创建了一个让用户登录的网页,如果他们没有帐户,则可以注册并为他们创建一个新用户。

This is the table users in the database ( user_registration ) 这是数据库中的表用户user_registration

    user_id    username   password    email                 wage
    1          johnsmith  jsmith99    jsmith@gmail.com      0
    2          davidscott dscott95    davidscott@gmail.com  0

When a new user registered, the default value for the wage is 0. I want them to be able to edit this through the use of a form - this is the HTML code for the form:- 当一个新用户注册时,工资的默认值为0。我希望他们能够通过使用表单来编辑它- 这是表单的HTML代码:-

<form method="post" action="<?php $_PHP_SELF ?>">
  <input name="new-wage" type="text" id="new-wage" class="textbox" placeholder="New Wage">
  <input name="update" type="submit" id="update" value="Update" class="btn">
</form>

PHP Code: (note- the php and the html form are all in the same file(index.php) ) PHP代码:(请注意-php和html表单都在同一个文件中(index.php))

 <?php
 if(isset($_POST['update']))
 {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
   die('Could not connect: ' . mysql_error());
}

$user_id = $_SESSION['MM_Username'];;
$wage = $_POST['new-wage'];

$query = "UPDATE users ".
       "SET wage = '$wage' ".
       "WHERE user_id = '$user_id'" ;

mysql_select_db('user_registration');
$retval = mysql_query( $query, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
?>

When i fill in this form and hit the update button, it reloads the webpage and displays Updated data successfully which is exactly what should happen. 当我填写此表单并单击“更新”按钮时,它将重新加载网页并成功显示更新的数据 ,这正是应该发生的情况。 BUT - when I refresh the table in PHP My Admin, it keeps the wage as 0, and not what i entered in the form. 但是-当我在PHP My Admin中刷新表时,它将工资保持为0,而不是我在表格中输入的工资。

Has anyone got any ideas what might be wrong with my code? 有谁知道我的代码可能有什么问题吗?

Thanks in advance for any answers. 预先感谢您的任何答案。

PS .- I know that I have used the functions mysql_* and not mysqli_ , simply because i dont know how to convert it, can you also help me with this?? PS .-我知道我使用了mysql_ *而不是mysqli_函数 ,只是因为我不知道如何转换它,您还能帮我吗?

You are vulnerable to sql injection attacks . 您容易受到sql注入攻击的攻击

Plus, you don't seem to call session_start() so $_SESSION is probably empty, leading to $user_id being empty, and your query not matching anything. 另外,您似乎没有调用session_start()因此$_SESSION可能为空,导致$user_id为空,并且您的查询不匹配任何内容。 A simple var_dump($query) will show you what's actually going on there. 一个简单的var_dump($query)将向您显示实际发生的情况。

You need to check mysql_affected_rows() as well, to see if anything actually got updated. 您还需要检查mysql_affected_rows() ,以查看是否有任何实际更新。

Based on what you said in the comments: 根据您在评论中所说:

This: 这个:

$user_id = $_SESSION['MM_Username'];
$wage = $_POST['new-wage'];

$query = "UPDATE users ".
       "SET wage = '$wage' ".
       "WHERE user_id = '$user_id'" ;

Should be this: 应该是这样的:

$user = $_SESSION['MM_Username'];
$wage = $_POST['new-wage'];

$query = "UPDATE users ".
       "SET wage = '$wage' ".
       "WHERE username = '$user'";

As you wanted to update the records based on the user id but you passed in the username as the parameter! 您想根据用户ID更新记录,但是您将用户名作为参数传递了!

However, I'm assuming you want to update by the user_id , as that is unique for each user, and thus a more robust design. 但是,我假设您要通过user_id进行更新,因为这对于每个用户来说都是唯一的,因此设计更加健壮。

To do that you'd have to add another $_SESSION variable that stores the user id when they log in. Call it $_SESSION['MM_Id'] or whatever. 为此,您必须添加另一个$_SESSION变量来存储用户登录时的用户ID 。将其$_SESSION['MM_Id']或其他名称。 The name is arbitrary. 名称是任意的。

You also have an unclosed else statement at the end of your code: 您在代码末尾还有一个未封闭的else语句:

else
{
?>

Either close it or remove it. 关闭或移除它。

As Mark said, your code is susceptible to SQL injection attacks, a commonly abused mistake. 正如Mark所言,您的代码容易受到SQL注入攻击的攻击,这是一种常见的错误。 I'd recommend looking at PDO (my personal favorite). 我建议您查看PDO (我个人最喜欢的)。 If you're new to object-oriented programming, it may be difficult, but it offers more power, flexibility, and security. 如果您不熟悉面向对象的编程,可能会很困难,但是它提供了更多的功能,灵活性和安全性。 Plus, object-oriented programming is an important concept anyway . 另外, 无论如何 ,面向对象编程是一个重要的概念。

To protect your current code from injection, use mysql_real_escape_string() ... 为了防止当前代码被注入,请使用mysql_real_escape_string() ...

$user = mysql_real_escape_string($_SESSION['MM_Username']);
$wage = mysql_real_escape_string($_POST['new-wage']);

$query = "UPDATE users ".
       "SET wage = '$wage' ".
       "WHERE username = '$user'";

This prevents people form putting in special characters (such as quotes) to attack your queries. 这样可以防止人们使用特殊字符(例如引号)来攻击您的查询。 I gotta run, but if you read up on SQL injection, you'll understand! 我一定要跑,但是如果您读了有关SQL注入的书,您会明白的!

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

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