简体   繁体   中英

PHP/SQL - Error SQL syntax

When run the code:

 $result = select_query("tblinvoices", "", "id IN 
   (SELECT invoiceid FROM tblinvoiceitems 
    WHERE description LIKE '%Project #" . $projectid . "%' 
    OR " . $ticketinvoicesquery . " (type='Project'
    AND relid='" . $projectid . "'))
    OR id IN (" . db_build_in_array(db_escape_numarray($invoiceids)) . ")",
       "id", "ASC");

The error appears in the log:

 SQL 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 ') ORDER BY `id` ASC' at line 1 
 - Full Query: SELECT * FROM tblinvoices WHERE id IN (SELECT invoiceid 
 FROM tblinvoiceitems WHERE description
 LIKE '%Project #4%' OR (type='Project' AND relid='4'))
 OR id IN () ORDER BY `id` ASC

How to solve this error?

UPDATE:

When I have no invoice is this error in the log. When I have an invoice the code works perfectly.

     {include file="$template/subheader.tpl" title=$_lang.associatedinvoices}
{if $invoices}
<table class="table table-striped table-framed">
    <thead>
        <tr>
            <th style="width:10%" class="textcenter">N°</th>
            <th style="width:15%" class="textcenter">Date</th>
            <th style="width:15%" class="textcenter">Due date</th>
            <th style="width:20%" class="textcenter">Price</th>
            <th style="width:20%" class="textcenter">Status</th>
            <th style="width:20%" class="textcenter">View</th>
        </tr>
    </thead>

    <tbody>
    {foreach from=$invoices item=invoice}
        <tr>
            <td class="textcenter">#{$invoice.id}</td>
            <td class="textcenter">{$invoice.date}</td>
            <td class="textcenter">{$invoice.duedate}</td>
            <td class="textcenter">{$invoice.total}</td>
            <td class="textcenter"><span class="label {$invoice.rawstatus}">{$invoice.status}</span></td>
            <td class="textcenter"><a href="viewinvoice.php?id={$invoice.id}"><i class="fa fa-eye icon_view" title="View"></i></a></td>
        </tr>
    {/foreach}
    </tbody>
</table>
<br />
{else}
    <p>{$_lang.none}</p>
{/if}

Thanks!

I can't say for certain regarding mysql but in postgresql the error is caused by this part:

id IN ()

IN doesn't appear to work with empty lists, at least in PostgreSQL.

ERROR:  syntax error at or near ")"
LINE 1: SELECT 1 from orders WHERE id IN ();
                                          ^
********** Error **********

ERROR: syntax error at or near ")"
SQL state: 42601
Character: 35

To make your code work you'll need to avoid calling your SQL statement in situations where the variable $invoiceids is an empty array.

Pseudocode:

if IsEmptyArray(invoiceids) {
  result = empty_query()
} else {
  result = select_query(...)
}

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