php cart out of stock mysql - php

The following PHP validation code works however if i go back to products page to add more of the same product it can overrides saying out' on cart page when quantity is not available
The following shows an if statement that is linked to $cart session data along with $film quantity which is stored in a table.
For some reason $film['quantity'] has a value of 1 so if quantity >1 added to cart 'out' shows which is wrong?
<?php $itemcount = $_SESSION['itemcount'];
if($cart[QUANTITY][$i] > $table['quantity'] ) { ?>
<span style="font-family:'arial';">Out </span>
<?php } ?>

With the code shown it is hard to say what the issue is. However there is a good suspect here:
if($cart[QUANTITY][$i] > $film['quantity'] ) {
I suspect it should be:
if($cart[$i]['QUANTITY'] > $film['quantity'] ) {
Other things that might be amiss:
Have you session_start()ed?
Is the key QUANTITY not quantity (Case is important)
Do you happen to have defined a constant QUANTITY somewhere? (Quotes are important)

I think it will help you:
<?php
echo $product_image;
echo $product_count;
echo $product_price;
echo "<form method='post' action=''>";
echo "<input type='text' name='product_shopping_count' />";
echo "<input type='hidden' name='product_id' value='".$product_id."' />";
echo "<input type='submit' name='addCart' value='AddCart' />";
echo "</form>";
if(isset($_POST['addCart']){
if(isset($_SESSION['cart']){
$cart = $_SESSION['cart'];
if($cart[$_POST['product_id']]['quantity']>$product_count){
echo "<span style='font-family:arial;font-size:16px;'>Out </span>";
}else if($_POST['product_shopping_count']>$product_count){
echo "<span style='font-family:arial;font-size:16px;'>Out </span>";
}else {
$cart['product_id']= $_POST['product_id'];
$cart['product_id']['quantity'] = $_POST['product_shopping_count'];
$cart['product_id']['price'] = $product_price;
$_SESSION['cart'] = $cart;
}
}
}
?>

Related

Update from checkbox to wrong ID in php

issue for days now.
I have a form with multiple checkboxes, I will never know how many rows will be in the form, and I am aware that an empty checkbox will not post to the processing page. I have also tried providing a hidden value to the checkbox to no avail...
I know there is a straightforward method to this, please assist:
The following code is on the form:
echo "<td><center><input type='checkbox' checked='checked' name='jce_pnt_id[]' value='$jce_pnt_id' onclick='return false;' /><br>$jce_pnt_id</center></td>";
echo "<td title='Task Complete ?'><center><input type='hidden' name='tr_g[]' value='25'>";
if($jce_ins_complete < '100' || $jce_ins_act_complete == '0')
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100'>";
}
else
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100' checked='checked'>";
}
echo "</center></td>";
Here is a screenshot (look at the red circled column):
Here is the processing snippet (not working):
$jce_pnt_id = $_POST['jce_pnt_id'];
$tr_g = $_POST['tr_g']; // jce_pnt_act_complete
foreach ($tr_g as $id => $trg) {
//if(empty($trg)) { $trg = "0"; }
$queryg = "UPDATE `jce_pnt` SET `jce_pnt_act_complete` = '$trg' WHERE `jce_pnt_id` = '$jce_pnt_id[$id]'";
$resultg = mysqli_query($cxn,$queryg);
}
* The value writes to the wrong ID in the database.

How do I use $_SESSION to store items in an array and trigger a button swap?

so I'm trying to store shopping cart items by there ID. Once this happens a button needs to appear "Remove from Cart", if I click this button a new button will appear "Add to Cart" but I'm a little bit lost on it from this point. Im new on here so please excuse my mistakes. My code is below:
session_start();
$items[] = $_POST['add'];
if (isset($_POST['add'])) {
$_SESSION['cart'][]=$_POST['add'];
} else if (isset($_POST['remove'])) {
unset ($_SESSION['cart'][$_POST['remove']]);
}
foreach($vend as $vendID=> $items) {
echo "<form action='vend.php' method='post'>";
echo "<article id ='vend-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "</div>";
echo "<div class ='img-div'>";
echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='item-p'>";
echo "<p>{$items['desc']}</p></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
//echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
if(isset($_POST['add']) && ($_SESSION['cart'] == $vendID)) {
echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
}
else {
echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
}
In your case here what should be done:
session_start();
/* Check if $_SESSION['cart'] exists */
if (!isset($_SESSION['cart'])) {
/* Init cart as empty array */
$_SESSION['cart'] = [];
}
if (isset($_POST['add']) && 0 < $_POST['add']) {
/* Add product id to session cart */
$_SESSION['cart'][$_POST['add']] = 1;
} else if (isset($_POST['remove']) && 0 < $_POST['remove']) {
/* Remove product id from session cart */
unset($_SESSION['cart'][$_POST['remove']]);
}
// I check with `0 < $id` because ids are always positive
foreach($vend as $vendID=> $items) {
echo "<form action='vend.php' method='post'>";
echo "<article id ='vend-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "</div>";
echo "<div class ='img-div'>";
echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='item-p'>";
echo "<p>{$items['desc']}</p></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
if(isset($_SESSION['cart'][$vendID])) {
// you have `$vendID` in session cart - then show Remove button
echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
} else {
// you DON'T have `$vendID` in session cart - then show Add button
echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
}
// Also don't forget to close `</form>`
}
To remove an item from your cart you are accessing the item based on your $vendID variable. Which would require your item to be stored in $_SESSION['cart'] as an array:
$_SESSION['cart'][0] = "item 1";
$_SESSION['cart'][1] = "item 2";
...
but later in your code you are accessing $_SESSION['cart'] as if it is just a value not an array
adding an item in your code is done with the following line:
$_SESSION['cart'][]=$_POST['add'];
This results in something like this:
//empty cart
$_SESSION['cart'][0] = $vendid;
Turns out I needed a underscore , i had $POST instead of $_POST and it works now, thanks all :)

How get post value in pagination

I am working on search functionality with pagination in php.Below is my code but whenever I click on next link the search query is not taking variable which is passed through POST.
Can you please help me..
searchstr=$_POST['q'];
session_start();
$_SESSION['searchchar']=$searchstr;
$rec_limit=3;
$connection=connection();
$searchstr=$_REQUEST['q'];
$sql="Select count(*) from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%'";
echo$sql;
$result1=mysql_query($sql);
if(! $result1)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result1,MYSQL_NUM );
$rec_count = $row[0];
echo$rec_count;
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql="Select * from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%' limit $offset,$rec_limit";
echo$sql;
$result=mysql_query($sql);
$rec_num=mysql_num_rows($result);
if($rec_num>0)
{
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$fname=$row['FirstName'];
$image=$row['ProfileImagePath'];
$about=$row['About'];
$url=$row['CustomURL'];
$DOB=$row['DOB'];
$DOD=$row['DOD'];
?>
<div class="search">
<p class="content"><img src="<?php echo $image;?>" style="border:1px solid red;width:100px;height:100px;" /></p>
<div class="content"><p>
<?php
echo "$fname";
?>
<p>(<?php echo date("Y",strtotime($DOB));?>-<?php echo date("Y", strtotime($DOD));?>)</p>
</p><p><?php echo$about?></p>
</div>
</div>
<?php
}
if( $page > 0 & $left_rec > $rec_limit)
{
$last = $page - 2;
echo "Previous Records |";
echo "Next Records";
}
else if( $page == 0 & $rec_count>$rec_limit )
{
echo "Next Records";
}
else if( $left_rec < $rec_limit & $rec_count>$rec_limit )
{
$last = $page - 2;
echo "Previous Records";
}
}
else
{
echo"No memorial found.Please try with other name.";
}
enter code here
?>
There are a couple problems in your code.
First:
You are using the $_GET variable incorrectly. You have $_GET{'page'}, but you should be using it the same way as the$_POST variable, like this: $_GET['page']
Second:
You are interpreting the idea of POSTing and clicking links incorrectly. What you need to make with your next and previous buttons are a hidden form that has all the other variables you need. For example, the simplest way that won't involve changing a lot of your other code:
if( $page > 0 & $left_rec > $rec_limit)
{
$last = $page - 2;
// Previous button form
echo "<form action='?page=$last' method='POST'>";
echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
echo "<input type='submit' value='Previous Records' />";
echo "</form>";
echo " | "; // The delimiting character you were using, you'll need to style the input type='submit's to look more like a link if that's what you are going for
// Next button form
echo "<form action='?page=$page' method='POST'>";
echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
echo "<input type='submit' value='Next Records' />";
echo "</form>";
}
This method will keep your variables in the request. Using a link essentially resets the $_POST variable. There are a ton of other possible solutions, but this is one will require the least amount of work to modify your example.
if you are creating sorting and searching functionality than I will suggest you to use $_SESSION variable rather than $_POST or $_GET it will help you manage your grid/data very well.

