jason310771 Posted April 5, 2011 Share Posted April 5, 2011 how to cross reference $_POST array 'checkboxes[]' with mysql field I have a form that is submitted so the user can select which results to print. but I am having problems getting the results to show correctly. how do I correctly cross reference the form data with the mysql $getRequests = mysql_query("SELECT * FROM requests WHERE `customer_email`='{$_SESSION['FM_user']}'"); while($request = mysql_fetch_assoc($getRequests)) { if (!in_array($request['request_id'],array($_POST[request]))) { echo '<li style="font-size:10px; border-bottom: 0.1em solid #D0D0D0"> <div class="leftfloat"> <input type="checkbox" name="request[]" value="',$request['request_id'],'"> </div> <div class="leftfloat"> <em>',$request['request_id'],'</em> </div><div class="leftfloat"> | </div> <div class="leftfloat"> <em>',$request['customer_name'],'</em> </div><div class="leftfloat"> | </div> <br style="clear:both"> </li>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/ Share on other sites More sharing options...
Psycho Posted April 5, 2011 Share Posted April 5, 2011 You are doing it the hard way. Instead of querying for all the records and then filter out the ones you don't want - simply query for only the records you need using the $_POST[request] value as a parameter in the SELECT query. //Clean the POST data array for query purposes $requestIDs = implode(',', array_map('intval', $_POST['request'])); //Create/run query to get only the records needed $query = "SELECT * FROM requests WHERE `customer_email`='{$_SESSION['FM_user']}' AND `request_id` IN ({$requestIDs})" $getRequests = mysql_query($query); //Display the valid results while($request = mysql_fetch_assoc($getRequests)) { echo "<li style=\"font-size:10px; border-bottom: 0.1em solid #D0D0D0\">\n"; echo " <div class=\"leftfloat\"> <input type=\"checkbox\" name=\"request[]\" value=\"{$request['request_id']}\"></div>"; echo " <div class=\"leftfloat\"> <em>{$request['request_id']}</em> </div><div class=\"leftfloat\"> | </div>\n"; echo " <div class=\"leftfloat\"> <em>{$request['customer_name']}</em> </div>\n"; echo " <div class=\"leftfloat\"> | </div>\n"; echo " <br style=\"clear:both\">\n"; echo "</li>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/#findComment-1197329 Share on other sites More sharing options...
jason310771 Posted April 5, 2011 Author Share Posted April 5, 2011 hey thank you, yes that makes sense, but using this causes all records to show still. Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/#findComment-1197337 Share on other sites More sharing options...
jason310771 Posted April 5, 2011 Author Share Posted April 5, 2011 this is the code that shows the initial form they select from <form method="post" action=""> <ul> <? while($request = mysql_fetch_assoc($getRequests)) { echo '<li class="field" style="font-size:10px; border-bottom: 0.1em solid #D0D0D0"> <div class="customerViewEditLink"> <input type="checkbox" name="request[]" value="',$request['request_id'],'"> </div> <div class="customerRequestIDSpacer"> <em>',$request['request_id'],'</em> </div><div class="customerListSpacer"> | </div> <div class="customerNameSpacer"> <em>',$request['customer_name'],'</em> </div><div class="customerListSpacer"> | </div> <div class="customerMobileSpacer"> <em>',$request['customer_mobile'],'</em> </div><div class="customerListSpacer"> | </div> <br style="clear:both"> </li>'; } ?> </ul> <input type="submit" name="PRINTSELECTED" value="PRINT SELECTED"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/#findComment-1197344 Share on other sites More sharing options...
Psycho Posted April 5, 2011 Share Posted April 5, 2011 There are no input fields on that form. What is the user supposed to be selecting? Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/#findComment-1197475 Share on other sites More sharing options...
jason310771 Posted April 6, 2011 Author Share Posted April 6, 2011 the input field is in the sixth line <input type="checkbox" name="request[]" value="',$request['request_id'],'"> Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/#findComment-1197507 Share on other sites More sharing options...
Psycho Posted April 6, 2011 Share Posted April 6, 2011 OK, the form that submits to the 2nd page has a field called 'request_id', but your code is referencing the field as $_POST['request'] Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/#findComment-1197545 Share on other sites More sharing options...
jason310771 Posted April 6, 2011 Author Share Posted April 6, 2011 This is my full code I have added in echo lines to display the content of the full query that is being used to get the data from mysql this is the output of these two lines. echo("<br><br>".$requestIDs."<br><br>"); shows 0 and echo("SELECT * FROM requests WHERE `customer_email`='{$_SESSION['FM_user']}' AND `request_id` IN ({$requestIDs})"); shows SELECT * FROM requests WHERE `customer_email`='[email protected]' AND `request_id` IN (0) <?PHP session_start(); // viewPreviousRequests.php if(!isSet($_SESSION['FM_user'])) { $_SESSION['error'] = 'You must be logged in to view that page.'; header('Location: login.php'); exit; } else { include('includes/connection.php'); $user = mysql_query("SELECT * FROM customers WHERE email='{$_SESSION['FM_user']}' LIMIT 1"); $user = mysql_fetch_assoc($user); } include('includes/header.php'); ?> <body> <div class="headerBar"> <? include('includes/navigation.php');?> </div> <div class="headerSpace"></div> <div class="content"> <div class="widthLimiter contentStyle"> <? if(isSet($thisError)) { echo '<div class="errorDiv">',$thisError,'</div>'; } ?> <? if(isSet($thisSuccess)) { echo '<div class="successDiv">',$thisSuccess,'</div>'; } ?> <div class="formWrapper" style="width: 940px;"> <? if ($_POST['PRINTSELECTED'] == "PRINT SELECTED") { // PRINT SELECTED ?>PRINT SELECTED<br> <ul> <li class="field" style="font-size:10px;"> <div class="customerRequestIDSpacer"><strong>Request ID:</strong></div><div class="customerListSpacer"> </div> <div class="customerNameSpacer"><strong>Customer:</strong></div><div class="customerListSpacer"> </div> <div class="customerMobileSpacer"><strong>Mobile No:</strong></div><div class="customerListSpacer"> </div> <div class="customerPaymentMethodSpacer"><strong>Payment Method:</strong></div> <br style="clear:both"> </li> <? $requestIDs = implode(',', array_map('intval', $_POST['request'])); echo("<br><br>".$requestIDs."<br><br>"); $getRequests = mysql_query("SELECT * FROM requests WHERE `customer_email`='{$_SESSION['FM_user']}' AND `request_id` IN ({$requestIDs})"); echo("SELECT * FROM requests WHERE `customer_email`='{$_SESSION['FM_user']}' AND `request_id` IN ({$requestIDs})"); while($request = mysql_fetch_assoc($getRequests)) { echo '<li class="field" style="font-size:10px; border-bottom: 0.1em solid #D0D0D0"> <div class="customerRequestIDSpacer"> <em>',$request['request_id'],'</em> </div><div class="customerListSpacer"> | </div> <div class="customerNameSpacer"> <em>',$request['customer_name'],'</em> </div><div class="customerListSpacer"> | </div> <div class="customerMobileSpacer"> <em>',$request['customer_mobile'],'</em> </div><div class="customerListSpacer"> | </div>'; if ($request['paymentMethod'] != "Other") { $howTheyWantToPay = $request['paymentMethod']; } else { $howTheyWantToPay = $request['altpaymentMethod']; } echo '<div class="customerPaymentMethodSpacer"> <em>',$howTheyWantToPay,'</em> </div> <br style="clear:both"></li>'; } ?> </ul> <? } else { ?> <form method="post" action=""> <ul> <li class="field" style="font-size:10px;"> <div class="customerViewEditLink"></div><div class="customerListSpacer"> </div> <div class="customerRequestIDSpacer"><strong>Request ID:</strong></div><div class="customerListSpacer"> </div> <div class="customerNameSpacer"><strong>Customer:</strong></div><div class="customerListSpacer"> </div> <div class="customerMobileSpacer"><strong>Mobile No:</strong></div><div class="customerListSpacer"> </div> <div class="customerPaymentMethodSpacer"><strong>Payment Method:</strong></div> <br style="clear:both"> </li> <? $getRequests = mysql_query("SELECT * FROM requests WHERE `customer_email`='{$_SESSION['FM_user']}'"); while($request = mysql_fetch_assoc($getRequests)) { echo '<li class="field" style="font-size:10px; border-bottom: 0.1em solid #D0D0D0"> <div class="customerViewEditLink"> <input type="checkbox" name="request[]" value="',$request['request_id'],'"> </div> <div class="customerRequestIDSpacer"> <em>',$request['request_id'],'</em> </div><div class="customerListSpacer"> | </div> <div class="customerNameSpacer"> <em>',$request['customer_name'],'</em> </div><div class="customerListSpacer"> | </div> <div class="customerMobileSpacer"> <em>',$request['customer_mobile'],'</em> </div><div class="customerListSpacer"> | </div>'; if ($request['paymentMethod'] != "Other") { $howTheyWantToPay = $request['paymentMethod']; } else { $howTheyWantToPay = $request['altpaymentMethod']; } echo '<div class="customerPaymentMethodSpacer"> <em>',$howTheyWantToPay,'</em> </div> <br style="clear:both"></li>'; } ?> </ul> <input type="submit" name="PRINTSELECTED" value="PRINT SELECTED"> </form> <? } ?> </div> </div> </div> <? include('includes/footer.php');?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/232778-how-to-cross-reference-_post-array-checkboxes-with-mysql-field/#findComment-1197583 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.