SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing Form Var's to SQL Statement then Into DB

    I'm in the process of building a bookstore where customers can add Book Reviews. The sku# of a product is passed to a page level var called $sku via $_GET. I have a form that lets customers add reviews of a product. This is what it looks like:

    Code:
    <form name="review_form" action="review_added.php" method="post">
    
    <input type="hidden" value="<?=$sku ?>" name="prod_sku">
    
    <p>
    <label for="first">Firstname</label><br />
    <input type="text" value="<? $firstname ?>" id="firstname" name="firstname" maxlength="30" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';" class="text" /></p>
    
    <p>
    <label for="last">Lastname</label><br />
    <input type="text" value="<? $lastname ?>" id="lastname" name="lastname" maxlength="50" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';" class="text" /></p>
    
    <p>
    <label for="title">Review Title</label><br />
    <input type="text" value="<? $review_title ?>" id="review_title" name="review_title" size="60" maxlength="100" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';" class="text" />
    </p>
    
    <p>
    <label for="review">Your Review</label><br />
    <textarea cols="40" rows="15" id="review" name="review" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';"></textarea>
    </p>
    <input type="submit" value="Add Review" />
    </form>
    Here is the process page of this form:

    PHP Code:
    //review_added.php


    db_connect();
    $reviews_table "greg_php_reviews";

    /* insert into table */
    $sql "INSERT INTO $reviews_table
    (id, firstname, lastname, reviewDate, title, review, sku)
    VALUES
    ('', '
    $firstname','$lastname', NOW(), '$review_title', '$review', 'prod_sku')";

    /*get results */

    $query mysql_query($sql) or die(mysql_error());

    ?> 
    My problem is filling the sku field in the table. I am writing the value of $sku in the form as a hidden value and I would like this to be put into the database. Say that $sku for a specific product is 888888. How do I get 88888 into the db sku field?

    Thanks!

  2. #2
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...
    PHP Code:
    //review_added.php 

    $prod_sku $_POST['prod_sku'];

    db_connect(); 
    $reviews_table "greg_php_reviews"

    /* insert into table */ 
    $sql "INSERT INTO $reviews_table 
    (id, firstname, lastname, reviewDate, title, review, sku) 
    VALUES 
    ('', '
    $firstname','$lastname', NOW(), '$review_title', '$review', '$prod_sku')"

    /*get results */ 

    $query mysql_query($sql) or die(mysql_error()); 

    ?> 
    Lats...

  3. #3
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Lats ... I echoed $prod_sku on review_added.php and it displays the right value. However, when I look at the db the value has changed to something else. The sku field in MySQL is a not null int. Should I change this?

    Thanks!

  4. #4
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've changed the sku field to a not null VARCHAR and things seem to be working (the value is inserted correctly). Why did MySQL not like the sku as INT?

  5. #5
    SitePoint Member
    Join Date
    Nov 2004
    Location
    USORA
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "gregor002"
    If you are making add-on for your script (review) of list's book's script than you need to catch "id" of book that is curently open for reviews... if you put form in place where you list information about book for exm.
    print "Book title: $row[title]<br>";
    print "Pages: $row[pgs]<br><br><br><br>";
    print "Reviews of this book...<br><br>";
    ----
    In this case at and of all information you give put your form... (BUT CLOSE PHP)
    ?>

    <form name="review_form" action="review_added.php" method="post">

    <input type="hidden" value="<?=$row[pgs] ?>" name="prod_sku">

    <p>
    <label for="first">Firstname</label><br />
    <input type="text" value="<? $firstname ?>" id="firstname" name="firstname" maxlength="30" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';" class="text" /></p>

    <p>
    <label for="last">Lastname</label><br />
    <input type="text" value="<? $lastname ?>" id="lastname" name="lastname" maxlength="50" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';" class="text" /></p>

    <p>
    <label for="title">Review Title</label><br />
    <input type="text" value="<? $review_title ?>" id="review_title" name="review_title" size="60" maxlength="100" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';" class="text" />
    </p>

    <p>
    <label for="review">Your Review</label><br />
    <textarea cols="40" rows="15" id="review" name="review" onfocus="this.style.background='#fff';" onblur="this.style.background='#eee';"></textarea>
    </p>
    <input type="submit" value="Add Review" />
    </form>

    -----------------------
    or you can make link to book's like this
    print "<a href=book_open.php?id=$row[title]> $row[title]</a><br>";
    and when somone click it will goto page that contains inform. about book and review.. and than you can catch id (sku ) of curennt book by doing this...
    $suk=$_GET[id];
    and you have var. that contains id of book.

  6. #6
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregor002
    ...the value has changed to something else.
    Interesting, what did it change 888888 to?
    Lats...

  7. #7
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lats
    Interesting, what did it change 888888 to?
    I believe the sku in question was 2121212121 and it was changed to 2147483647

    WTF?

  8. #8
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nm ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •