im on the quest of figuring out the php/sql/html code I was left. I am just trying to change a number value in the database if a certain condition is met. ill give a breakdown in order to not get this flagged as a duplicate.
- tutorials that i see in regarding to php and sql update tends to show that you have to write down a variable to connect to the database and update from there:
W3SCHOOLS EXAMPLE:
<?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();
?>
- the code i was given however, does it differently. it uses a "public function" in order to connect to the database. here is a example of a working part of the code where a user's information is changed from what is typed in textfields via "UpdateObj"
CHANGING SETTINGS: (named "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);
}
} ?>
it is using a public function from the file "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; }
- here is a screenshot of the usr_customer_profile table in the database:
now i have a php code where a user's status is determined by what package id he/she has
<?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 { ?>
- there is a login feature used hence the use of the "$_SESSION"
- as you can see with the screenshot of the database, if a user's expiration date has gone by, the packageid becomes 0, and displays guest.
"if ($_SESSION['usrexpirationdays'] <= 0) { $_SESSION['usrcustomer_packageid'] = 0;"
- the problem is that this only updates the display but not the database value, what else should i add to achieve this?
- i provided enough information to show that i analyzed the code before asking my concern, any help would be great thank you.