首页 > 解决方案 > 如果满足条件,则更新数据库中的 ID 号

问题描述

我正在寻求找出我留下的 php/sql/html 代码。如果满足某个条件,我只是想更改数据库中的数值。为了不将其标记为重复,请给出细分。

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();
?>

更改设置:(名为“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);
        }
      } ?>

来宾

“如果($_SESSION['usrexpirationdays'] <= 0){ $_SESSION['usrcustomer_packageid'] = 0;”

标签: phphtmlsql

解决方案


推荐阅读