1

I have now :

$inhoud = $_POST['inhoud'];
if($inhoud == "") {
    echo $inhoud;
    $inhoud = NULL;
    echo $inhoud;
}

$sql = 'SELECT DISTINCT bestanden.id AS id,
               bestanden.uploader AS uploader,
               bestanden.name AS name,
               bestanden.mime AS mime,
               bestanden.size AS size,
               bestanden.created AS created,
               zoeken.open_id AS open_id,
               zoeken.woord AS woord
        FROM bestanden
        LEFT JOIN zoeken ON bestanden.id = zoeken.open_id 
        WHERE zoeken.woord = "' . $inhoud . '" AND
              bestanden.name LIKE "%' . $_POST['naam'] . '%" AND
              bestanden.mime LIKE "%' . $_POST['formaat'] . '%"
              AND bestanden.created LIKE "%' . $_POST['datum'] . '%";';
$result = mysql_query($sql);

But when I search and $inhoud is empty, I got no results because they dont have an empty( " " ) (inhoud)space there. Is it possible to use an if or something in my mysql_query?

Adinia
  • 3,722
  • 5
  • 40
  • 58
Stefan
  • 19
  • 6

5 Answers5

0
    $sql = 'SELECT DISTINCT bestanden.id AS id, bestanden.uploader AS uploader, bestanden.name AS name, bestanden.mime AS mime, bestanden.size AS size, bestanden.created AS created, zoeken.open_id AS open_id, zoeken.woord AS woord FROM bestanden LEFT JOIN zoeken ON bestanden.id = zoeken.open_id 
    WHERE 1=1';
if(!empty($inhoud))
    $sql.=' AND zoeken.woord="' . $inhoud . '"';
$sql.=' AND bestanden.name LIKE "%' . $_POST['naam'] . '%" AND bestanden.mime LIKE "%' . $_POST['formaat'] . '%" AND bestanden.created LIKE "%' . $_POST['datum'] . '%"';
$result = mysql_query($sql);
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
0

Use empty function

empty($inhound);

Look here: http://php.net/manual/en/function.empty.php

Peter Adrian
  • 279
  • 1
  • 5
0

Maybe strlen() ?

if(strlen($inhoud)>0){
    $sql.=' AND zoeken.woord="' . $inhoud . '"';
    $sql.=' AND bestanden.name LIKE "%' . $_POST['naam'] . '%" AND
    bestanden.mime LIKE "%' .         $_POST['formaat'] . '%" AND 
    bestanden.created LIKE "%' . $_POST['datum'] . '%"';
    $result = mysql_query($sql);
 }else{
    //error fill in search term
 }
v0d1ch
  • 2,738
  • 1
  • 22
  • 27
0

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

You can try with:

$inhoud = $_POST['inhoud'];
if ($inhoud == "") {
    echo $inhoud;
    $inhoud = NULL;
    echo $inhoud;
}

$sql = 'SELECT DISTINCT bestanden.id AS id,
               bestanden.uploader AS uploader,
               bestanden.name AS name,
               bestanden.mime AS mime,
               bestanden.size AS size,
               bestanden.created AS created,
               zoeken.open_id AS open_id,
               zoeken.woord AS woord
        FROM bestanden
        LEFT JOIN zoeken ON bestanden.id = zoeken.open_id 
        WHERE ';
if(!empty($inhoud)) {
    $sql .= 'zoeken.woord = "' . $inhoud . '" AND';
}
$sql .= 'bestanden.name LIKE "%' . $_POST['naam'] . '%" AND
         bestanden.mime LIKE "%' . $_POST['formaat'] . '%"
         AND bestanden.created LIKE "%' . $_POST['datum'] . '%";';

$result = mysql_query($sql);
Zoe
  • 27,060
  • 21
  • 118
  • 148
jacoz
  • 3,508
  • 5
  • 26
  • 42
0

First of all, you shouldn't assume that $_POST[...] is set under any circumstances (unless you've already checked that it is set). Correct usage would be:

$inhoud = null:
if( isset( $_POST['inhound'])){
    $inhound = trim( $_POST['inhound']); // Trim to prevent strings like "    "
    if( strlen( $inhound) < 1){
        $inhound = null;
    }
}

Also note that mysql_* family is deprecated so rather use mysqli_* or PDO, having PDO example (using PDO::Prepare(), PDOStatement::bindParam() and PDOStatement::Execute()):

$sql = 'SELECT DISTINCT bestanden.id AS id, bestanden.uploader AS uploader,
            bestanden.name AS name, bestanden.mime AS mime,
            bestanden.size AS size, bestanden.created AS created,
            zoeken.open_id AS open_id, zoeken.woord AS woord
       FROM bestanden
       LEFT JOIN zoeken ON bestanden.id = zoeken.open_id 
       WHERE zoeken.woord=:inhound
           AND bestanden.name LIKE :naam
           AND bestanden.mime LIKE :formaat
           AND bestanden.created LIKE :date';

$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));

$sth->bindParam(':naam', isset( $_POST['naam']) ? '%' . $_POST['naam'] . '%', '%');
$sth->bindParam(':formaat', isset( $_POST['formaat']) ? '%'.$_POST['formaat'].'%', '%');
$sth->bindParam(':naam', isset( $_POST['date']) ? '%' . $_POST['date'] . '%', '%');
$sth->bindParam(':inhound', is_null( $inhound) ? '%' . $inhound . '%', '%');

$sth->execute();
$sth->fetchAll();
Vyktor
  • 20,559
  • 6
  • 64
  • 96