invalid argument foreach() in PHP - php

I have a shopping cart with a foreach() to update the quantities.
HTML
<form method="post" >
<?php while ($data2 = mysql_fetch_array( $data)) { ?>
<tr>
<td> <input name="quantity" type="text" class="form-field" size="3" value="<? echo $data2['quantity'];?>" />
<input type="hidden" name="product_id" value="<? echo $data2['product_id']; ?>">
</td>
</tr>
<? } ?>
</form>
AND FOR MY PHP, I have
if (isset($_POST['submit_qty']))
{
foreach($_POST['product_id'] as $key => $id)
{
$item_id = $id;
$quantity = $_POST['quantity'][$key];
$sql2 = "update cart SET quantity = '".$quantity."' where product_id = '".$item_id."' ";
$result2 = mysql_query($sql2) or die ("Error in query: $result2");
}
header('Location: mycart.php');
exit();
}
IF I Echo my sql query I get :
Warning: Invalid argument supplied for foreach()
what could be the problem ?

You aren't passing your product_id as an array.
Try <input type="hidden" name="product_id[]" value="<? echo $data2['product_id']; ?>">

Related

How to disable button when returned books is equal with original books volume?

I am making a book management system.I have a return button ,with that button user can return books.
Here is my release-books.php:
<?php
$sql_select = "SELECT * FROM carti ORDER BY titlu";
$rezultat = mysqli_query($conn, $sql_select);
if (mysqli_num_rows($rezultat) > 0) {
while ($row = mysqli_fetch_assoc($rezultat)) {
$disabled = $row['stoc'] > 0 ? "" : "disabled"; ?>
<tr><td><input type="submit" name="id" value="<?php echo $row['idCarte']; ?>" <?php echo $disabled; ?> formaction="imprumutare.php"></input></td>
<td><input type="submit" name="returnare" value="<?php echo $row['idCarte']; ?>" formaction="returnare_carte.php"></input>
</td>
<td><input type="text" name="nume" value="<?php echo $row['titlu']; ?>" ></input></td>
<td><input type="text" name="" value="<?php echo $row['autor']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['editura']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['categorie']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['data_adaugarii']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['stoc']; ?>"></input></td>
</tr>
<?php
}
}
?>
And here is my borrowing.php
include('conexiune.php');
//sfarsit if
//Imprumutare
if (isset($_POST['id'])) {
$identificator = $_POST['id'];
$carte_nume = $_POST['nume'];
$current_date = date('y:m:d');
$current_date_plus_14days = date('y:m:d', strtotime("+14 days"));
$nume_carte = $_POST['nume'];
$insert_in_imprumuturi = "INSERT INTO imprumuturi(idc,nume_carte,data,termen_returnare,carti_imprumutate) VALUES('$identificator','$carte_nume','$current_date','$current_date_plus_14days','1')";
mysqli_query($conn, $insert_in_imprumuturi) or die(mysqli_error($conn));
$sql_rezervare = "UPDATE carti SET stoc=stoc-1 WHERE iDCarte='$identificator' ";
if (mysqli_query($conn, $sql_rezervare)) {
header('Refresh:0,url=emitere_carti.php');
} else {
die(mysqli_error($conn));
}
}
But I do not know how to disable the return button when the returned books volume is equal or greater that the originial volume
Can somone help me?
Check If your books volume is equal or greater than original volume, if it is. Use PHP echo to disable the Return Button.
<button type="submit" name="return"
<?php
if($returned_books_volume >= $original_books_volume){
echo 'disabled';
}
?>
>Return</button>

mysqli_num_rows() expects parameter 1 to be mysqli_result, null given

