Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[newbie] PHP searchform 1

Status
Not open for further replies.

joep

Programmer
Jun 4, 2000
15
NL
Hello,<br><br>I have a searchform where i want to be able to search for persons who are married and display the selected records in a table. That's going o.k.<br>But if i don't check the checkbox, there has to be a 'zero' value so that all the records in the database will be displayed. What am i doing wrong (or what am i missing) in the following code to produce such a form?<br>Thanks in advance,<br><br>Joep<br>------------------------------------------------<br>&lt;html&gt;<br>&lt;body&gt;<br>&lt;?<br>$db = mysql_connect(&quot;localhost&quot;, &quot;username&quot;, &quot;password&quot;);<br>mysql_select_db(&quot;database&quot;,$db);<br><br>if ($submit)<br>for($i=0;$i&lt;count($married);$i++)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;$sql = &quot;SELECT * FROM employees WHERE married = '$married'&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = mysql_query($sql);<br><br>echo &quot;&lt;table border=1&gt;\n&quot;;<br>echo<br>&quot;&lt;tr&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Adres&lt;/td&gt;&lt;td&gt;Children&lt;/td&gt;&lt;td&gt;Income&lt;/td&gt;&lt;/tr&gt;\n&quot;;<br>while ($myrow = mysql_fetch_array($result)) {<br>printf(&quot;&lt;tr&gt;&lt;td&gt;%s&lt;/td&gt;&lt;td&gt;%s&lt;/td&gt;&lt;td&gt;%s&lt;/td&gt;&lt;td&gt;%s&lt;/td&gt;&lt;/tr&gt;\n&quot;,<br>$myrow[Name], $myrow[Adres], $myrow[Children], $myrow[Income]);<br>}}<br><br>?&gt;<br><br>&lt;P&gt;<br>&lt;form method=&quot;post&quot; action=&quot;&lt;? echo $PHP_SELF?&gt;&quot;&gt;<br>Married? &lt;input type=checkbox name=&quot;married&quot; value=y&gt; Yes &lt;br&gt;<br>&lt;input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Search&quot;&gt;<br>&lt;/form&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br><br>
 
If the &quot;married&quot; checkbox is not ticked, then when the user submits the form <FONT FACE=monospace>$married</font> will not be set.&nbsp;&nbsp;You want to add a line something like the following before your &quot;if ($submit)&quot; line:<br><FONT FACE=monospace><br>if (! isset($married) ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;$sql = &quot;SELECT * FROM employees;&quot;;<br>} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;$sql = &quot;SELECT * FROM employees WHERE married = 'y';&quot;;<br>}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></font><br>You can then drop the later &quot;$sql = &quot; line and all should be OK.
 
If the &quot;married&quot; checkbox is not ticked, then when the user submits the form <FONT FACE=monospace>$married</font> will not be set.&nbsp;&nbsp;You want to add a line something like the following before your &quot;if ($submit)&quot; line:<br><FONT FACE=monospace><br>if (isset($married) ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;$sql = &quot;SELECT * FROM employees WHERE married = 'y';&quot;;<br>} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;$sql = &quot;SELECT * FROM employees;&quot;;<br>}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></font><br>You can then drop the later &quot;$sql = &quot; line and all should be OK.
 
Hi iam useing a varible number of checkboxes wich values are pulled out of a database. I then have to submit those values to an other table

here is the script fot getting the checkboxes
?php
$result = mysql_db_query (&quot;setf&quot;,&quot;select ond_id, titel from onderwerpen &quot;);
?>
Onderwerpen waar het mee te maken heeft:<br>
<?php
while ($myrow = mysql_fetch_array($result)){
?>
<INPUT name=&quot;ond_id&quot; type=&quot;checkbox&quot; value=&quot;<?php echo $myrow[&quot;ond_id&quot;] ?>&quot;> <?php echo $myrow[&quot;titel&quot;] ?>
<?php
}

$ond_id this is an array how do i take apart this array and insert the multible values into the database?

thanx in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top