Quantcast
Channel: Serif Software Forums (Closed) - WebPlus
Viewing all articles
Browse latest Browse all 4745

WebPlus X6: PHP if statement for SQL query

$
0
0
Hi,
I am trying to do a SQL search based on a form submit. I have got the basics (you can search by filling in one field at a time), but what if the user fills in multiple form fields (say First name and Last name)? Well, I have written a very long php if statement to try and solve the problem, but it doesn't work.

Can someone who knows PHP take a look and tell me a) if there is a shorter/easier way to do this search depending on which fields are filled, and b) how to fix the problem (I get a compile error on line 8 (well, 14 in the script but I cut out the first 6 lines).

PHP Code:

<?php
if (!isset ($_POST['dosearch']) ) {
    
$dosearch 0;
    }
    else {
    
$dosearch $_POST['dosearch'];
    }
if (
$dosearch == 1) { 
    if (!empty (
$_POST['firstname'])) && (!empty ($_POST['lastname'])) && (!empty ($_POST['username'])) && (!empty ($_POST['company']))
    {
    
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]' AND surname='$_POST[lastname]' AND username='$_POST[username]' AND company='$_POST[company]'");
    }
    else {
        if (empty (
$_POST['firstname'])) && (!empty ($_POST['lastname'])) && (!empty ($_POST['username']) && (!empty ($_POST['company'])
        {
        
$result mysqli_query($con,"SELECT * FROM clients WHERE surname='$_POST[lastname]' AND username='$_POST[username]' AND company='$_POST[company]'");
        }
        if (!empty (
$_POST['firstname'])) && (empty ($_POST['lastname'])) && (!empty ($_POST['username']) && (!empty ($_POST['company'])
        {
        
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]' AND username='$_POST[username]' AND company='$_POST[company]'");
        }
        if (!empty (
$_POST['firstname'])) && (!empty ($_POST['lastname'])) && (empty ($_POST['username']) && (!empty ($_POST['company'])
        {
        
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]' AND surname='$_POST[lastname]' AND company='$_POST[company]'");
        }
        if (!empty (
$_POST['firstname'])) && (!empty ($_POST['lastname'])) && (!empty ($_POST['username']) && (empty ($_POST['company'])
        {
        
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]' AND surname='$_POST[lastname]' AND username='$_POST[username]'");
        }
        else {
            if (!empty (
$_POST['username']) && (!empty ($_POST['company'])
            {
            
$result mysqli_query($con,"SELECT * FROM clients WHERE username='$_POST[username]' AND company='$_POST[company]'");
            }
            if (!empty (
$_POST['lastname'])) && (!empty ($_POST['company'])
            {
            
$result mysqli_query($con,"SELECT * FROM clients WHERE surname='$_POST[lastname]' AND company='$_POST[company]'");
            }
            if (!empty (
$_POST['lastname'])) && (!empty ($_POST['username'])
            {
            
$result mysqli_query($con,"SELECT * FROM clients WHERE surname='$_POST[lastname]' AND username='$_POST[username]'");
            }
            if (!empty (
$_POST['firstname'])) && (!empty ($_POST['company'])
            {
            
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]' AND company='$_POST[company]'");
            }
            if (!empty (
$_POST['firstname'])) && (!empty ($_POST['username'])
            {
            
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]' AND username='$_POST[username]'");
            }
            if (!empty (
$_POST['firstname'])) && (!empty ($_POST['lastname']))
            {
            
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]' AND surname='$_POST[lastname]'");
            }
            else {
                if (!empty (
$_POST['firstname']))
                
$result mysqli_query($con,"SELECT * FROM clients WHERE firstName='$_POST[firstname]'");
                if (!empty (
$_POST['lastname']))
                {
                
$result mysqli_query($con,"SELECT * FROM clients WHERE surname='$_POST[lastname]'");
                }
                if (!empty (
$_POST['username'])
                {
                
$result mysqli_query($con,"SELECT * FROM clients WHERE username='$_POST[username]'");
                }
                if (!empty (
$_POST['company'])
                {
                
$result mysqli_query($con,"SELECT * FROM clients WHERE company='$_POST[company]'");
                }
                else {
                    echo 
"The query returned no results.  Please try again.";
                    }
while(
$row mysqli_fetch_array($result))
  {
  echo 
$row['firstName'] . " " $row['surname'];
  echo 
"<br>";
  }

}

?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Firstname: <input type="text" name="firstname"><br />
Lastname: <input type="text" name="lastname"><br />
Username: <input type="text" name="username"><br />
Company: <input type="text" name="company"><br />
<input type="hidden" name="dosearch" value="1">
<input type="submit"><input type="reset">
</form>

I am only writing the "back-end" code before I copy it into WebPlus to "pretty it up", as I don't think there's much point in making it look nice if it doesn't work!


I hope that someone can help me with this,

Regards,
TAE

Viewing all articles
Browse latest Browse all 4745

Trending Articles