Jump to content

Help with arrays of hidden values.


ali_2kool2002

Recommended Posts

Hi im using a while loop to go through my query from the database. I get a product id and store it to the variable  using this line $productId = $results['productId']; I then try printing it on screen the value and comes up with 1, 2, 3, 4, which is correct. The 4 buttons are also displayed with the following code

 $hidenval[$i] = $results['productId'];

This is used to store the product id into an array

 

This is where its applied to the hidden value

<input type="hidden" name="sel_item_id[]" value="'.$productId.'">';

 

However, when i click on one of the 4 buttons on the actual output of this code i get an error message because im not sure how to check the value passed by the button clicked. E.g. if a user clicks button 1 then the value passed should be of productId x, and if button 2 is pressed the value passed in the hidden value should be some other value acording to the product id retrieved from the database. Is there any chance some1 could show me how to make this handle form check the right value. I think its something to do with how im checking the value of the sel_item_id in the handle form with the productId in the databse but im not sure how u compare it with arrays: I Get an error on my handle form saying "Unknown column 'Array' in 'where clause'" which is probable to do with the comparison i am doing in the query in the handle form which has the "where" clause

 

$get_iteminfo = "select productName from product where productId=$_POST[sel_item_id]";

 

the code of the main thing is this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?
//connect to the DB
session_start();

require_once('mysql_connect.php');
$display_block = "<h1>PRODUCTS - Product Detail</h1>";

//validate item
$get_item = "select c.catId as catId, c.cat_name, p.productId,p.productName, p.price, p.productDescription, p.image from product as p left join 
category as c on c.catId = p.catId";

$get_item_res = mysql_query($get_item) or die(mysql_error());

if ($get_item_res) {



echo '<table align="center" cellspacing="1" cellpadding="5">
<tr><td align="left"><b>Cat Id</b></td>
<td align="center"><b>  Category Name</b></td>
<td align="center"><b>  Item Name</b></td>
<td align="center"><b>  Item Description</b></td>
<td align="center"><b>  Images</b></td>
<td align="center"><b>  Price</b></td>
</tr></table>';

while ($results = mysql_fetch_array($get_item_res, MYSQL_ASSOC))
      { 
//variable to hold image file name from array
extract($results);
$image = $results['image']  ;	
//variable to hold productId from array
//$productId = $results['productId'];
$hidenval[$i] = $results['productId'];
//echo $productId;
echo $hidenval[$i];
}

for( $x=0; $x < $hidenval[$i]; $x++ ){

echo '<form method = post action="addtocart.php">';									

echo'<INPUT TYPE ="submit" name = "submit" value = "Add to Cart" maxlength "50">
<input type="hidden" name="sel_item_id[]" value="'.$productId.'">';
}
//	echo '</table>';
//echo $display_block;
} else 
{ // If it did not run OK.
echo '<p>The current products could not be retrieved.
We apologize for any inconvenience.</p>'; 
echo '<p>' . mysql_error() . '</p>';
}
mysql_close(); 


?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

 

The CODE OF THE HANDLE FORM I HAVE IS

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();
$PHPSESSID = session_id(); 
require_once('mysql_connect.php');
//if (isset($_POST['submitted'])) 
if ($_POST['sel_item_id'] != null) 
{
//echo $x = $_POST['sel_item_id[]'];


$get_iteminfo = "select productName from product where productId=$_POST[sel_item_id]";
$get_iteminfo_res = mysql_query($get_iteminfo) or die(mysql_error());

if (mysql_num_rows($get_iteminfo_res) < 1) {
   	    //invalid id, send away
   	    echo "query failed";
	//header("Location: homepage.php");
   	    exit;
}else {
   	    //get info
	 	    $item_title =  mysql_result($get_iteminfo_res,0,'productName');
	echo $item_title;
	//add info to cart table
	$id = 0;

   	    $addtocart = "insert into store_shoppertrack values ('$id', '$PHPSESSID', '$_POST[sel_item_id]', '2', now())";
	$id= 1 + $id;
	$addquery = mysql_query($addtocart) or die (mysql_error());


	//header("Location: showcart.php");
  	    exit;
} }else {
   echo "//send them somewhere else";
  //  header("Location: homepage.php");
    exit;
}

?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/38183-help-with-arrays-of-hidden-values/
Share on other sites

SORRY IGNORE THE 1ST CODE IV GIVEN FOR THE MAIN THING AS THIS CODE BELOW IS THE CORRECT 1 ,, THE HANDELING FORM CODE IS STILL THE SAME AS THE CODE GIVEN BEFORE  :'(

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?
//connect to the DB
session_start();

require_once('mysql_connect.php');
$display_block = "<h1>PRODUCTS - Product Detail</h1>";

//validate item
$get_item = "select c.catId as catId, c.cat_name, p.productId,p.productName, p.price, p.productDescription, p.image from product as p left join 
category as c on c.catId = p.catId";

$get_item_res = mysql_query($get_item) or die(mysql_error());

if ($get_item_res) {



echo '<table align="center" cellspacing="1" cellpadding="5">
<tr><td align="left"><b>Cat Id</b></td>
<td align="center"><b>  Category Name</b></td>
<td align="center"><b>  Item Name</b></td>
<td align="center"><b>  Item Description</b></td>
<td align="center"><b>  Images</b></td>
<td align="center"><b>  Price</b></td>
</tr></table>';

while ($results = mysql_fetch_array($get_item_res, MYSQL_ASSOC))
      { 
//variable to hold image file name from array
extract($results);
$image = $results['image']  ;	
//variable to hold productId from array
//$productId = $results['productId'];
$hidenval[$i] = $results['productId'];
//echo $productId;
echo $hidenval[$i];
}

for( $x=0; $x < $hidenval[$i]; $x++ ){

echo '<form method = post action="addtocart.php">';									

echo'<INPUT TYPE ="submit" name = "submit" value = "Add to Cart" maxlength "50">
<input type="hidden" name="sel_item_id[]" value="'.$hidenval[$i].'">';
}
//	echo '</table>';
//echo $display_block;
} else 
{ // If it did not run OK.
echo '<p>The current products could not be retrieved.
We apologize for any inconvenience.</p>'; 
echo '<p>' . mysql_error() . '</p>';
}
mysql_close(); 


?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.