jlgray48 Posted April 24, 2007 Share Posted April 24, 2007 I have created a database named PHP012 and a table within the database named Inventorytable. The inventorytable has 4 fields named ItemID ItemName Itemprice ItemQuantity. I have created an html form with 3 checkboxes named Item1, Item2, Item3 and values of 1, 2, 3. The Html form passes the checkboxes that are checked to a php file named order.php. The php file is to take the passed data and update the Itemquantity if the box was checked. I am having problems creating my loops. That may not be needed just let me know if that is the case. What I'm trying to do in the code below is start for loop from 1 to 3 to check all 3 possible passed values. If the value is null or doesn't exist then it want enter the while loop for processing. I don't know the code for null or empty so I put not empty in the code. <?php $Item1 = $_REQUEST[item 1]; $Item2 = $_REQUEST[item 2]; $Item3 = $_REQUEST[item 3]; $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn); For ($i = 1; $i <=3 ; $i+=1) { While $Item.$i (not empty) { Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item.$i ; } Quote Link to comment Share on other sites More sharing options...
Garath531 Posted April 24, 2007 Share Posted April 24, 2007 Can I have the whole code please?(In tags.) Quote Link to comment Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 As in the other thread: <?php $Item1 = $_REQUEST[item 1]; $Item2 = $_REQUEST[item 2]; $Item3 = $_REQUEST[item 3]; $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn); For ($i = 1; $i <=3 ; $i+=1) { While $Item.$i (not empty) { Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item.$i ; } ?> Try: <?php $Item1 = $_REQUEST[item 1]; $Item2 = $_REQUEST[item 2]; $Item3 = $_REQUEST[item 3]; $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn); For ($i = 1; $i <=3 ; $i++) { While ($Item.$i == FALSE) { Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item.$i ; } } ?> Quote Link to comment Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 Here, I would like you to try this, please tell me what it prints: <?php $Item1 = $_REQUEST[item 1]; $Item2 = $_REQUEST[item 2]; $Item3 = $_REQUEST[item 3]; for ($i = 1; $i <=3 ; $i++) { echo $Item.$i; } ?> Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 Ok, I can see how I've confused you. I have passed from an html file ITEM1, ITEM2 and ITEM3 and stored them in $ITEM1 $ITEM2 and $ITEM3. I am creating a loop to see if they are empty if they aren't empy I want the sql query to run. My problem is withing the $item.$i I'm tryint to combine the 2 to create the variable above. I need to check $Item[$i] and I can't figure it out. I may have confused you more. Quote Link to comment Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 How about something like this: <?php $conn = mysql_connect("127.0.0.1", "odbc", "") or die("ERROR: Cannot connect to database"); mysql_select_db("php012",$conn) or die("ERROR: Cannot select database"); for ($i = 1; $i <=3 ; $i++){ $item = $_REQUEST['Item ' . $i]; echo "$item (just checking)<br><br>"; // Remove this line later, just to check the variables coming through if(!empty($item)) { mysql_query("UPDATE inventorytable SET Itemquantity = Itemquantity - 1 WHERE ItemID = '$item'") or die(mysql_error()); } } ?> The only thing im not really sure of there is the syntax of the UPDATE query where its subtracting Quote Link to comment Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 Ok, I can see how I've confused you. I have passed from an html file ITEM1, ITEM2 and ITEM3 and stored them in $ITEM1 $ITEM2 and $ITEM3. I am creating a loop to see if they are empty if they aren't empy I want the sql query to run. My problem is withing the $item.$i I'm tryint to combine the 2 to create the variable above. I need to check $Item[$i] and I can't figure it out. I may have confused you more. No, I think I understand. I still want you to try the following: <?php $Item1 = $_REQUEST[item 1]; $Item2 = $_REQUEST[item 2]; $Item3 = $_REQUEST[item 3]; for ($i = 1; $i <= 3 ; $i++) { echo $Item.$i . "<br />"; } ?> I would like to see what your results are. Based on those results, I may be able to help you more. Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 The guy above is real close $Item.$i prints nothing Quote Link to comment Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 Oh, OK, then try this: <?php for ($i = 1; $i <= 3 ; $i++) { $Item = $_REQUEST['Item'.$i]; echo $Item."<br />"; } ?> If the above worked, try: <?php for ($i = 1; $i <= 3 ; $i++) { $Item = $_REQUEST['Item'.$i]; if ($Item == FALSE) { $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn); Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item; } } ?> Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 I think we be close I'm checking on the if $item==false. I want the code to run if it is set. How do you use the !isset command? Quote Link to comment Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 Basically: isset($variable) - then the $variable is set. !isset($variable) - then the $variable is NOT set. Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 I just got to figure out how to say if ($item not empty) { If i plug in if $item = 1 it works like a champ. Quote Link to comment Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 Yeah, 1 is the same as saying TRUE or isset($variable) or the list goes on... If the last script worked backwards, use this: <?php for ($i = 1; $i <= 3 ; $i++) { $Item = $_REQUEST['Item'.$i]; if ($Item == TRUE) { $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn); Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item; } } ?> Quote Link to comment Share on other sites More sharing options...
Glyde Posted April 24, 2007 Share Posted April 24, 2007 If you're checking for a TRUE statement, you can omit the == TRUE as it is assumed within PHP if statements. if ($someVar == TRUE) Is the same as saying if ($someVar) Quote Link to comment Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 If you're checking for a TRUE statement, you can omit the == TRUE as it is assumed within PHP if statements. if ($someVar == TRUE) Is the same as saying if ($someVar) True! jlgray48 recently made a different thread asking how to say not empty. And I think he is finally realizing that we weren't joking about how many ways to say that in php! Not Empty equals: if ($variable) if (isset($variable)) if ($variable == 1) if ($variable != 2) if ($variable == TRUE) if ($variable != FALSE) if (!empty($variable)) Should I continue!? lol Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 OK, it is done. Boy did I make this way too hard. THANKS A MILLION FOR EVERYONES HELP. <?php $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn); for ($i = 1; $i <=3 ; $i++) { $item = $_REQUEST['Item'.$i]; If ($item == true) { echo "$item"; $sql = "Update inventorytable set Itemquantity = Itemquantity -1 where ItemID = $item;"; mysql_query($sql,$conn); } } ?> Quote Link to comment Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 And, when putting if true or false should also use 3 = eg: if($somevar === true) or if ($somevar === false) Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 SANFLY, Would you happen to know how to create a listbox and have the contents of the listbox come from a mysql db? jg Quote Link to comment Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 Yes I would Of course you will have to adapt the code below for your own database structure, but it should give you an idea... Im also assuming short tags are enabled.... Basically, you just extract the option values and names using the while() function <select name="selectMenu"> <? // db connection info goes here $r = mysql_query("SELECT * FROM tablename WHERE somefield = '$somevalue'") or die(mysql_error()); while($row = mysql_fetch_array($r)){ $field_id = $row['field_id']; $field_name = $row['field_name']; ?> <option value="<?=$field_id?>"><?=$field_name?> <? } ?> </select> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.