Re: validation & inserts not working

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PJ,

On Tue, Mar 10, 2009 at 3:46 PM, PJ <af.gourmet@xxxxxxxxxxxx> wrote:

> <snip>
> $sql1 = "INSERT INTO book ( title, sub_title, descr,
>            comment, bk_cover, copyright, ISBN, language, sellers )
>         VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN',
>            '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN',
>            '$sellersIN')";
>    $result1 = mysql_query($sql1, $db);
>    $autoid = mysql_insert_id($result1);
>    }/* <--- IF THIS LIKE IS DELETED, THE PAGE DOES NOT DISPLAY
> So, if I select insert on the page, only the first query is executed
> since the rest is commented out. Thus, I get onle the book table
> inserted but not other tables, like author, book_author, or publishers,
> book_publisher or categories, book_categories...
> Is there something wrong with what follows immediately...
> like, do I have the brackets right? I've tried about every
> combination possible with no change.
>
> //Check if Author is entered & exists
> if( (strlen($_POST["first_nameIN"]) > 0) && (strlen($_POST["last_nameIN"])
> > 0) ) {
>  $sql2 = "SELECT (first_name, last_name)
>          FROM author WHERE (first_name LIKE '$first_nameIN'
>              && last_name LIKE '$last_nameIN)'";


LIKE is going to do full-text search, which isn't what you want when
searching for a specific author. You can have the query return the ID for
that specific author (to use in your $sql2a query).
$sql2 = "SELECT id FROM author WHERE first_name = '$first_nameIN' AND
last_name = '$last_nameIN' ";

    $result2 = mysql_query($sql2);
>     if (mysql_num_rows($result2) > 0) {

           $row = mysql_fetch_assoc($result2); // gives you the row return
from $sql2

>
>            $sql2a = "INSERT INTO book_author (authID, bookID, ordinal)
>                VALUES (author.id WHERE (first_name LIKE '$first_nameIN'
>                 && last_name LIKE '$last_nameIN'),
>                 book.ID WHERE book.title LIKE '$titleIN'), '1'";

With the change in $sql2 and the fact that the bookID is stored in $autoid,
this becomes:
$sql2a = "INSERT INTO book_author (authID, bookID, ordinal) VALUES (" .
$row['id'] . ", " . $autoid . ", '1')";

>
>             $result2a = mysql_query($sql2a, $db);
>        }
>      elseif (mysql_num_rows($result2) = 0) {
>          $sql2b = "INSERT INTO author (first_name, last_name)
>                VALUES ('$first_nameIN', '$last_nameIN')";
>             $result2b = mysql_query($sql2b, $db);

                $authorID = mysql_insert_id($result2b); // gives you the id
of the newly inserted author for use in your book_author table

>
>          $sql2c = "INSERT INTO book_author (authID, bookID, ordinal)
>                VALUES (author.id WHERE (first_name LIKE '$first_nameIN'
>                && last_name LIKE '$last_nameIN'), book.ID
>                WHERE book.title LIKE '$titleIN'), '1'";

With the addition of $authorID and the fact that bookID is stored in
$autoid, your $sql2c should now be:
$sql2c = "INSERT INTO book_author (authID, bookID, ordinal) VALUES (" .
$authorID . ", " . $autoid . ", '1')";

>
>             $result2c = mysql_query($sql2c, $db);
>        }
>    }


- Lex

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux