0

G' day!i have my code here in searching a record..it runs very well..but i want to add an options..i want to add like this:

<select>
<option name="course">course</option>
<option name="surname">surname</option>
<option name="department">department</option>
<option name="email address">email address</option>
<option name="student no">student no</option>
</select>

i want to add that code in my program..and i know there's a revision in my code..here's my static html code:

index.php

<div><form method="post" id="newsletterform" action="search.php">
<input type="text" name="term" id="s" style="width: 95%;" />
<button class="Button" type="submit" name="search">

search.php

<?php
   
      mysql_connect ("localhost", "root") or die (mysql_error());
   
      mysql_select_db ("records");
   

   
      $term = $_POST['term'];

       

      $sql = mysql_query("select * from students where name like '%$term%' OR studno like '$term%'");

        echo "<table width='600' cellspacing='0' cellpadding='0' border='0' align='center'>";
         echo "<tr><th bgcolor='#99FFCC'>Student No:</th><th bgcolor='#99FFCC'>Name</th> <th bgcolor='#99FFCC'>Course</th> <th bgcolor='#99FFCC'>Year</th><th bgcolor='#99FFCC'>Department</th></tr>";

      while ($row = mysql_fetch_assoc($sql)){

       echo '<td bgcolor="ffffcc"><center>' . $row['studno'] . '</td>';
       echo '<td bgcolor="ffffcc"><center>' . $row['name'] . '</td>';
	echo '<td bgcolor="ffffcc"><center>' . $row['course'] . '</td>';
	echo '<td bgcolor="ffffcc"><center>' . $row['year'] . '</td>';
	echo '<td bgcolor="ffffcc"><center>' . $row['department'] . '</td>';
        echo "</tr>";

            }
        echo "</table>";


      ?>
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by takeshi
0

First of all your form...

<div>
<form method="post" id="newsletterform" action="search.php">
<p><input type="text" name="term" id="s" style="width: 95%;" /></p>
<p><select name="criteria">
<option value="1">course</option>
<option  value="2">surname</option>
<option  value="3">department</option>
<option  value="4">email address</option>
<option  value="5">student no</option>
</select></p>
<p><button class="Button" type="submit" name="search"></p>
</form>
</div>

Now in the php script you just parse the value of "criteria" and generate the correct mysql query.

// your code here ...
$criteria = (int)$_POST['criteria'];
$sql = "SELECT * FROM students WHERE ";
switch($criteria)
{
case 1: $sql .= "course"; break
case 2: $sql .= "surname"; break;
case 3: $sql .= "department"; break;
case 4: $sql .= "email"; break;
case 5: $sql .= "studno"; break;
}

$sql .= " LIKE '%term%'";
// your code here ...

Edited by amd_k8: n/a

0

First of all your form...

<div>
<form method="post" id="newsletterform" action="search.php">
<p><input type="text" name="term" id="s" style="width: 95%;" /></p>
<p><select name="criteria">
<option value="1">course</option>
<option  value="2">surname</option>
<option  value="3">department</option>
<option  value="4">email address</option>
<option  value="5">student no</option>
</select></p>
<p><button class="Button" type="submit" name="search"></p>
</form>
</div>

Now in the php script you just parse the value of "criteria" and generate the correct mysql query.

// your code here ...
$criteria = (int)$_POST['criteria'];
$sql = "SELECT * FROM students WHERE ";
switch($criteria)
{
case 1: $sql .= "course"; break
case 2: $sql .= "surname"; break;
case 3: $sql .= "department"; break;
case 4: $sql .= "email"; break;
case 5: $sql .= "studno"; break;
}

$sql .= " LIKE '%term%'";
// your code here ...

ok illt try this..tnx...:)

0

ok illt try this..tnx...:)

This code are not correct.. What's the problem lies about?


search.php

<?php
   
      mysql_connect ("localhost", "root") or die (mysql_error());
   
      mysql_select_db ("records");
   $criteria = (int)$_POST['criteria'];
$sql = "SELECT * FROM students WHERE ";
switch($criteria)
case 1: $sql .= "course"; break
case 2: $sql .= "surname"; break;
case 3: $sql .= "department"; break;
case 4: $sql .= "email"; break;
case 5: $sql .= "studno"; break;
}
$sql .= " LIKE '%term%'";

while ($row = mysql_fetch_array($sql)){
      
    echo '<br/> Surname: '.$row['lname'];
echo '<br/> Course: '.$row['course'];
echo '<br/> Student No: '.$row['studno'];
echo '<br/> Department: '.$row['department'];
echo '<br/> Email Address: '.$row['email'];
  }
     ?>

My html code.

form.php

<div>
   
      <form method="post" id="newsletterform" action="search.php">
   
      <p><input type="text" name="term" id="s"  /></p>
   
      <p><select name="criteria">
   
      <option value="1">course</option>
   
      <option value="2">surname</option>
   
      <option value="3">department</option>
   
      <option value="4">email address</option>
   
      <option value="5">student no</option>
  
      </select></p>
  
      <p><button class="Button" type="submit" name="search"></p>
  
      </form>
  
      </div>

Edited by takeshi: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.