I've just started working on my first PHP / MySQL / web page, but I've ran into a snag. I'm using XAMPP & Dreamweaver CS4. I'm building a small site that will allow me to have several different people log in to make purchases. Each person will have their own discount rate. I've got the log in page built with PHP. A MySQL database that holds their names, email/Password, & discount multipliers. My problem is, how can I make it so that when someone logs in, the prices are showing that individual's discounted price? (Sales Price * Salesperson's Multiplier from Database)The way I've got it now the discount multiplier keeps pulling from the first row in the table, regardless of who I log in as. Thanks for any help or tips that you might could get me.
thanks
Probably Simple for a PHP Guru
Moderator: General Moderators
Re: Probably Simple for a PHP Guru
In your PHP script that builds the web page, you must first get the data for the person who logs in, probably save it to a $_SESSION variable so you can use it on several pages, then calculate the display price as you build each part of the page. If you need more detailed help, you will have to show us the code you now have.
Re: Probably Simple for a PHP Guru
This is what I have so far. I don't know how to add the session variable like you're talking about so that it carries from page to page. I'll keep doing research. Like I said, I'm just starting to learn this stuff. All of your help so far has been greatly appreciated.
Code: Select all
<?php require_once('Connections/connRegister.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "fail.htm";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_connRegister, $connRegister);
$query_Recordset1 = "SELECT * FROM register";
$Recordset1 = mysql_query($query_Recordset1, $connRegister) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
mysql_select_db($database_connRegister, $connRegister);
$query_recordsetDiscount = "SELECT MULTIPLIER FROM register ORDER BY ID ASC";
$recordsetDiscount = mysql_query($query_recordsetDiscount, $connRegister) or die(mysql_error());
$row_recordsetDiscount = mysql_fetch_assoc($recordsetDiscount);
$totalRows_recordsetDiscount = mysql_num_rows($recordsetDiscount);
$maxRows_Recordset2 = 10;
$pageNum_Recordset2 = 0;
if (isset($_GET['pageNum_Recordset2'])) {
$pageNum_Recordset2 = $_GET['pageNum_Recordset2'];
}
$startRow_Recordset2 = $pageNum_Recordset2 * $maxRows_Recordset2;
mysql_select_db($database_connRegister, $connRegister);
$query_Recordset2 = "SELECT * FROM products";
$query_limit_Recordset2 = sprintf("%s LIMIT %d, %d", $query_Recordset2, $startRow_Recordset2, $maxRows_Recordset2);
$Recordset2 = mysql_query($query_limit_Recordset2, $connRegister) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
if (isset($_GET['totalRows_Recordset2'])) {
$totalRows_Recordset2 = $_GET['totalRows_Recordset2'];
} else {
$all_Recordset2 = mysql_query($query_Recordset2);
$totalRows_Recordset2 = mysql_num_rows($all_Recordset2);
}
$totalPages_Recordset2 = ceil($totalRows_Recordset2/$maxRows_Recordset2)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Shopping Cart
</h1>
<h2> </h2>
<form action="" method="get" name="form1">
<?php do { ?>
<p><?php echo $row_Recordset2['Description']; ?> $<?php echo ($row_Recordset2['PRICE'] * $row_Recordset1['MULTIPLIER']); ?></p>
<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
<p> </p>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($recordsetDiscount);
mysql_free_result($Recordset2);
?>
Re: Probably Simple for a PHP Guru
Would I be correct in guessing that you have copied this code from somewhere and are trying to customize it for your purpose? Nothing wrong with that, but in this case, you have code that was written for something rather different from what you said you need. It's at least 3 times as much code as is needed.
Here are the steps that your code needs to take:
Some may disagree with me, but my advice would be to not even try to use someone else's code at this early stage of your learning, unless you're lucky enough to find a script that is clearly written to apply very closely to what you want to do. I would suggest that you begin with writing a script that properly displays the products on a web page and get that working. Only then would I suggest that you begin wrapping that in code that provides for a user to login and then for your code to validate that input.
Here are the steps that your code needs to take:
- Check whether user is already logged in (your code does that, but since you don't ever provide a way for anyone to login, it's pointless).
- If user isn't logged in, present a login form (the code you've shown here doesn't do that).
- If the login form data is present in the $_POST array, query the database to determine if it is a valid user and password (your code doesn't do that).
- If the user and password are valid, set $_SESSION variables for the user, password, and discount (your code doesn't do that).
- If the user and password are valid, query the database for the product information and display on the page (NOT in a <form>, as your code does), using the discount multiplier that's stored in the $_SESSION variable.
Some may disagree with me, but my advice would be to not even try to use someone else's code at this early stage of your learning, unless you're lucky enough to find a script that is clearly written to apply very closely to what you want to do. I would suggest that you begin with writing a script that properly displays the products on a web page and get that working. Only then would I suggest that you begin wrapping that in code that provides for a user to login and then for your code to validate that input.
Re: Probably Simple for a PHP Guru
The code you're seeing is what Dreamweaver is generating. That's why I'm having a hard time customizing this to fit my need. I don't think Dreamweaver has an option for what I'm wanting. Thanks for your help. I'll try what you've suggested.
Re: Probably Simple for a PHP Guru
I am definitely biased, but I firmly believe that Dreamweaver (or any competitive product, for that matter) is terrible software to use for anything that involves processing logic. It's OK for simple HTML web page layouts, but worse than useless for anything else. No doubt others will disagree with me.