receiving this error:
mysqli_num_rows() expects parameter 1 to be mysqli_result, null given
Not sure why and have browsed a lot of topics on here trying to figure it out. I know it has something to do with my query, but I just can't put the pieces together.
Thanks for the assistance, I appreciate it!
<?php
session_start();
if (!isset($_SESSION['email'])){
echo "Please login first.";
exit();
}else{
include ("header.php");
require_once ('mysqli_connect.php');
$id=$_GET['id'];
$query = "SELECT * FROM post WHERE post_id=$id";
$result = #mysqli_query ($query);
$num = mysqli_num_rows($result);
if ($num > 0) {
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
?>
<form action="update.php" method="post">
<p>Title: <input name="title" size=50 value="<? echo $row['title']; ?>"></p>
<p>Author: <input name="author" size=50 value="<? echo $row['author']; ?>"></p>
<p>Post: <input name="post" size=50 value="<? echo $row['post']; ?>"></p>
<p>
<input type=submit value=update>
<input type=reset value=reset>
<input type=hidden name="post_id" value="<? echo $row['post_id']; ? >">
</form>
<?
}
}
}
?>
In the line $result = #mysqli_query ($query); the # will silence any errors. Check that $result is not false, null, or some other error before proceeding.
You may want to go without the # until you have it working so you can get more meaningful error messages.
The first thing is that in the last section the code is not properly intented..like value="<? echo $row['post_id']; ?..Then secondly as Joe said the # symbol supress errors..So you need to remove those #symbol from your code..So the code must be ..
<?php
session_start();
if (!isset($_SESSION['email'])){
echo "Please login first.";
exit();
}else{
include ("header.php");
require_once ('mysqli_connect.php');
$id=$_GET['id'];
$query = "SELECT * FROM post WHERE post_id=$id";
$result = mysqli_query ($query);
$num = mysqli_num_rows($result);
if ($num > 0) {
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
?>
<form action="update.php" method="post">
<p>Title: <input name="title" size=50 value="<? echo $row['title']; ?>"></p>
<p>Author: <input name="author" size=50 value="<? echo $row['author']; ?>"></p>
<p>Post: <input name="post" size=50 value="<? echo $row['post']; ?>"></p>
<p>
<input type=submit value=update>
<input type=reset value=reset>
<input type=hidden name="post_id" value="<? echo $row['post_id']; ?>">
</form>
Hope this one helps you ..If any problems regarding then comment below ..:)
I think you must check the value of GET['id'] is having value or not and query is formed with appropriate data.
You might be getting the error because the result is null.
and also check the connection.

Show data with the same name only once

I need to show my billing history, so user can automatically submit by clicking submit. But instead of showing all transactions, I only have to show for each billing_name, so I should use SELECT DISTINCT right?
<?php
$query3 = "SELECT DISTINCT billing_name, billing_reference_num FROM billing_history";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));
while ($row3 = mysqli_fetch_array($result3)) {
$billing_name = $row3['billing_name'];
$billing_reference_num = $row3['billing_reference_num'];
?>
<td><?php echo $billing_name ?></td>
<td><?php echo $billing_reference_num ?></td>
<?php
$queryBill = "SELECT * FROM billing_history WHERE billing_name = '$billing_name'";
$resultBill = mysqli_query($link, $queryBill) or die(mysqli_error($link));
while ($row4 = mysqli_fetch_array($resultBill)) {
$billing_name_id = $row4['billing_name_id'];
$account_id = $row4['account_id'];
$account_number = $row4['account_number'];
}
?>
<td><input type="text" name="transaction_amount"/></td>
<td><input type="submit" value="Submit"/></td>
<input type="hidden" name="billing_name_id" value="<?php echo $billing_name_id ?>"/>
<input type="hidden" name="billing_name" value="<?php echo $billing_name ?>"/>
<input type="hidden" name="billing_reference_num" value="<?php echo $billing_reference_num ?>"/>
<input type="hidden" name="account_number" value="<?php echo $account_number ?>"/>
<input type="hidden" name="account_id" value="<?php echo $account_id ?>"/>

saving data from an array without submitting any form php

I'm tryng to save an array so I have the following code:
<?php
$sql = "SELECT * FROM scenarii where code_s='".mysql_real_escape_string($_POST['code_s'])."'";
$qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
$i = -1; // index des enregistrements
?>
<table cellpadding="5" cellspacing="5">
<tr>
<td><strong>CODE SCENARIO</strong></td>
<td><strong>LIBELLE</strong></td>
<td><strong>ACTION</strong></td>
<td><strong>DESCRIPTION</strong></td>
<td><strong>DATE</strong></td>
</tr>
<form action="<?php echo (isset($_POST['go'])) ? 'go.php' : '#'; ?>" method="post">
<input type="hidden" name="liasse" value="<?php echo $_POST['liasse']; ?>"/>
<input type="hidden" name="n_doss" value="<?php echo $_POST['n_doss']; ?>"/>
<input type="hidden" name="qualite" value="<?php echo $_POST['qualite']; ?>"/>
<?php while($row = mysql_fetch_assoc($qry)): ?>
<tr>
<td><input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="<?php echo $row['code_s'];?>" size="10"></td>
<td><input name="data[<?php echo $i; ?>][titre]" type="text" value="<?php echo $row['titre']; ?>" size="45"></td>
<td><input name="data[<?php echo $i; ?>][action]" type="text" value="<?php echo $row['action']; ?>" size="15"></td>
<td><input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['libelle']; ?>" size="55"></td>
<td><input type="text" name="data[<?php echo $i; ?>][date]" value="<?php echo $get_date($row['jour']) ; ?>" size="12"></td>
</tr>
<?php endwhile; ?>
And in order to save this I have this code:
if (isset($_POST['liasse'])) {
$value = $_POST['data'] ;
foreach($value as $key => $array)
{
$sql = 'INSERT INTO agenda SET
liasse = "'.mysql_real_escape_string($_POST['liasse']).'",
code_s = "'.mysql_real_escape_string($array['code_s']).'",
date_action = "'.date('Y-m-d',strtotime($array['date'])).'",
libelle = "'.mysql_real_escape_string($array['titre']).'",
action = "'.mysql_real_escape_string($array['action']).'",
description = "'.mysql_real_escape_string($array['libelle']).'",
n_doss = "'.mysql_real_escape_string($_POST['n_doss']).'",
qualite = "'.mysql_real_escape_string($_POST['qualite']).'"
';
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
}
But I'm really lost,
In fact I use a form for that, now I would like to submit all of this data but without any form, directly when I have the first while I would like to save it.
The thing is that I'm lost because I can not call any var like that data[][code_s].
So I do not know how to save this. I would like to save it in background, and not to display that something has been saved.
Receive all my Utmost Respect
kind regards,
SP.
Wrap the code od the lower code block into a function and hand over the value array as argument:
function storeValues ($data) {
foreach($data as $key => $val)
{
$catalog=sprintf("%s='%s'",$key,$val);
$sql = sprintf('INSERT INTO agenda SET %s', implode(',',$catalog));
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
} // foreach
} // function storeValues
You call this function when you want to save the values. So after retrieving them from the database. You call it for each row you retrieve and hand over the values like that:
storeValues ($row);
This will store one row of values at a time. Obviously this can be optimized to use a multiple insert. But let's take one step after another...

Update cart - mysql table update - while loop

new guy here!
I am building a custom shopping cart driven by mysql and i am trying to update my cart item by item in terms of quantity. It seems that i am doing something wrong because when i try to update the quantity it only update the last item. I am posting the code below. Any help would be appreciated. Thanks in advance.
1.cart.php:
$sql = "select * from orders";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
echo "Στοιχεία: ".$num;
?>
<form name="cart" method="post" action="updatecart.php">
<table border="2">
<tr>
<td>Α/Α</td>
<td>img_name</td>
<td>Reprints</td>
<td>Color</td>
</tr>
<?php
if($num){
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['item_id']; ?></td>
<td><?php echo $row['img_name']; ?></td>
<td><input type="number" name="quantity" value="<?php echo $row['quantity']; ?>"></td>
<input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">
<td><?php echo $row['color']; ?></td>
</tr>
<?php
}
}
?>
</table>
<input type="submit" name="update" value="Update Cart" />
<input type="button" name="2checkout" value="Proceed to Checkout" />
</form>
2.updatecart.php
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
$database_name = "vog";
$conn = mysql_connect("localhost","root","toor");
mysql_select_db($database_name);
$num = 2; //na to ferw me session meta edw!
if(isset($_POST['update'])){
$item_id = $_POST['item_id'];
$i=1;
while($i<=$num){
$item_id = $_POST['item_id'][$i];
$quantity = $_POST['quantity'];
$sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
$result2 = mysql_query($sql2) or die ("Error in query: $result2");
$i++;
}
}
if(isset($result2)){
header("Location:cart.php");
}
?>
So far it updates just the last record.
Your problem is with the names of the fields in your HTML form:
<input type="number" name="quantity" value="<?php echo $row['quantity']; ?>">
<input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">
I think you meant to call them quantity[] and item_id[] instead, so they will and up as arrays in your $_POST variable later on, now they overwrite eachother, making $_POST['item_id'] only contain the last id in the database.
in #1.cart.php use the inputs as array:
<input type="number" name="quantity[<?php echo $row['item_id']; ?>]" value="<?php echo $row['quantity']; ?>">
<input type="hidden" name="item_id[<?php echo $row['item_id']; ?>]" value="<? echo $row['item_id']; ?>">
and in #2.updatecart.php: process it like
foreach($_POST['item_id'] as $key => $id){
$item_id = $id;
$quantity = $_POST['quantity'][$key];
$sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
$result2 = mysql_query($sql2) or die ("Error in query: $result2");
$i++;
}
You need to tell PHP that you're using an array for your submitted form items. The way to do this is to make the name of each input quantity[]. You can also place the item ID directly in the array as a key. In cart.php you can do this in your loop:
<input type="number" name="quantity[<?php echo $row['item_id']; ?>]"
value="<?php echo $row['quantity']; ?>"/>
In effect, this will putput something like:
<input type="number" name="quantity[2]" value="1" />
<input type="number" name="quantity[4]" value="1" />
<input type="number" name="quantity[8]" value="2" />
i.e. 1 of item 2, 1 of item 4 and 2 of item 8.
Then, in updatecart.php you can read in the array and process it in a loop.
if(isset($_POST['update'])){
foreach ($_POST['quantity'] as $item_id => $item_qty) {
$item_id = (int)$item_id;
$item_qty = (int)$item_qty;
$sql2 = "update orders SET quantity = '$item_qty' where item_id = '$item_id' ";
mysql_query($sql2);
}
}

Categories