Jump to content

Passing checkbox values to next page then query database for matches?


merylvingien

Recommended Posts

Hi guys, been floating about here for the last few days reading and learning, but i could really do with some help here.

 

I have a page with a list of results from my database, each result has a checkbox next to it with a value.

 

When a user selects a checkbox or several checkboxes i need to then pass those values to the next page and check the database for matches against those values.

 

Lets say for arguments sake that the values are 1, 5, 10 , 35

They could be ID's.

I need to check if 1, 5, 10, 35 exist in the databse and if they do i need to return the rows of data on the page.

For example: you have selected id 1, bla bla bla

                                          id 5, bla bla bla

                                          id 10, bla bla bla

                                          id 35, bla bla bla and so on.

 

I have a little more to ask after i get this sorted, but not too much  ;)

 

Any help here would be great.

 

P.s this forum is great, so many people willing to help out, much better than the last i visited  :D

Sounds like your looking for something like this...

 

<input type="checkbox" name="id[]" value="5" />
<input type="checkbox" name="id[]" value="10" />
<input type="checkbox" name="id[]" value="15" />

<?php
if(isset($_POST['id'])) {
   foreach($_POST['id'] as $item) {
      $sql = "SELECT * FROM table WHERE id=$item LIMIT 1";
      mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
      // do something with item
   }
}
?>

WOW thanks for the fast reply, i am getting a bit of an error with that code.

 

Fatal error: SQL: SELECT * FROM table WHERE id=x LIMIT 1, ERROR: Unknown column 'x' in 'where clause' in C:\Program Files\EasyPHP5.3.0\www\file\code7.php on line 94

 

Cant understand why its saying that, cause its just pulled column x from the database LOL

 

Whats the limit1 about?

LIMIT 1 means only fetch 1 row from the database, as you are searching by unique id you should only ever match one variable it's just a way of potentially speeding up the. I'm not certin it's required when searching a Unique field I just thought I'd add it in there. Your getting the error because you passed x. My code assumed the id is an integer. Strings passed to MySQL should be enclosed in single quotes.

Ok next question, will be the last for today i promise  ;D

 

Ive got my results showing up now as expected, But i have a price for each one.

 

Now to the realy dim part of the question, how do i add these prices up and have a total at the bottom?

 

Can anyone help me out with this code?

Its only displaying the last price total when it should be adding all the prices up to give a final total, ive tried allsorts to get it to work but i cant figure it out.

  <?php
if(isset($_POST['selected'])) {
   foreach($_POST['selected'] as $item) {
      $sql = "SELECT * FROM Postcode WHERE postcodeID=$item";
      mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
      $result = mysql_query($sql);
  $total = 0;
  while ($db_field = mysql_fetch_assoc($result)) {

print "<div class='redisplay'>" . $db_field['postcodename']. ", ". $db_field['town']. " <a href='#'>Towns Covered<span> {$db_field['stowns']}</span></a> ". $db_field['county']. ", ". $db_field['country']. " £". $db_field['priceb']. "</div> <br />";
$total += $db_field['priceb'];
}
	  
   }
  
}

print " Total = £$total <br />";

mysql_close($con);
?>

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.