|
 |
|
Codewalkers Forums
> PHP Related
> PHP Coding
|
PHP and MySql
Discuss PHP and MySql in the PHP Coding forum on Codewalkers. PHP and MySql Having problems with a PHP script you are coding? This is the place to get help!
|
|
|
|
 |
|
|
|
|
|

Codewalkers Forums Sponsor:
|
|
|

November 9th, 2012, 04:04 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
|
PHP and MySql
I have confused myself no end and I am not sure how to explain my predicament. I am trying to reduce my inventory (in MySql database) by the amount of purchases a customer has made, but I can't seem to figure out which php variables to use. My last attempt was:-
<code>
<?php
require_once 'connectdb.php';
if (isset($totalCount)){
if(($totalCount)!="" && !empty($totalCount)) {
$totalCount = $_POST[$totalCount];
$panname = $_POST['panname'];
}
// Select product table and reduce inventory quantity by purchased quantity
$query = ("UPDATE $pans SET $row[stock]=stock-{'$totalCount'} WHERE panname = {'$panname'}");
// Function: update_record()
// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}
?>
<close code>
What ever I try does not seem to make any reductions in my inventory. I have four previous pages of scripting, and I am not sure which php coding I require to extract the details from the customer's form input to initiate the action.
I hope you understand this, I know you will require the form details, but as stated above I am not sure which bit to attach because I don't know which script is associated with the action I require. I have attempted the script about six times with different coding from prior pages. I am sorry it is so vague, but I hope you can put me on the right track. Thanks
|

November 11th, 2012, 03:58 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 6 m 20 sec
Reputation Power: 0
|
|
|
Where are your values coming from?
|

November 12th, 2012, 10:35 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Sleeping Troll Where are your values coming from? |
Quote: | This is my problem, I'm not sure where it should come from. I have the following scripts:- |
[CODE]
<php>
if(!isset($_SESSION['basketItems'])) {
$_SESSION['basketItems'] = Array();
}
// Check to see if products have been purchased and update quantities appropriately
if(isset($_POST['updateQty'])) {
foreach($_SESSION['basketItems'] as $target) {
$cTarget = 'cartItem_'.$target['name'];
$cQuantity = 'itemQuantity_'.$target['name'];
if(isset($_POST[$cTarget])) {
if($_POST[$cQuantity] != $target['count']) {
$basketText = "Item quantity updated : " . $target['niceName'] ;
if($_POST[$cQuantity] == 0) {
unset($_SESSION['basketItems'][$target['name']]);
} else {
$_SESSION['basketItems'][$target['name']]['count'] = $_POST[$cQuantity];
?>
[CLOSE CODE]
[CODE]
<?php
// Inform the customer of the cost of the items purchased together with post & packaging
foreach($_SESSION['basketItems'] as $target) {
echo '<tr>';
echo '<td>'.$target['niceName'].'</td>';
echo '<td>'.$target['count'].'</td>';
echo '<td>'.number_format(($target['panPrice']/100), 2, '.', '').'</td>';
echo '</tr>';
$totalPrice += ($target['count'] * $target['panPrice']);
$totalCount += $target['count'];
}
?>
[CLOSE CODE]
[CODE]
<?php
<h2>Credit Card Details</h2>
<form name = "checkout" method="POST" action="exit.php"
<p>
<input type="text" name= "cardNumber" <?php echo $_POST['cardNumber'].""; ?> size="20" />
<input type="submit" name="submit" value="Purchase Pans" />
<input type="hidden" name="orderTotal" value= <?php echo $totalPrice; ?>" />
</p>
</form>
?> [CLOSE CODE]
[CODE]
<?php
echo '<form name="cartForm" method="POST" action="cartaction.php">';
echo "<table padding=\"3\">"; // start the cart display formatting
// Call the purchased Session items, add and print to cart checkout page
foreach($_SESSION['basketItems'] as $bItem) {
$output = '<tr>';
$output .= '<input type="hidden" name="cartItem_'.$bItem['name'].'" value="'.$bItem['name'].'" />';
$output .= '<td><img src="'.$bItem['panImage'].'" alt="'.$bItem['niceName'].'" title="'.$bItem['niceName'].'" /></td>';
$output .= '<td>' . $bItem['niceName'] . '</td>';
$output .= "<td><input type='text' value='".$bItem['count']."' size='2' maxlength='2' name='itemQuantity_".$bItem['name']."' /></td>";
$output .= "<td><input type='submit' name='updateQty' value='Update This One Item' size='20' maxlength='20' /></td>";
$output .= '</tr>';
echo $output;
}
echo '</table>';
echo '</form>';
?> [CLOSE CODE]
Quote: | or none of the above. (The above code is on two separate pages) |
|

November 13th, 2012, 09:05 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,361

Time spent in forums: 4 Weeks 1 Day 23 h 39 m 34 sec
Reputation Power: 8
|
|
Code:
if (isset($totalCount)){
does $totalcount exist?
it does exist
if(($totalCount)!="" && !empty($totalCount)) {
if its not blank, and its not empty go in here
$totalCount = $_POST[$totalCount];
why declare it again?
$panname = $_POST['panname'];
}
you are redeclaring the variable, are you doing that intentionally?
__________________
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.
Last edited by DavidMR : November 13th, 2012 at 09:08 AM.
|

November 13th, 2012, 11:14 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by DavidMR
Code:
if (isset($totalCount)){
does $totalcount exist?
it does exist
if(($totalCount)!="" && !empty($totalCount)) {
if its not blank, and its not empty go in here
$totalCount = $_POST[$totalCount];
why declare it again?
$panname = $_POST['panname'];
}
you are redeclaring the variable, are you doing that intentionally? |
No, just don't know what I am doing. Originally I was getting error messages telling me that items were not declared, so just covered all bases. I have now removed the extra script and it hasn't thrown up an error, BUT it is also still not decreasing my inventory??? [/QUOTE]
|

November 13th, 2012, 11:17 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,361

Time spent in forums: 4 Weeks 1 Day 23 h 39 m 34 sec
Reputation Power: 8
|
|
Quote: | Originally Posted by Soo No, just don't know what I am doing. Originally I was getting error messages telling me that items were not declared, so just covered all bases. I have now removed the extra script and it hasn't thrown up an error, BUT it is also still not decreasing my inventory??? |
is the update query correct? echo it out and check (paste it here if you can)
|

November 14th, 2012, 01:59 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by DavidMR is the update query correct? echo it out and check (paste it here if you can) |
I have a feeling that it is more than likely Not Correct, but I can't figure out where to take the query input from. I am getting nothing at all from echoing it out (providing of course that I am doing that right also - My brain has completely fogged over!  [/QUOTE]
|

November 14th, 2012, 02:36 PM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,361

Time spent in forums: 4 Weeks 1 Day 23 h 39 m 34 sec
Reputation Power: 8
|
|
do you have a php errors file? you could echo text at various parts of the script including where the update query is running, this could identify where the issue is.
also put a die(); after echoing the q uery so you can see it, if you dont see it then, there could be a chance its not going into the section where the update is.
example, replace
PHP Code:
$query = ("UPDATE $pans SET $row[stock]=stock-{'$totalCount'} WHERE panname = {'$panname'}");
// Function: update_record()
// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}
with:
PHP Code:
$query = ("UPDATE $pans SET $row[stock]=stock-{'$totalCount'} WHERE panname = {'$panname'}");
//NOTE 1
echo $query;
// Function: update_record()
// submit the query to the database
$results = @mysql_query($query, $connection);
//Note2
if (!$results)
die(mysql_error());
}
Note1 is what i added, Note2 is where you had an error and left out a $.
Last edited by DavidMR : November 14th, 2012 at 02:39 PM.
|

November 15th, 2012, 10:22 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 1
Time spent in forums: 6 m 51 sec
Reputation Power: 0
|
|
PHP and MySql
s this code is worked .... i used this code in my site phpden. 
|

November 15th, 2012, 11:30 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by DavidMR do you have a php errors file? you could echo text at various parts of the script including where the update query is running, this could identify where the issue is.
also put a die(); after echoing the q uery so you can see it, if you dont see it then, there could be a chance its not going into the section where the update is.
example, replace
PHP Code:
$query = ("UPDATE $pans SET $row[stock]=stock-{'$totalCount'} WHERE panname = {'$panname'}");
// Function: update_record()
// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}
with:
PHP Code:
$query = ("UPDATE $pans SET $row[stock]=stock-{'$totalCount'} WHERE panname = {'$panname'}");
//NOTE 1
echo $query;
// Function: update_record()
// submit the query to the database
$results = @mysql_query($query, $connection);
//Note2
if (!$results)
die(mysql_error());
}
Note1 is what i added, Note2 is where you had an error and left out a $. |
I have ini_set('display_errors',1);
error_reporting(E_ALL); to catch any errors and nothing is appearing at the moment. I put your Note 1 (and 2) in and am getting nothing returned on that, so would therefore assume that I do not have the right script in there in the first instance. I even tried changing SET $row[stock] to just stock
eg: $query = ("UPDATE pans SET stock=stock-{'$totalCount'} WHERE panname = {'$panname'}");
but no joy [/QUOTE]
|

November 16th, 2012, 04:29 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,361

Time spent in forums: 4 Weeks 1 Day 23 h 39 m 34 sec
Reputation Power: 8
|
|
|
Are you able to echo anything on that script? If so, where can you echo stuff? What do you see when the page is submitted?
|

November 16th, 2012, 01:54 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by DavidMR Are you able to echo anything on that script? If so, where can you echo stuff? What do you see when the page is submitted? |
Nothing seems to be echoing at all. All that is showing is:
Your Order
Thank you for purchasing your pans.
The sum of £ 25.49 will be debited from your Visa card.
Go Back Home
No matter where I echo, nothing shows and nothing is reduced in the stock table.
This is the complete script on this page (exit.php)
Code:
<?php
session_start();
setlocale(LC_MONETARY, 'en_GB');
ini_set('display_errors',1);
error_reporting(E_ALL);
$error = false;
$_SESSION['errorMessage'] = array();
// initialise our functions
function CCvalidate($cardNumber, $cardType)
{
// Validate store card format
if ((preg_match('/^([P-Z1-6]{16})$/', $cardNumber) || preg_match('/^([P-Z]{4}[1-6]{4}[P-Z]{4}[1-6]{4})$/', $cardNumber)) && strlen($cardNumber)>15)
{ // A & B Store Card
return true;
} else {
$_SESSION['errorMessage'][] = "You have entered your card details incorrectly, please correct and try again, thank you";
return false;
}
}
// Check that we have the required variables and they're not just
// empty. If we don't, then go back to the form page
if(isset($_POST['cardNumber'])) {
if(empty($_POST['cardNumber'])||$_POST['cardNumber']=="") {
$error = true;
$_SESSION['errorMessage'][] = '<p>You have not entered a card number, please try again</p>';
}
} else {
$error = true;
$_SESSION['errorMessage'][] = '<p>You have not entered a card number, please try again</p>';
}
if (isset($_POST['cardNumber'])){
if($_POST['cardNumber']!="" && !empty($_POST['cardNumber'])) {
$cardNumber = $_POST['cardNumber'];
$cardType = $_POST['cardType'];
if(!CCvalidate($cardNumber, $cardType)){
$error = true;
$_SESSION['errorMessage'][] = '<p>You have not entered a card number, please try again</p>';
}
}
}
// check to see if we have errors and redirect
if($error==true){
header('Location: checkout.php');
exit();
}
require_once 'connectdb.php';
if (isset($totalCount)){
if(($totalCount)!="" && !empty($totalCount)) {
[$totalCount];
$panname = $_POST['panname'];
}
// Select product table and reduce inventory quantity by purchased quantity
$query = ("UPDATE $pans SET $row[stock]=stock-{'$totalCount'} WHERE panname = {'$panname'}");
echo $query;
// Function: update_record()
// submit the query to the database
$results = @mysql_query($query, $connection);
if (!$results) {
die(mysql_error());
echo $results;
}
}
require_once 'connectdb.php';
if (isset($_POST['cardNumber'])){
if($_POST['cardNumber']!="" && !empty($_POST['cardNumber'])) {
$cardnumber = $_POST['cardNumber'];
$totalcost = $_POST['orderTotal'];
}
// Keep record of purchased items for this customer
$query = @"INSERT INTO transactions VALUES ('$_POST[cardNumber]', '$_POST[orderTotal]')";
// submit the query to the database
$results = mysql_query($query, $connection);
if (@!$results) {
die(mysql_error());
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Checkout and Order Your Selected Pans</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
</head>
<body>
<div id="header">
<h1>Your Order</h1>
</div>
<div id="mainblock">
<p>Thank you for purchasing your pans.</p>
<p>The sum of £ <?php echo number_format(($_POST['orderTotal']/100), 2, '.', ''); ?> will be debited from your <?php echo ($_POST['cardType']); ?> card.</p>
</div>
<div id="footer">
<ul class="navlist">
<li><a href="index.php">Go Back Home</a></li>
</ul>
</div>
</body>
</html>
Quote: | With the echo $query inserted twice to try and get some response. ??? |
|

November 18th, 2012, 04:12 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
[QUOTE=Soo]Nothing seems to be echoing at all. All that is showing is:
Your Order
Thank you for purchasing your pans.
The sum of £ 25.49 will be debited from your Visa card.
Go Back Home
No matter where I echo, nothing shows and nothing is reduced in the stock table.
This is the complete script on this page (exit.php)
Code:
<?php
session_start();
setlocale(LC_MONETARY, 'en_GB');
ini_set('display_errors',1);
error_reporting(E_ALL);
$error = false;
$_SESSION['errorMessage'] = array();
// initialise our functions
function CCvalidate($cardNumber, $cardType)
{
// Validate store card format
if ((preg_match('/^([P-Z1-6]{16})$/', $cardNumber) || preg_match('/^([P-Z]{4}[1-6]{4}[P-Z]{4}[1-6]{4})$/', $cardNumber)) && strlen($cardNumber)>15)
{ // A & B Store Card
return true;
} else {
$_SESSION['errorMessage'][] = "You have entered your card details incorrectly, please correct and try again, thank you";
return false;
}
}
// Check that we have the required variables and they're not just
// empty. If we don't, then go back to the form page
if(isset($_POST['cardNumber'])) {
if(empty($_POST['cardNumber'])||$_POST['cardNumber']=="") {
$error = true;
$_SESSION['errorMessage'][] = '<p>You have not entered a card number, please try again</p>';
}
} else {
$error = true;
$_SESSION['errorMessage'][] = '<p>You have not entered a card number, please try again</p>';
}
if (isset($_POST['cardNumber'])){
if($_POST['cardNumber']!="" && !empty($_POST['cardNumber'])) {
$cardNumber = $_POST['cardNumber'];
$cardType = $_POST['cardType'];
if(!CCvalidate($cardNumber, $cardType)){
$error = true;
$_SESSION['errorMessage'][] = '<p>You have not entered a card number, please try again</p>';
}
}
}
// check to see if we have errors and redirect
if($error==true){
header('Location: checkout.php');
exit();
}
require_once 'connectdb.php';
if (isset($totalCount)){
if(($totalCount)!="" && !empty($totalCount)) {
[$totalCount];
$panname = $_POST['panname'];
}
// Select product table and reduce inventory quantity by purchased quantity
$query = ("UPDATE $pans SET $row[stock]=stock-{'$totalCount'} WHERE panname = {'$panname'}");
echo $query;
// Function: update_record()
// submit the query to the database
$results = @mysql_query($query, $connection);
if (!$results) {
die(mysql_error());
echo $results;
}
}
require_once 'connectdb.php';
if (isset($_POST['cardNumber'])){
if($_POST['cardNumber']!="" && !empty($_POST['cardNumber'])) {
$cardnumber = $_POST['cardNumber'];
$totalcost = $_POST['orderTotal'];
}
// Keep record of purchased items for this customer
$query = @"INSERT INTO transactions VALUES ('$_POST[cardNumber]', '$_POST[orderTotal]')";
// submit the query to the database
$results = mysql_query($query, $connection);
if (@!$results) {
die(mysql_error());
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Checkout and Order Your Selected Pans</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
</head>
<body>
<div id="header">
<h1>Your Order</h1>
</div>
<div id="mainblock">
<p>Thank you for purchasing your pans.</p>
<p>The sum of £ <?php echo number_format(($_POST['orderTotal']/100), 2, '.', ''); ?> will be debited from your <?php echo ($_POST['cardType']); ?> card.</p>
</div>
<div id="footer">
<ul class="navlist">
<li><a href="index.php">Go Back Home</a></li>
</ul>
</div>
</body>
</html>
|

November 19th, 2012, 05:52 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,361

Time spent in forums: 4 Weeks 1 Day 23 h 39 m 34 sec
Reputation Power: 8
|
|
This is around your update query but its not defined?
|

November 19th, 2012, 12:58 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 15
Time spent in forums: 3 h 20 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by DavidMR
This is around your update query but its not defined? |
Does this count? On another page linked with session_start();at the head of both pages?
Code:
<?php
$totalPrice = 0;
$totalPostage = 0;
$totalCount = 0;
// Inform the customer of the cost of the items purchased together with post & packaging
foreach($_SESSION['basketItems'] as $target) {
echo '<tr>';
echo '<td>'.$target['niceName'].'</td>';
echo '<td>'.$target['count'].'</td>';
echo '<td>'.number_format(($target['panPrice']/100), 2, '.', '').'</td>';
echo '</tr>';
$totalPrice += ($target['count'] * $target['panPrice']);
$totalCount += $target['count'];
}
if($totalCount == 1) {
$totalPostage = 350;
} else if ($totalCount == 0) {
$totalPostage = 0;
} else if ($totalPrice > 6000) {
$totalPostage = 750;
} else {
$totalPostage = 350 + (200 * ($totalCount - 1));
}
$totalPrice += $totalPostage;
?>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|