Jump to content

[SOLVED] posting variables


steveh62

Recommended Posts

I have a number of variables which I post and collect via $var = $_POST['something'];

 

in particular the following gets sent by post

<?php
$labels = array(
            0 => '200mm x 200mm',
            1 => '200mm x 300mm',
            2 => '200mm x 400mm',
            3 => '200mm x 500mm',
            4 => '200mm x 600mm',
        );

foreach ($labels as $ar => $label) {
    print '<label>' .$label. '</label><input id="sizes" name="sizes[' .$ar. ']" type="checkbox" onclick="setChecks(this)" value="' . $label. '"';
    if ($ar == 0){
 // print "checked";
 }
   print ' /><input id="price" name="price[' .$ar. ']" type="text" class="dropdown" /><br/>';	
}

?>

and then picked up and used like

 

<?php
$foo = $_POST['sizes'];
	//if(isset($_POST['$foo'])){
	if (count($foo > 0)) {
	// loop through the array
	foreach ($foo as $key => $size) {
    //echo $size.'<br/>';


$sprice = $_POST['price'][$key];
//echo 'price:'.$sprice;

	//for ($i=0;$i<count($foo);$i++)
	// {//now insert sizes in table spcanvas_sizes
mysql_query( "INSERT INTO spcanvas_sizes(`sizes`, `canv_price`, `pid`) VALUES('".$size."', '".$sprice."', '".$id."')" );
	// do something - this can be a SQL query,
	// echoing data to the browser, or whatever
	//echo "<li>$foo[$i] \n";

	}  //end for loop

	}  //end if
?>

 

THE PROBLEM IS...the data does get POSTED yet the database insert an extra record at the begining with nothing inserted

 

ANY IDEAS ANYONE

Link to comment
https://forums.phpfreaks.com/topic/142795-solved-posting-variables/
Share on other sites

Basically, what happens is the array makes up 5 checkboxes with associated price inputs and on submitting gets posted.

As part of the submittion, data and images get inserted to a db table and is assigned an id.

 

**as part of knowing where to associated the sizes the code picks out the latest id and uses that to insert the sizes and prices to a particular id.

 

But on the live test site the db is inserting a BLANK row which in turn gets pushed to the user as option 1 being blank. This does not happen locally tho'

<?php
$labels = array(
            0 => '200mm x 200mm',
            1 => '200mm x 300mm',
            2 => '200mm x 400mm',
            3 => '200mm x 500mm',
            4 => '200mm x 600mm',
        );

foreach ($labels as $ar => $label) {
    print '<label>' .$label. '</label><input id="sizes" name="sizes[' .$ar. ']" type="checkbox" onclick="setChecks(this)" value="' . $label. '"';
    if ($ar == 0){
 // print "checked";
 }
   print ' /><input id="price" name="price[' .$ar. ']" type="text" class="dropdown" /><br/>';	
}

?>

the above gets posted to below

<?php
  if(get_magic_quotes_gpc()) {
            $gallerytype = stripslashes($_POST['gallerytype']);
            $wrap = stripslashes($_POST['wrap']);
		$frame = stripslashes($_POST['frame']);
		$title = stripslashes($_POST['title']);
		$description = stripslashes($_POST['description']);
		$keywords = stripslashes($_POST['keywords']);
		$category = stripslashes($_POST['category']);
		$cat = stripslashes($_POST['cat']);
		$foo2 = stripslashes($_POST['sizes2']);
		$price = stripslashes($_POST['price']);
		$sprice = stripslashes($_POST['price2']);
		$foo = stripslashes($_POST['sizes']);
        } else {
            $gallerytype = $_POST['gallerytype'];
            $wrap = $_POST['wrap'];
		$frame = $_POST['frame'];
		$title = $_POST['title'];
		$description = $_POST['description'];
		$keywords = $_POST['keywords'];
		$category = $_POST['category'];
		$cat = $_POST['cat'];
		$foo2=$_POST['sizes2'];
		$price = $_POST['price'];
		$sprice = $_POST['price2'];
		$foo = $_POST['sizes'];
        }

			/////////load it////////////
			mysql_query( "INSERT INTO gallery(`galleryType`, `print`, `frame`, `title`, `filename`, `desc`, `keywords`, `gid`, `gCATID`) VALUES('".$gallerytype."', '".$wrap."', '".$frame."', '".$title."', '".addslashes($thumbname)."', '".$description."', '".$keywords."', '".$category."', '".$cat."')" );	 

		//now retrieve last item from gallery as it will need to have at least 1 size applied to spsizes table
		$qry = mysql_query("SELECT * FROM gallery ORDER BY id DESC LIMIT 1");
		while( $row = mysql_fetch_array( $qry ) )
{
$id = stripslashes($row['id']);
echo 'latest id:'.$id;
}
            imagejpeg($tn, $save, 80) ; 
            echo "<span class='dropdown'>Large image:</span> <img src='".$images_dir."/".$imagepath."'><br>"; 
            echo "<span class='dropdown'>Thumbnail:</span><img src='".$images_dir."/tb_".$imagepath."'>"; 



////get sizes////////////////////////////
////select for specials one only size and price
if(isset($foo2)){
 mysql_query( "INSERT INTO spcanvas_sizes(`sizes`, `canv_price`, `pid`) VALUES('".$foo2."', '".$sprice."', '".$id."')" );
 }
 ///checkbox////
if(isset($_POST['sizes'])){ 
	$foo = $_POST['sizes'];
	//if(isset($_POST['$foo'])){
	if (count($foo > 0)) {
	// loop through the array
	foreach ($foo as $key => $size) {
    echo $key.'<br/>';


$sprice = $_POST['price'][$key];
//echo 'price:'.$sprice;

	//for ($i=0;$i<count($foo);$i++)
	// {//now insert sizes in table spcanvas_sizes
mysql_query( "INSERT INTO spcanvas_sizes(`sizes`, `canv_price`, `pid`) VALUES('".$size."', '".$sprice."', '".$id."')" )or die(mysql_error());
	// do something - this can be a SQL query,
	// echoing data to the browser, or whatever
	//echo "<li>$foo[$i] \n";

	}  //end for loop

	}  //end if count
	}//isset sizes
?>

so does anybody know why the submittion is inserting an empty entry before inserting the data [sizes] and an associative [price]

 

the array gets looped thru the size array we price[$key] - as $key is the pointer for the size array - so in essence it shuold get the data from 0 to 4 if they are checked and as we loop it inserts the data into the table 'spcanvas_sizes.

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.