Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have problem with bindings, I want to insert name or surname in label Pisatelj as keyword to search for this author, I want also do the same way if I insert keyword for Naslov(=Title of book) please correct my code I have this code in php with postgres:

function get_knjige_sql ( )
{
    global $CRUD;
    $dbh = $CRUD['dbh'];
        $str_query = '';
        if(isset($_POST['Knaslov'])){
                $str_query = addslashes($_POST['Knaslov']);
        }
        if(isset($_POST['Ppriimek'])){
                $str_query = addslashes($_POST['Ppriimek']);
        } 

    $query = " SELECT *  FROM knjiga, pisatelj, zaloga WHERE (knjiga.naslov ILIKE '?' OR CONCAT(pisatelj.ime, ' ', pisatelj.priimek) ILIKE '?' ) AND knjiga.p_id = pisatelj.p_id AND knjiga.k_id = zaloga.k_id AND zaloga.prodana = false ";
    if($sth)
    $sth->bindValue(':Knaslov', $Knaslov, PDO::PARAM_STR);
    $sth->bindValue(':Ppriimek', $Ppriimek, PDO::PARAM_STR);
    if($sth)
    $sth->execute();

    else error('get_knjige_sql: select prepare returned no statement handle');

    $err = $sth->errorInfo();
    if($err[0] != 0) error( $err[2] );

    return($sth);
}

main.php:

<!-- main html file for CRUD (php version) -->
<?php echo $CRUD["MESSAGES"] ?><?php echo $CRUD["ERRORS"] ?>
<div class="form">
<form action="<?php echo $CRUD["SELF"] ?>" method="post" name="knjiga">
    <p class="subheading"><?php echo $CRUD["FORM_HEAD"] ?></p>

    <table class="form">
    <tr>
        <td><p class="Afield"> Naslov:</p></td>
        <td><input class="Afield" type="text" name="Knaslov" value="<?php echo $CRUD["Knaslov"] ?>"> </td>
    </tr>
    <tr>
        <td><p class="Afield"> Isbn:</p></td>
        <td><input class="Afield" type="text" name="kisbn" value="<?php echo $CRUD["Kisbn"] ?>"> </td>
    </tr>
    <tr>
        <td><p class="Afield"> Cena:</p></td>
        <td><input class="Afield" type="text" name="Kcena" value="<?php echo $CRUD["Kcena"] ?>"> </td>
    </tr>
    <tr>
        <td><p class="Afield"> Pisatelj:</p></td>
        <td><input class="Afield" type="text" name="Ppriimek" value="<?php echo $CRUD["Pime"] ?><?php echo $CRUD["Ppriimek"] ?>"> </td>

    </tr>

    <tr class="buttons"><td colspan="2">
<p class="buttons">
<?php echo $CRUD["BUTTONS"] ?><?php echo $CRUD["HIDDENS"] ?>
</p>
    </td></tr>
    </table>


</form>
</div>
<?php echo $CRUD["PRECONTENT"] ?><?php echo $CRUD["CONTENT"] ?><?php echo $CRUD["POSTCONTENT"] ?>
share|improve this question
    
I want to post code here, but I cannot, I tried with ctrl+k and insert code in the midle of code here but it doesnt work, so I post to pastebin.com –  user564456 Dec 14 '13 at 19:27
    
Don't paste it into "code here", just paste in the code, highlight it, then ctl-k –  Michael Berkowski Dec 14 '13 at 19:29
    
Your query itself is not using the placeholders that you are using in the bindValue() –  user602525 Dec 14 '13 at 19:32
    
Lots is confusing here. What is $sth? That isn't in scope in this function. Did you mean $dbh? Looks like you are trying to use PDO. You never called $dbh->prepare() before binding values and executing. Finally, you should not quote the ? placeholders as '?' in the prepared statement. Since you bound named parameters, those should appear in your prepared statement instead of ? as in knjiga.naslov ILIKE :knaslov –  Michael Berkowski Dec 14 '13 at 19:32
1  
Finally, remove both addslashes(). That is not needed when binding parameters (and is the wrong way to escape, even when not using prepared statements) –  Michael Berkowski Dec 14 '13 at 19:38
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.