简体   繁体   中英

Simple mysql_query with WHERE clause failing

Here's my code:

<p>Select application status to view.</p>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <select name="type" style="width:100px;">
        <option name="New">New</option>
        <option name="Approved">Approved</option>
        <option name="Denied">Denied</option>
        <option name="In Training">In Training</option>
        <option name="Passed">Passed</option>
        <option name="Retrained">Retrained</option>
        <option name="Failed">Failed</option>
        <option name="Placed">Placed</option>
    </select>
    <input type="submit" value="Go" />
</form>

<?php if (!isset($_POST['type'])): 
    $newapps = mysql_query('SELECT aid, status, Day, Dte, Month, Year, email, Character_Name FROM applications WHERE status LIKE "New"'); ?>

    <table>
        <tr>
            <td><strong>ID</strong></td>
            <td><strong>Status</strong></td>
            <td><strong>Date Submitted</strong></td>
            <td><strong>E-mail</strong></td>
            <td><strong>Character Name</strong></td>
        </tr>
        <?php while ($newapp = mysql_fetch_array($newapps))
            {
            $aid = $newapp['aid'];
            $status = $newapp['status'];
            $day = $newapp['Day'];
            $date = $newapp['Dte'];
            $month = $newapp['Month'];
            $year = $newapp['Year'];
            $email = $newapp['email'];
            $name = $newapp['Character_Name'];
            echo "<tr><td>$aid</td><td>$status</td><td>$day, $month $date $year</td><td>$email</td><td>$name</td></tr>\n";
            }
        ?>
    </table>

<?php else: 
    $query1 = "SELECT aid, status, Day, Dte, Month, Year, email, Character_Name FROM applications WHERE status LIKE '{$_POST['type']}'";
    $result = mysql_query('$query1') or die(mysql_error()); 
?>

    <table>
        <tr>
            <td><strong>ID</strong></td>
            <td><strong>Status</strong></td>
            <td><strong>Date Submitted</strong></td>
            <td><strong>E-mail</strong></td>
            <td><strong>Character Name</strong></td>
        </tr>
        <?php while ($applist = mysql_fetch_array($result))
            {
            $aid = $applist['aid'];
            $status = $applist['status'];
            $day = $applist['Day'];
            $date = $applist['Dte'];
            $month = $applist['Month'];
            $year = $applist['Year'];
            $email = $applist['email'];
            $name = $applist['Character_Name'];
            echo "<tr><td>$aid</td><td>$status</td><td>$day, $month $date $year</td><td>$email</td><td>$name</td></tr>\n";
            }
        ?>
    </table>    

<?php endif; ?>

When I load the page, it DOES pull the "New" applications properly. But when I run a query to pull up, for example, Approved apps, I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query1' at line 1

Any ideas what I'm missing?

You are wrapping $query in single quotes, causing it to be taken literally.

Use double quotes, or none at all:

mysql_query($query1)

Try mysql_query("{$query1}").

Use double quotes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM