php - 如果满足条件,则更新数据库中的 ID 号
问题描述
我正在寻求找出我留下的 php/sql/html 代码。如果满足某个条件,我只是想更改数据库中的数值。为了不将其标记为重复,请给出细分。
- 我看到的关于 php 和 sql 更新的教程往往表明您必须写下一个变量才能连接到数据库并从那里更新:
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 MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
- 然而,给我的代码却有所不同。它使用“公共功能”来连接数据库。这是代码工作部分的示例,其中用户的信息从通过“UpdateObj”在文本字段中键入的内容更改
更改设置:(名为“settings-profile.vc.php”)
<?php
$routePath = "../";
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
require_once($routePath . "_config/app.php");
require_once($routePath . "_lib/c/Photo_Resizer.php");
require_once($routePath . "_mc/UsrCustomerMembershipOrder.mc.php");
$mcUsrCustomerMembershipOrder = new UsrCustomerMembershipOrder_MC();
require_once($routePath . "_mc/UsrCustomer.mc.php");
$mcUsrCustomer = new UsrCustomer_MC();
require_once($routePath . "_mc/UsrCustomerProfile.mc.php");
$mcUsrCustomerProfile = new UsrCustomerProfile_MC();
require_once($routePath . "_mc/Product.mc.php");
$mcProduct = new Product_MC();
require_once($routePath . "_mc/ProductTag.mc.php");
$mcProductTag = new ProductTag_MC();
require_once($routePath . "_mc/Merchant.mc.php");
$mcMerchant = new Merchant_MC();
require_once($routePath . "_mc/ProductTagLink.mc.php");
$mcProductTagLink = new ProductTagLink_MC();
/********** Implementation **********/
// Check Session Credentials and auto-login
if ($_SESSION['usrcustomerid'] == '') {
header('Location: signout.php'); die();
}
$usrcustomerid = $_SESSION['usrcustomerid'];
$rowUsrCustomerProfile = $mcUsrCustomerProfile->SelectObj_ByUsrCustomerId($db, $usrcustomerid);
// On Register Click
if (isset($_POST['save']) && $_POST['save'] == 'SAVE')
{
$err_msg = '';
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$birthdate = $_POST['birthdate'];
$birthdate = date('Y-m-d H:i:s', strtotime($birthdate));
$country = $_POST['country'];
$address = $_POST['address'] . ' ';
$contact = $_POST['contact'];
if ($birthdate == '') { $err_msg .= '<li>Enter your Birth Date. </li>'; }
if ($lname == '') { $err_msg .= '<li>Enter your last name. </li>'; }
if ($fname == '') { $err_msg .= '<li>Enter your first name. </li>'; }
if ($contact == '') { $err_msg .= '<li>Enter your contact number. </li>'; }
/**** If form is valid then Save to database ****/
if($err_msg == '')
{
$mcUsrCustomerProfile->UpdateObj($db, $usrcustomerid, $fname, $lname, $birthdate, $country, $address, $contact);
}
} ?>
它使用文件“UsrCustomerProfile.mc.php”中的公共函数
public function UpdateObj($db, $usrcustomerid, $fname, $lname, $birthdate, $country, $address, $contact) { $stmt = $db->prepare( " UPDATE usr_customer_profile SET fname = :fname, lname = :lname, birthdate = :birthdate, country = :country, address = :address, contact = :contact WHERE usrcustomerid = :usrcustomerid" ); $stmt->bindValue(':usrcustomerid', $usrcustomerid, PDO::PARAM_INT); $stmt->bindValue(':fname', $fname, PDO::PARAM_STR); $stmt->bindValue(':lname', $lname, PDO::PARAM_STR); $stmt->bindValue(':birthdate', $birthdate, PDO::PARAM_STR); $stmt->bindValue(':country', $country, PDO::PARAM_STR); $stmt->bindValue(':address', $address, PDO::PARAM_STR); $stmt->bindValue(':contact', $contact, PDO::PARAM_STR); $stmt->execute(); $rowAffected = $stmt->rowCount(); return $rowAffected; }
- 这是数据库中 usr_customer_profile 表的屏幕截图:
现在我有一个 php 代码,其中用户的状态由他/她拥有的包 ID 确定
<?php // Check Session Credentials and auto-login $packageid = $_SESSION['usrcustomer_packageid']; if ($packageid == 0) { $ribbon = 'ribbon-guest'; } else if ($packageid <= 1) { $ribbon = 'ribbon-elite'; } else if ($packageid == 2) { $ribbon = 'ribbon-premium'; } else if ($packageid == 3) { $ribbon = 'ribbon-luxury'; } echo('Status: <a href="membership.php"><img src="../img/'.$ribbon.'.png" class="small-icon"></a> '); if ($packageid == 0) { echo ('Guest <br/>'); } else if($packageid == 1) { echo('Elite Member <br/>'); } else if($packageid == 2) { echo('Premium Member <br/>'); } else if($packageid == 3) { echo('Luxury Member <br/>'); } if ($_SESSION['usrexpirationdays'] <= 0) { $_SESSION['usrcustomer_packageid'] = 0; } else if (isset($_SESSION['usrexpirationdate'])) { echo(' <small>(Expiry: ' . $_SESSION['usrexpirationdate'] . ', ' . $_SESSION['usrexpirationdays'] . ' days remaining)</small>'); } ?> <?php } else { ?>
- 使用了登录功能,因此使用了“$_SESSION”
- 从数据库截图中可以看出,如果用户的过期日期已经过去,则 packageid 变为 0,并显示 guest。
“如果($_SESSION['usrexpirationdays'] <= 0){ $_SESSION['usrcustomer_packageid'] = 0;”
- 问题是这只会更新显示而不是数据库值,我还应该添加什么来实现这一点?
- 我提供了足够的信息来表明我在询问我的问题之前分析了代码,任何帮助都会非常感谢你。
解决方案
推荐阅读
- python - 从 pandas DataFrame 中选择部分字符串保存在列中
- python - 如何在python中要求验证?
- amazon-web-services - SQL Server(托管在 AWS RDS 上)在 Power BI 上具有实时更新
- android - @MockK 或 mockk()
- linux - 如果条件通过,则从 mongo shell 退出 bash 脚本
- c# - ASP Net Core 和实体框架错误 - 在前一个操作完成之前在此上下文中启动了第二个操作
- c++ - 如何在 QT 中添加状态栏(不使用表单)并显示文本的长度和他的坐标(正在刷新)?
- azure-devops - 如何使用 azure DevOps API 为 GitHub Enterprise 存储库创建 azure DevOps 管道作为源
- oracle - 使用数据泵导出时无法重新映射数据
- r - 用于 plogis 的基本 R 代码的 Tidyverse 版本?