如何更新 SQL 数据库中的数据 [英] How to update data in an SQL database

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

问题描述

我已经构建了一个功能正常的登录页面,它将用户重定向到 index.php 文件.根据之前的帮助,我能够根据登录的用户获取工资并将其显示在页面上.这是数据库user_registration

users>

user_id username 密码 email 工资1 约翰史密斯 jsmith99 jsmith@gmail.com 1002 davidscott dscott95 davidscott@gmail.com 90

我坚持的部分是创建一个功能表单,用户可以将他们的工资更新到 sql 数据库.

有人可以帮我处理 php 代码吗?这是我已经准备好的表格:

<input type="text" id="new_wage" name="new_wage"><input type="button" value="保存"></表单>

这是代码,它的目的是用户可以通过填写文本框并按提交来更新表格中的工资值.我有什么想法可以做到这一点吗?>

<?php//改变工资$username = '$_SESSION['MM_Username'];';if (isset($_POST['submit'])){$wage = $_POST['wage-new'];//连接到服务器mysql_connect ("localhost","root","") 或死 ("无法连接");mysql_select_db("user_registration") 或 die ("无法连接到数据库");mysql_query ("UPDATE users SET wage='$wage' WHERE username = '$username'") or die ("Could not update");}?>

解决方案

除非你像前面的评论员所说的那样演示,否则我不会给你代码.不过,我会给您一个概述,以便您可以自行处理.

更新.php

<前>检查您是否已登录.如果为真,继续.从表格中获取新工资$new_wage = $_POST['new_wage'];请务必验证并清除 $new_wage 变量.下一阶段假设您使用 PDO$params = array($new_wage, $logged_in_user_id);$update = "更新 user_registration SET 工资=?WHERE user_id=?";$pdo->准备($更新);$pdo->execute($params);

I have already build a functioning login page which redirects the user the index.php file. From previous help, I was able to get the wage and display it on the page depending on which user logs in. This is the table users in the database user_registration

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

The part i am stuck on is creating a functioning form that the user can update their wage to the sql database.

Can someone please help me with the php code? This is the form i already have in place:

<form id="change-wage" action="update.php" method="post">
  <input type="text" id="new_wage" name="new_wage">
  <input type="button" value="Save">
</form>

EDIT: this is the code, The aim of it is that the user can update the wage value in the table by filling in the textbox and pressing submit. any Ideas how i can acieve this?>

<?php  //CHANGING THE WAGE
$username = '$_SESSION['MM_Username'];';
if (isset($_POST['submit'])){
        $wage = $_POST['wage-new'];
        //connect to server
        mysql_connect ("localhost","root","") or die ("Could not connect");
        mysql_select_db("user_registration") or die ("Could not connect to the database");

    mysql_query ("UPDATE users SET wage='$wage' WHERE username = '$username'")     or die ("Could not update");
}


    ?>

解决方案

I wont give you the code unless you demonstrate as previous commentator said. However I will give you a an overview so you can work at it your self.

update.php

Check if your is logged in.

if TRUE, continue.

get the new wage from the form

$new_wage = $_POST['new_wage'];

Be sure to validate and clean the $new_wage variable.

Next stage assumes your using PDO

$params = array($new_wage, $logged_in_user_id);

$update = "UPDATE user_registration SET wage=? WHERE user_id=?";

$pdo->prepare($update);
$pdo->execute($params);

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

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