Jump to content

Recommended Posts

Maybe someone could answer this if you explained what you mean by "validate chase switching". I'd say "validate case switching", but that doesn't make sense either.

 

Is it possible to add some type of validation to the

code below? I know that I can validate the form entries,

but can there be some sort of validation added to the

case switching example below?

 

<?php
$searchType = $_POST['searchType'];
$searchTerm = $_POST['searchTerm'];

switch($searchType) {
    case radio1:
         $sql = some query...
         break;
    case radio2:
         $sql = some different query...
         break;
    case radio3:
         $sql = some other different query...
         break;
     default:
       $sql = not NEEDED, but nice to have a backup 
}

?>

1) Strings go in quotes. You're comparing against constants now.

2) What do you mean by validation? The following could already be considered "validation".

 

switch($searchType) {
    case 'sometype':
         $sql = some query...
    break;
    case 'someOtherType':
         $sql = some different query...
      break;
     default:
       trigger_error('Invalid type', E_USER_ERROR);
}

Ok, I am trying to validate the script below which

is inside a funtion.

 

I am trying to get it to return a value

<?php

#Checks that username is in database 
   $result = searchBy($variable)

   #Check error codes 
   if($result == 0){
      //No data 
   }   
?>

 

<?php
function searchBy(){

$searchType = $_POST['searchType'];
$searchTerm = $_POST['searchTerm'];

switch($searchType) {
    case radio1:
         $sql = "SELECT * FROM members_info WHERE user_name='$searchTerm'";
         break;
case radio2:
         $sql = "SELECT * FROM members_info WHERE email_address ='$searchTerm'";
         break;
case radio3:
         $sql = "SELECT * FROM members_address WHERE state ='$searchTerm'";
         break;
     default:
       echo "There is a problem in your script";
}


$res = mysql_query($sql);
$num_results = mysql_num_rows($res); 

for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($res);

  $user_name = $row["user_name"];

echo "
<div class=\"friendtable\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\">
<tr>	
  <td class=\"image\">
<img src=\" \">
  </td>
  <td class=\"info\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">

<tr>
   <td class=\"name\">Name:</td>
   <td class=\"name\">$user_name</td>
   <td class=\"actions\"><a style=\"text-decoration:none;\" href=\"http://\">View Profile</a></td>
</tr>	
  
<tr>
  <td class=\"label\">
    Network:
  </td>
  <td>
   <a href=\" \">Lander '09</a><br />
  </td>

  <td class=\"actions\">
   <a style=\"text-decoration:none;\" href=\"http://\">Send Message</a>
   <a style=\"text-decoration:none;\" href=\"\">Poke Dolores!</a>
   <a style=\"text-decoration:none;\" href=\"http://\">View Friends</a>
   <a style=\"text-decoration:none;\" href=\"http://\">Add to Friends</a>
  </td>
</tr>

</table>

</td>
</tr>
</table>
		  
</div>";

}


}

?>

If the searchTerm is not in the database

    return false

 

How would I check to see if searchTerm is in the

database by using the method below?

<?php
switch($searchType) {
    case radio1:
         $sql = "SELECT * FROM members_info WHERE user_name='$searchTerm'";
         break;
case radio2:
         $sql = "SELECT * FROM members_info WHERE email_address ='$searchTerm'";
         break;
case radio3:
         $sql = "SELECT * FROM members_address WHERE state ='$searchTerm'";
         break;
     default:
       echo "There is a problem in your script";
}

?>

<?php
switch($searchType) {
    case radio1:
         $sql = "SELECT * FROM members_info WHERE user_name='$searchTerm'";
         break;
case radio2:
         $sql = "SELECT * FROM members_info WHERE email_address ='$searchTerm'";
         break;
case radio3:
         $sql = "SELECT * FROM members_address WHERE state ='$searchTerm'";
         break;
     default:
       echo "There is a problem in your script";
}

$result = mysql_query($sql) OR DIE(mysql_error());
if (mysql_num_rows($result) < 1) {
    echo 'No results were found for ' . $searchTerm;
}
?>

 

Like that ???

<?php
switch($searchType) {
    case radio1:
         $sql = "SELECT * FROM members_info WHERE user_name='$searchTerm'";
         break;
case radio2:
         $sql = "SELECT * FROM members_info WHERE email_address ='$searchTerm'";
         break;
case radio3:
         $sql = "SELECT * FROM members_address WHERE state ='$searchTerm'";
         break;
     default:
       echo "There is a problem in your script";
}

$result = mysql_query($sql) OR DIE(mysql_error());
if (mysql_num_rows($result) < 1) {
    echo 'No results were found for ' . $searchTerm;
}
?>

 

Like that ???

 

Ok, how would I redirect the  echo 'No results were found for ' . $searchTerm;

string back to search.php page?

 

<?php
switch($searchType) {
    case radio1:
         $sql = "SELECT * FROM members_info WHERE user_name='$searchTerm'";
         break;
case radio2:
         $sql = "SELECT * FROM members_info WHERE email_address ='$searchTerm'";
         break;
case radio3:
         $sql = "SELECT * FROM members_address WHERE state ='$searchTerm'";
         break;
     default:
       echo "There is a problem in your script";
}

$result = mysql_query($sql) OR DIE(mysql_error());
if (mysql_num_rows($result) < 1) {
    header("Location: search.php");

   //I need to return this back to search.php
    echo 'No results were found for ' . $searchTerm; 
    exit;
}
?>

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.