Jump to content

send tow value over foreach


abdu808

Recommended Posts

hi every body

 

my database 

1st table =>(basket)
{b_id | basket_name | qu | basket_price | active | date_insert}
2nd table => (member)
{id | fileid | name | etc>>}
3d table =>(users)
{id | name | pass }
4nd table =>(operationlog)
{log_id | idno | fileid | basket_name | basket_price | user | }
 
my code is :
<form action="?p=addlog" method="post">
<div>
        <?php		 
		  $q=mysql_query("SELECT * FROM `basket` where active = 1 and qu >=1   ");
            if(mysql_num_rows($q)){
                while($m=mysql_fetch_assoc($q))
                {
				?>	
             <td> <input type="checkbox" name="basket[]" value="<?echo $m["basket_name"] ;?>" />
			   <?
			    echo "<span>".  $m["basket_name"]."</span> ";
			    }
			  ?>
	   			 <?
                }        
                ?>
</div></br>
<input type="submit" value="SEND" />

</form>
<?
if($_GET[p]=="addlog"){

$reqtime=date("h:i:s");

 foreach($_POST["basket"] as $basket1)
{
if(mysql_query("insert operationlog set 


idno='$_POST[curid]',fileid='$_POST[curfid]',basket_name='$basket1',basket_price='$basket1',username='{$_SESSION['loginname']}'"))
?>

it works fine but  basket_price  (basket_price='$basket1') dosnt send value , it send value = 0 

 

so i know  "foreach($_POST["basket"] as $basket1)" is the problem >

 

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/296405-send-tow-value-over-foreach/
Share on other sites

Your form only submits the the basket name in the form of a checkbox value

<td> <input type="checkbox" name="basket[]" value="<?echo $m["basket_name"] ;?>" />

The foreach loop will be looping through the values from the submitted checkboxes. The $basket1 variable will only contain the value from the checkbox (which is the basket_name). 

 

You are seeing 0 in the basket_price field  because its data type is an integer and its is being truncated to 0 because you are trying insert a string value into it (the baskek name). You need another input field to submit the basket price and insert that value into the basket_price field in your operationlog table.

 

But you should not be inserting the basket name/price into another table you should ideally be referencing those values using the primary key of the basket table as the foreign key in the operationlog table. Later on when you go to display the data from the operationlog table you would use a join to get the basket name/price values.

 

Also you should not be using raw $_POST data in your queries. You should be validating and sanitizing the input before using it in your queries. And note the mysql_ functions are deprecated (no longer supported) you should update your code to use PDO or MySQLi database api's

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.