how to submit a form to another page in wordpress plugin

I am developing a wordpress plugin , which submits a form to another page. But when I try to submit the form to another page , then that page returns some php error. My form code is below
echo "<form action='".plugins_url()."/wp_voting_poll/frontend_poll_process.php' method='post'>";
echo "<input type='hidden' name='hide' value='$ques' />";
$total_vote_count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_result WHERE question_uid='$ques'" );
if($ques!=""){
echo "<table>";
foreach($ans_data as $ans_res){
// $ans=$ans_res->answer;
$answer_id=$ans_res->id;
$type=$ans_res->answer_type;
$vote_count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_result WHERE answer_id='$answer_id'" );
if($vote_count==0){
error_reporting(0);
}
$vote_percent=($vote_count*100)/$total_vote_count;
echo "<tr> <td>";
echo "<div class='answer_div'>";
if($type==1){
echo "<div class='input'><input type='radio' name='ans_name[]' value='$answer_id'/>".$ans_res->answer."<br/></div>";
}
elseif($type==0){
echo "<div class='input'><input type='checkbox' name='ans_name[]' value='$answer_id'/>".$ans_res->answer."<br/></div>";
}
if($backend==0){
echo "</td> <td>";
echo "<h4> total vote counted $vote_percent% </h4>";
// echo "<img src='$url' width='$width_img'/>";
$bar=$vote_percent*5.9;
echo "<img src='$url' height='10' width='$bar' />";
echo "</td></tr>";
}
}
echo "</table>";
echo "<input type='submit' value='Submit vote' />";
echo "</form>";
And this is my code of another page , which should process the form . But unfortunately it returns php error.
<?php
require_once("function_ip.php");
$vote_result=$_POST['ans_name'];
$uid=uniqid();
global $wpdb;
$table_vote=$wpdb->prefix."poll_answer_result";
$count=count($vote_result);
$hidden=$_POST['hide'];
$ans_data=$wpdb->get_results("SELECT * FROM $table_vote WHERE question_id='$hidden'" );
if($count>0){
foreach($vote_result as $vote_arr){
$wpdb->insert($table_vote,
array('answer_id' => $vote_arr,
'ip' =>get_client_ip(),
'question_uid' => $hidden
));
}
}
?>
Wordpress has a generic handler to deal with all forms - admin-post.php.
If you include a hidden field in your form called action, you can then hook in to a function of your choice with all the goodness of wordpress included.
echo "<form action='".get_admin_url()."admin-post.php' method='post'>";
echo "<input type='hidden' name='action' value='submit-form' />";
echo "<input type='hidden' name='hide' value='$ques' />";
{ Enter the rest of your first block of code from above here }
echo "</form>";
And then in your functions.php file (or any other php file that you have included via functions.php), you can use this method.
add_action('admin_post_submit-form', '_handle_form_action'); // If the user is logged in
add_action('admin_post_nopriv_submit-form', '_handle_form_action'); // If the user in not logged in
function _handle_form_action(){
{ Enter your second block of code from above here }
}
I'm not sure if you require a redirect once you reach your desired destination, but that can be easily accounted for if you do.
And one final question - is this form on the front end, or in the admin area? Not that it should make a difference that this answer, I'm just curious...
Your frontend_poll_process.php page is getting called out of the WordPress environment, therefore returning an error on $wpdb->get_results().
You can add your code to a plugin or functions.php using hooks:
<?php
add_action( 'after_setup_theme', 'so_19997913' );
function so_19997913() {
require_once("function_ip.php");
$vote_result = $_POST['ans_name'];
$uid = uniqid();
global $wpdb;
$table_vote = $wpdb->prefix . "poll_answer_result";
$count = count( $vote_result );
$hidden = $_POST['hide'];
$ans_data = $wpdb->get_results( "SELECT * FROM $table_vote WHERE question_id='$hidden'" );
if ( $count > 0 ) {
foreach ( $vote_result as $vote_arr ) {
$wpdb->insert( $table_vote, array('answer_id' => $vote_arr,
'ip' => get_client_ip(),
'question_uid' => $hidden
) );
}
}
}

How to display information in another PHP Page

I am having trouble with a mini shopping cart. I have created the mini shopping cart and when a product is added it goes into the Basket page. However, i want to create a checkout page. How do i grab the products in the basket and put it in a checkout page.
So For example, the check out page will look like this
ITEM NAME, ITEM MODEL, ITEM PRICE
TOTAL OF ITEMS
CHECKOUT BUTTON (links to a payment system)
This is my Basket.php code:
<?php
$bikecode = $_GET['id']; //the product id from the URL
$action = $_GET['action']; //the action from the URL
if($bikecode && !productExists($bikecode)) {
die("Product Doesn't Exist");
}
switch($action) { //decide what to do
case "add":
$_SESSION['cart'][$bikecode]++; //add one to the quantity of the product with id $bikecode
break;
case "remove":
$_SESSION['cart'][$bikecode]--; //remove one from the quantity of the product with id $bikecode
if($_SESSION['cart'][$bikecode] == 0) unset($_SESSION['cart'][$bikecode]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
if($_SESSION['cart']){
echo "<table width=\"100%\">";
foreach($_SESSION['cart'] as $bikecode => $quantity) {
$sql = sprintf("SELECT BikeCode, Model, Price FROM Bike WHERE BikeCode = '%s';", $bikecode);
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0) {
list($bikecode, $model, $price) = mysqli_fetch_row($result);
$cost = $quantity * $price;
$total = $total + $cost;
echo "<tr><th>BikeCode:</th><th>Model:</th><th>Quantity:</th><th>Price:</th></tr>";
echo "<tr>";
echo "<td align=\"center\">$bikecode</td>";
echo "<td align=\"center\">$model</td>";
echo "<td align=\"center\">$quantity X</td>";
echo "<td align=\"center\">£$cost</td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\">Total</td>";
echo "<td align=\"right\">£$total</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"4\" align=\"right\">Empty Cart</td>";
echo "</tr>";
echo "</table>";
}else{
echo "You have no items in your shopping cart.";
}
function productExists($bikecode) {
$sql = sprintf("SELECT * FROM Bike WHERE BikeCode = '%s';", $bikecode);
return mysqli_num_rows(mysqli_query($con, $sql)) > 0;
}
?>
As long as you have a session_start(); on top of every page you should be able to use the exact same foreach loop as you do in this piece of coding. The session with the information would just be carried over to the next page.
Assume you have a page called test.php with the following code:
<?php
session_start();
$_SESSION['test'] = 1;
echo $_SESSION['test'];
?>
Then to access the test variable in othertest.php page,simply do the following:
<?php
session_start();
echo $_SESSION['test']; ?>
You forgot to add session_start() to the top of your php file. I recommend adding session_start() to a separate file and then including that file in each of the php pages that uses session variables.

Categories