billgod Posted December 17, 2008 Share Posted December 17, 2008 I have no idea where to begin here. I am pretty new to php and am writing a site trying to learn the language. I have a form that populates from a select statement from my database. on this page you select a few random inputs via check boxes. hit the submit button. Those check boxes pass my ID's of a table. I need to be able to run a query against those ID's. So I basically need to generate a dynamic query based upon the post. I hope this make sense to some one. From what I read so far I need to use $_POST['query'] but have no idea how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/ Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 ummm Post some of your code for better help. It is hard to code "blind" as it be. <?php foreach ($_POST as $key => $val) { if (stristr($key, "chk")) { $ids[] = $val; } } $ids = implode(", ", $val); $sql = "SELECT * FROM table_name WHERE column IN(" . $ids . ")"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { // etc etc } ?> The above is assuming that in your form you use "chk" as part of each checkbox's name. Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-717290 Share on other sites More sharing options...
billgod Posted December 17, 2008 Author Share Posted December 17, 2008 Thanks for the quick response. Nice to see a forum people participate in. here is the code for the form. (i know most of it is probably crappy code) <?php ob_start(); $host="cartman"; // Host name $username="bill"; // Mysql username $password="mypassword"; // Mysql password $db_name="heroscape"; // Database name $tbl_name="characters"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE available='yes'"; $result=mysql_query($sql); ?><head> <script type="text/javascript" src="lightbox.js"></script> <TITLE></TITLE></head> <form action="dopickteamverify.php" method="POST"> <table border="1" cellpadding="1"> <tr> <td><? echo "Choose"; ?></td> <td width="35"><? echo "Image"; ?></td> <td width="150"><? echo "Name"; ?></td> <td ><? echo "Points"; ?></td> <td><? echo "Life"; ?></td> <td><? echo "Move"; ?></td> <td><? echo "Range"; ?></td> <td><? echo "Attack"; ?></td> <td><? echo "Defense"; ?></td> <!--<td><? echo "Special"; ?></td>--> </tr> <? while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? $myid=$rows['id']; echo "<input name='$myid' type='checkbox' id='$myid'>"; ?></td> <td width="25"><? $myimage= $rows['images']; echo "<a href='$myimage' rel='lightbox'><img src='$myimage' width='35'></a> " ; ?></td> <td width="150"><? echo $rows['name']; ?></td> <td ><? echo $rows['points']; ?></td> <td><? echo $rows['life']; ?></td> <td><? echo $rows['move']; ?></td> <td><? echo $rows['range']; ?></td> <td><? echo $rows['attack']; ?></td> <td><? echo $rows['defense']; ?></td> <!--<td><? echo $rows['special']; ?></td>--> </tr> <? // close while loop } ?> </table> <input type="submit" name="Submit" value="Go"> </form> <? // ob_end_flush(); ?> Now I need to update the database to set column "available" to no where ID=X Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-717320 Share on other sites More sharing options...
billgod Posted December 18, 2008 Author Share Posted December 18, 2008 Anyone tell me how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-718733 Share on other sites More sharing options...
ngreenwood6 Posted December 18, 2008 Share Posted December 18, 2008 You might want to look into the update function: mysql_query("UPDATE table SET field='value'"); Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-718737 Share on other sites More sharing options...
sasa Posted December 18, 2008 Share Posted December 18, 2008 change line <td><? $myid=$rows['id']; echo "<input name='$myid' type='checkbox' id='$myid'>"; ?></td> to <td><? $myid=$rows['id']; echo "<input name='id[]' value='$myid' type='checkbox' id='$myid'>"; ?></td> and in next page print_r($_POST['id']); Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-718837 Share on other sites More sharing options...
billgod Posted December 18, 2008 Author Share Posted December 18, 2008 I am aware of how to do an SQL query to update a record. print_r($_POST['id']); Is already in my second page for testing. It does list all my 'ID' on the screen. What I need to know is how to build an update query from the values returned in the $_post. I will try Sasa's changes tonight but have a feeling I will still be in the same boat since I don't know how to loop through the $_POST variable and query on each ID. Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-719108 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 foreach ($_POST as $key => $val) { if ($key == "id") { foreach ($val as $val2) { $ids[] = $val2; } } } $ids = implode(", ", $ids); mysql_query("UPDATE table_name SET col1 = 'test' WHERE id IN(" . $ids . ")"); Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-719113 Share on other sites More sharing options...
billgod Posted December 18, 2008 Author Share Posted December 18, 2008 that looks like its what i need but i get "Warning: implode() [function.implode]: Bad arguments." when I try and access the page. Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-719311 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 <?php if (isset($_POST['id']) && is_array($_POST['id'])) { foreach ($_POST['id'] as $key => $val) { foreach ($val as $val2) { $ids[] = $val2; } } $ids = implode(", ", $ids); mysql_query("UPDATE table_name SET col1 = 'test' WHERE id IN(" . $ids . ")"); }else { echo 'No POST data'; } Chances are it was because the post data wasnt there. Try that and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-719324 Share on other sites More sharing options...
billgod Posted December 19, 2008 Author Share Posted December 19, 2008 Ok think i have it working. little variable issue but I think I can get that. Thanks for everyones help Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-719325 Share on other sites More sharing options...
billgod Posted December 19, 2008 Author Share Posted December 19, 2008 ok last thing. I cant get the input name ='whatever' to change. I have <input name='id[]'..... when I view source on the page it says <input name='id[]' I am assuming it's supposed to increment. Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-719327 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 It increments on it's own, accessing it like the above will grab the ids from the checkboxes where the name is id. So yea the source will not increment, but when it is posted it will. Quote Link to comment https://forums.phpfreaks.com/topic/137285-need-to-query-mysql-databe-with-random-number-of-variables-from-a-post/#findComment-719329 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.