MySQL Forums
Forum List  »  PHP

MySQL Partial match query using PHP
Posted by: Jay Dayes
Date: February 08, 2006 09:53AM

Hi I have set up a query and results page in dreamweaver using PHP and MySQL database.
the is field in the table called subject. This is just a varchar field and stores different subject names seperated by commas. The data in this field is inserted from the values of checkboxes in a form.
e.g. one field would containt: subject1,subject2,subject3 while another may only containt subject2,subject3

on my query page. I have a list of the checkboxes each with the different subject values. When this query is submitted. the reults page should return all the relevant matches.

The issue is, that if the user clicks the following checkboxes:
subject1
subject3

I want all the rows to be returned that include either of these fields. But at the moment only rows with both subject 1 and subject 3 are returned.

I cant stored the different subject types in different fields because the list of subject types is dynamically updated from another table and so addition subject types could be inserted at a later date. The checkboxes are dynamically generated from this extra table.

Can anyone helpe me, I really cant find a solution on this!
Code for query.php and results.php is below
Kind Regards

Jay

Attach Code

********results.php************
<?php
$colname_Recordset1 = "-1";
if (isset($_POST['subjectname'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $_POST['subjectname'] : addslashes($_POST['subjectname']);
}
mysql_select_db($database_rcmconnect, $rcmconnect);
$query_Recordset1 = sprintf("SELECT * FROM subject_insert WHERE subjectname LIKE '%%%s%%' ORDER BY subid ASC", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $rcmconnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


?>

<table width="80%" border="0" cellspacing="0" cellpadding="0">
<?php do { ?>
<tr>
<td>
<?php echo $row_Recordset1['subjectname']; ?>
</td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<?php
mysql_free_result($Recordset1);
?>
********************************************
****************query.php*****************
<?php
mysql_select_db($database_rcmconnect, $rcmconnect);
if(is_array($_POST['subjectname']))
$_POST['subjectname']=implode(",",$_POST['subjectname']);
$query_Recordset1 = "SELECT subjectname FROM subjects ORDER BY subid ASC";
$Recordset1 = mysql_query($query_Recordset1, $rcmconnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><form action="results.php" method="post">

<table width="80%" border="0" cellspacing="0" cellpadding="0">
<?php do { ?>
<tr>
<td width="48%"><?php echo $row_Recordset1['subjectname']; ?></td>
<td width="52%"><label>
<input type="checkbox" name="subjectname" id="subjectname" value="<?php echo $row_Recordset1['subjectname']; ?>">
</label></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<tr><td><label>
<input type="submit" name="Submit" value="Submit">
</label></td>
</tr>
</table>
</form>
<?php
mysql_free_result($Recordset1);
?>
********************************************

Options: ReplyQuote


Subject
Written By
Posted
MySQL Partial match query using PHP
February 08, 2006 09:53AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.