Jump to content

how to cross reference $_POST array 'checkboxes[]' with mysql field


jason310771

Recommended Posts

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>';
	}
}

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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`='me@email.com' 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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.