SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru prequel's Avatar
    Join Date
    Nov 1999
    Location
    Brisbane, Australia
    Posts
    682
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    review script help

    I'm trying to write my first product review script where

    - the products are already populated into the database by me
    - the visitor browses through the products' database and can submit a review of them (haven't started on this part)

    PROBLEM:
    1. i'm currently having a problem with adding products to the product table which contains a drop down menu with brandnames pulled from the brand table. The form is on page named addproduct.php

    the incomplete and messed up code in addproduct.php is (seem to be missing INSERT into brand table on adding a product right ?)

    PHP Code:
    <?php
    // addproduct.php

    $dcnx = @mysql_connect("localhost""mysqluser""password");
        if (!
    $dcnx) {
        die(
    '<p>Unable to connect to database</a>');
        } else {
        echo 
    "Connection Ok";
        }

    mysql_select_db(reviewdb);
        if (!
    mysql_select_db(reviewdb)) {
        die(
    '<p>Unable to select database:' .
        
    mysql_error() . '</p>');
        }

    ?>

    <?php

    echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
    <p>Add new product to database:<br />
    Product Brand: <select name=\"selectbrand\">
    <option selected>select brand name</option>
    "
    ;
    ////////////////////////////////////////////////////////////////////////
        
    $brandsql "SELECT brandid, brandname FROM brand ORDER BY brandname ASC";
        
    $result = @mysql_query($brandsql);
        if(!
    $result) {
        die(
    '<p>Unable to perform query:' .
        
    mysql_error() . '</p>');
        }
      while ( 
    $brandarray = @mysql_fetch_array($result) ) {
      
    // while begin
        
    $brandnamelist "<option value=\"$brandarray[brandname]\">$brandarray[brandname]</option><br />";
        echo 
    $brandnamelist;
                 } 
    // while end
    ////////////////////////////////////////////////////////////////////////
    echo "</select><br />
    Product Model: <input type=\"text\" name=\"productmodel\" /><br />
    Product Size: <input type=\"text\" name=\"productsize\" /><br />
    Product Colour: <input type=\"text\" name=\"productcolour\" /><br />
    <input type=\"submit\" name=\"submitproduct\" value=\"SUBMIT\" />
    </p></form>"
    ;
    ?>

    <?php
    if ( !isset($submitproduct) ) {
        echo 
    "Fill in form the above to add product to database: ";
        } else {
        
    $insertdb "yes";

        
    // check to see if product form fields are empty
    //        if ($_POST['brandname'] == "") {
    //     $brandname_error = "<br />Please enter Brandname...<br />";
    //    $insertdb = "no";
    //    }
            
    if ($_POST['productmodel'] == "") {
         
    $productmodel_error "<br />Please enter Product Model...<br />";
        
    $insertdb "no";
        }
            if (
    $_POST['productsize'] == "") {
         
    $productsize_error "<br />Please enter Product Size...<br />";
        
    $insertdb "no";
        }
            if (
    $_POST['productcolour'] == "") {
         
    $productcolour_error "<br />Please enter Product Colour...<br />";
        
    $insertdb "no";
        }
        
    // if not empty insert product into brand database
        
    if ($insertdb == "yes") {
          
    $brandname $_POST['brandname'];
            
    $productmodel $_POST['productmodel'];
            
    $productsize $_POST['productsize'];
            
    $productcolour $_POST['productcolour'];

        
    $sql "INSERT INTO product SET
          productmodel = 
    $productmodel,
        productsize = 
    $productsize,
        productcolour = 
    $productcolour,
        adddate = CURDATE() "
    ;

            if (@
    mysql_query($sql)) {
            echo 
    "<p>Product was added successfully</p>";
            echo 
    "<p><a href=\"$_SERVER[PHP_SELF]\">Add another product</a></p>";
            } else {
            die(
    '<p>Unable to add product: ' .
            
    mysql_error() . '</p>');
            }
        } else { 
    // ($insertdb == "no")
        
    echo "$brandname_error";
           echo 
    "$productmodel_error";
          echo 
    "$productsize_error";
           echo 
    "$productcolour_error";
        }
        
    ////////////////////////////////////////////////////////
        
    // end else of !isset($submitproduct)

    ?>
    my database is named = reviewdb and below is the table structure

    Code:
    CREATE TABLE brand (
    brandid INT NOT NULL AUTO_INCREMENT,
    brandname VAR(255) NUT NULL
    )
    
    CREATE TABLE product (
    productid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    productmodel VARCHAR(255) NOT NULL,
    productsize INT NOT NULL,
    productcolour  VARCHAR(50) NOT NULL,
    adddate DATE NOT NULL
    )
    I have no problems adding the brandname of the product to brand table with this below code in a page called addbrand.php

    PHP Code:
    <?php
    // addbrand.php page

    $dcnx = @mysql_connect("localhost""mysqluser""password");
        if (!
    $dcnx) {
        die(
    '<p>Unable to connect to database</a>');
        } else {
        echo 
    "Connection Ok";
        }

    mysql_select_db(reviewdb);
        if (!
    mysql_select_db(reviewdb)) {
        die(
    '<p>Unable to select database:' .
        
    mysql_error() . '</p>');
        }

    $addbrandform "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
    Add Brandname: <input type=\"text\" name=\"brandname\" value\"
    $brandname\"><br />
    <input type=\"submit\" name=\"submitbrand\" value=\"Send\">
    </form>"
    ;

    if ( !isset(
    $submitbrand) ) {
        echo 
    $addbrandform;
        } else {
        
    ////////////////////////////////////////////////////////
        
    $insertdb "yes";
        
    // check to see if brandname field is empty
        
    if ($_POST['brandname'] == "") {
         
    $brandname_error "<p>Please enter Brandname...<p>";
        
    $insertdb "no";
        }
        
    // if not empty insert brandname into brand database
        
    if ($insertdb == "yes") {
        
    $brandname $_POST['brandname'];
        
    $sql "INSERT INTO brand SET brandname='$brandname' ";
        if (@
    mysql_query($sql)) {
        echo 
    "<p>Brandname was added successfully</>";
        echo 
    "<p><a href=\"$_SERVER[PHP_SELF]\">Add another Brandname</a></p>";
        } else {
        die (
    '<p>Unable to add Brandname to database:' .
        
    mysql_error() . '</p>');
        }
        } else { 
    // ($insertdb == "no")
        
    echo "$brandname_error";
        echo 
    "$addbrandform";
        }
        } 
    // else of !isset($submitbrand)

    ?>
    the brand table is populated with 36 entries which are

    brandid. brandname

    1. 3M
    2. Acer
    3. AG Neovo
    4. AOC
    5. BenQ
    6. Compaq
    7. Cornea
    8. CTX
    9. Dell
    10. Eizo
    11. Hercules
    12. Hewlett Packard
    13. Hitachi
    14. Hyundai
    15. IBM
    16. Ipex
    17. KDS
    18. LG
    19. Likom
    20. MAG
    21. Microtek
    22. Mitsubishi
    23. NEC
    24. Olympic
    25. Omni
    26. Panasonic
    27. Philips
    28. Planar
    29. Polyview
    30. Princeton
    31. Proview
    32. Sampo
    33. Samsung
    34. Sony
    35. Sylvania
    36. ViewSonic
    i want to be able to sort/browse products by brandname, productsize and productcolour as well, anyone would like to steer me in the right direction ?

  2. #2
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Dunno how much of the code was missing, but I cleaned it up a bit. Your select name was referenced different than it was named, and yeah, you didnt even have brandname in the query. See if this works...


    PHP Code:
    <html>

    <head>
    <title>Add item</title>
    </head>

    <body>

    <?php 

    $dcnx 
    mysql_connect("localhost""mysqluser""password" ) or die('<p>Unable to connect to database</a>');
    mysql_select_db("reviewdb"$dcnx) or die('<p>Unable to select database:'mysql_error() .'</p>'); 

    if( !isset(
    $_POST['submitproduct']) )
    {
    ?>
    <form action="addproduct.php" method="post"> 
        <p>Add new product to database:<br /> 
        Product Brand: <select name="brandname"> 
        <optgroup label="Brand Names">
    <?php

        $brandsql 
    "SELECT brandname FROM brand ORDER BY brandname ASC"
        
    $result mysql_query($brandsql) or die('<p>Unable to perform query:'mysql_error() .'</p>'); 

        while( 
    $brandarray mysql_fetch_array($result) )
        { 
            echo 
    '<option value="' $brandarray[brandname] . '">' $brandarray[brandname] . '</option>' "\r\n"
        }
    ?>
        </optgroup>
        </select>
        <br /> 
        Product Model: <input type="text" name="productmodel" />
        <br /> 
        Product Size: <input type="text" name="productsize" />
        <br /> 
        Product Colour: <input type="text" name="productcolour" />
        <br /> 
        <input type="submit" name="submitproduct" value="SUBMIT" /> 
        </p>
    </form>
    <?php
    }
    else
    {

    if( empty(
    $_POST['brandname']) || empty($_POST['productmodel']) || empty($_POST['productsize']) || empty($_POST['productcolour']) )
    {
        
    $brandsql "SELECT brandname FROM brand ORDER BY brandname ASC"
        
    $result mysql_query($brandsql) or die('<p>Unable to perform query:'mysql_error() .'</p>'); 
        
    $dropdown '';
        while( 
    $brandarray mysql_fetch_array($result) )
        { 
            
    $dropdown .= '<option value="' $brandarray[brandname] . '">' $brandarray[brandname] . '</option>' "\r\n"
        }
    echo <<< EOD
    <form action="addproduct.php" method="post"> 
        <p>Add new product to database:<br /> 
        Product Brand: <select name="brandname"> 
        <optgroup label="Brand names">
        
    $dropdown
        </optgroup>
        </select>
        <br /> 
        Product Model: <input type="text" name="productmodel" value="
    {$_POST['productmodel']}" />
        <br /> 
        Product Size: <input type="text" name="productsize" value="
    {$_POST['productsize']}" />
        <br /> 
        Product Colour: <input type="text" name="productcolour" value="
    {$_POST['productcolour']}" />
        <br /> 
        <input type="submit" name="submitproduct" value="SUBMIT" /> 
        </p>
    </form>
    EOD;

    }
    else
    {

        
    $brandname addslashes$_POST['brandname'] ); 
        
    $productmodel addslashes$_POST['productmodel'] ); 
        
    $productsize addslashes$_POST['productsize'] ); 
        
    $productcolour addslashes$_POST['productcolour'] ); 

        
    $sql "INSERT INTO product SET productmodel = '" $productmodel "', productsize = '" $productsize "', productcolour = '" $productcolour "', brandname = '" $brandname "', adddate = CURDATE() "

            if (@
    mysql_query($sql))
            { 
                echo 
    '<p>Product was added successfully</p>' "\r\n"
                echo 
    '<p><a href="addproduct.php ">Add another product</a></p>'
            }
            else
            { 
                die(
    '<p>Unable to add product: 'mysql_error() . '</p>'); 
            } 

    }

    ?>
    </body>

    </html>
    Mike
    It's not who I am underneath, but what I do that defines me.

  3. #3
    SitePoint Guru prequel's Avatar
    Join Date
    Nov 1999
    Location
    Brisbane, Australia
    Posts
    682
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a bit late but thanks mike for the help.. anyway i rewrote the whole thing and it works now


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
  •