如果使用php满足某些条件,则更新数据库中的值

时间:2019-01-16 01:44:37

标签: php html sql

im是为了找出我剩下的php / sql / html代码。我只是想在满足特定条件的情况下更改数据库中的数字值。请详细说明以免将其标记为重复。

  • 我看到的有关php和sql更新的教程往往表明,您必须写下一个变量才能连接到数据库并从那里更新:

W3SCHOOLS示例:

<?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表的屏幕截图:

    enter image description here

    • 现在我有一个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 { ?>
      

guest

  • 由于使用了登录功能,因此使用了“ $ _SESSION”
  • 从数据库的屏幕快照可以看到
  • ,如果用户的到期日期已经过去,则packageid变为0,并显示guest。

“ if($ _SESSION ['usrexpirationdays'] <= 0){                 $ _SESSION ['usrcustomer_packageid'] = 0;“

  • 问题在于,这只会更新显示,而不会更新数据库值,我还应该添加什么来实现这一点?
  • 我提供了足够的信息来表明我在询问问题之前已经分析了代码,非常感谢您。

0 个答案:

没有答案