使用按钮运行更新


Running an update using a button

我正在开发一个php/mysql系统。我有一个表juncfees有字段- juncid,物质,工作人员,费用。

按小时收费。

matterid指的是事项表,staffid指的是员工表,但我认为这与我想要实现的目标无关。

每年的费用将受到每年上涨,我想要能够做的是有一个页面,使管理员做到这一点,而不涉及我。在过去,我使用如下代码更改了值:UPDATE juncfees SET fee= ' 45 ' WHERE staffid= ' 5 ';这工作得很好,但我宁愿其他人可以在没有直接数据库访问的情况下做到这一点。

我在想象一个页面,那里有一个下拉列表的工作人员给我的工作人员和一个框,新的费用可以输入,然后一个按钮,一旦点击,将更新所有相关数据。

这是可能的吗?如果是,我该怎么做?(当然,如果有更好的方法,我很乐意遵循这条路线。)

多谢

这是一个改编自w3的示例。

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "UPDATE juncfees SET fee='".$_POST['fee']."' WHERE staffid='".$_POST['staffid']."'";
if ($conn->query($sql) === TRUE) {
    echo "Records updated successfully";
} else {
    echo "Error updating records: " . $conn->error;
}
$conn->close();
?>

HTML只是一个简单的表单:

<form action="update.php" method="post">
StaffID: <input type=text name=staffid><br>
Fee: <input type=text name=fee><br>
<input type=submit>
</form>

HTML表单只是一个例子,实际上任何可以发布http数据的标记或语言都可以触发PHP代码(这几乎是所有的)。因此,如果你想创建一个桌面应用程序来实现这一点,这将是相当容易的。

using System.Net;
function postData(float fee, int id)
{
    var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/update.php");
    var postData = "fee="+fee;
        postData += "&staffid="+id;
    var data = Encoding.ASCII.GetBytes(postData);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    using (var stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }
    var response = (HttpWebResponse)request.GetResponse();
    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}

请记住,这只是最基本的示例,对于用户访问或SQL注入没有保护。