Why does this php/ajax query fail?

Posted by Ashley Brown on Stack Overflow See other posts from Stack Overflow or by Ashley Brown
Published on 2014-08-20T16:18:18Z Indexed on 2014/08/20 16:20 UTC
Read the original article Hit count: 197

Filed under:
|
|
|

I'm ajaxing over to this php file.

$a = 'old';
$b = 'new';
if ($_POST['info-type'] == $a || $b)
{
    $info = $_POST['info-type'];
    $query = "SELECT * FROM `tld` WHERE type = '".$var."'";
} 

$query = "SELECT * FROM `tld` ";


$result = mysqli_query($link,$query);

while($row = mysqli_fetch_assoc($result))
    {
    echo '<div>'.$row['something'].'</div>';
    }

The data posted is either 'all' 'new' or 'old'.

If i send the data as either new or old, the script works and outputs as expected. If the posted data is neither new or old but all instead, it fails and don't show any errors or respond anything back. (I've monitored via dev tools aswell)

So, I tried this

if ($_POST['info-type'] == $a || $b)
    {
        $info = $_POST['info-type'];
        $var = "SELECT * FROM `tld` WHERE type = '".$var."'";
    } elseif ($_POST['info-type'] == 'all'){
       $query = "SELECT * FROM `tld` ";
    }

But the script still fails. If i fully remove the IF statements and use the query without the WHERE clause like it is after the elseif, it works?

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery