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.

If I tried this`

function get_books_sql ( )
{
    global $CRUD;
    $dbh = $CRUD['dbh'];


                $title = '';
                $fullName = '';
                $name = '';
                $Ktitle = '';
                $Psurname = '';

                if(isset($_POST['Ktitle'])){
        $title = $_POST['Ktitle'];
        }

                if(isset($_POST['Surname'])){
                $fullName = $_POST['Psurname'];
                $name = explode(" ", $fullName);
}      

$sth = $dbh->prepare(" SELECT *  FROM book JOIN stock ON stock.k_id = book.k_id WHERE (book.title LIKE :Ktitle ) AND stock.sold = FALSE ");
                $sth->bindValue(':Ktitle', $title, PDO::PARAM_STR);

if($sth) $sth->execute(array('%'.$title.'%'));

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

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

    return($sth);
}

` it works when I insert keyword for Book title and if I want to do both for book title keyword andfor author name and surname I want to filtered with keyword title and keywords of name and surname of author, both queries alone work but I have problem with Author name and surname and I dont know how can I bind them and execute

There is main.php file:

    <!-- 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="book">
    <p class="subheading"><?php echo $CRUD["FORM_HEAD"] ?></p>

    <table class="form">
    <tr>
        <td><p class="Afield"> Title:</p></td>
        <td><input class="Afield" type="text" name="Ktitle" value="<?php echo $CRUD["Ktitle"] ?>"> </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="Kprice" value="<?php echo $CRUD["Kprice"] ?>"> </td>
    </tr>
        <tr>
        <td><p class="Afield"> Author:</p></td>
        <td><input class="Afield" type="text" name="Psurname" value="<?php echo $CRUD["Pname"] ?><?php echo $CRUD["Psurname"] ?>"> </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"] ?>

This I did for onyl Author: `

function get_books_sql ( )
{
    global $CRUD;
    $dbh = $CRUD['dbh'];


                $title = '';
                $fullName = '';
                $name = '';
                $Ktitle = '';
                $Psurname = '';

                if(isset($_POST['Ktitle'])){
        $title = $_POST['Ktitle'];
        }

                if(isset($_POST['Surname'])){
                $fullName = $_POST['Psurname'];
                $name = explode(" ", $fullName);
}      

        $sth = $dbh->prepare("SELECT *  FROM book, author, stock WHERE (CONCAT( author.name , ' ', author.surname ) ILIKE :Psurname ) AND book.p_id = author.p_id AND book.k_id = stock.k_id AND stock.sold = false ");

                $sth->bindValue(':Psurname', $fullName, 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);
}`

This is my whole query which works with tested datas: SELECT * FROM book, author, stock WHERE (book.title ILIKE '%a%' AND CONCAT ( author.name, ' ', author.surname ) ILIKE '%pavcek%' ) AND book.p_id = author.p_id AND book.k_id = stock.k_id AND stock.sold = false;

but I have to do so I mean: SELECT * FROM book, author, stock WHERE (book.title ILIKE :Ktitle AND CONCAT ( author.name, ' ', author.surname ) ILIKE :Psurname ) AND book.p_id = author.p_id AND book.k_id = stock.k_id AND stock.sold = false;

share|improve this question
    
It's not clear to me what your problem is. I can't see the relevance of the HTML either. The code is formatted very oddly too. Is your question something to do with adding the % wildcards for the ILIKE matching? –  Richard Huxton Dec 17 '13 at 13:22
    
I tried so $sth->bindValue(':Psurname', '%'.$fullName.'%', PDO::PARAM_STR); but I get no output of any autohrs ,this is my query:"SELECT * FROM book, author, stock WHERE (CONCAT( author.name , ' ', author.surname ) ILIKE :Psurname ) AND book.p_id = author.p_id AND book.k_id = stock.k_id AND stock.sold = false "); –  user564456 Dec 17 '13 at 14:35
    
This test query in coomand line works: SELECT * FROM book, author, stock WHERE ( CONCAT( author.name , ' ' , author.surname ) ILIKE '%pavcek%' ) AND book.p_id = author.p_id AND book.k_id = stock.k_id AND stock.sold = false ; If I tried :Psurname instead of '%pavcek%' it doesnt work(no output) :) –  user564456 Dec 17 '13 at 14:44
    
Seems unlikely. What you're mistaken about I can't tell, but it seems likely you are mistaken. Simplify the problem, just try matching a single table with the ILIKE and parameters. Then build up from there. –  Richard Huxton Dec 17 '13 at 23:10

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.