SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database not updating

    I have a page which looks like this:



    It lists out all the packages that have been assigned to categories and which category they were assigned to. They appear as drop down boxes so that you can make changes by selecting from packages and categories that appear in the database.

    All seems to work well, except it's not upating properly. Everything looks fine from what I can tell, but obviously something is wrong.

    Here's the code I have:

    PHP Code:
    <table><tr><td width="300px"><b>Package</b></td><td width="300px"><b>Category</b></td></tr>


    <?php
    require_once('connectvars.php');

    $dbh mysql_connect(hostnameusernamepassword)

        or die(
    "Unable to connect to MySQL");

    mysql_select_db ("morenotravel"$dbh);

    $result mysql_query("SELECT * FROM categorypackagelink cl, category c, packages p WHERE cl.category_id = c.category_id AND cl.package_id = p.package_id");

    $i 0;

    print 
    "<form name='managecategorypackage' method='post' action='admin_updatecategorypackagelink.php'>\n";

    while (
    $categorypackage mysql_fetch_array($result)) {
    //print $categorypackage['category_id'];
    //die();
    print"<tr><td>"
    ?>
    <select name="package_id"  />  <option value="disabled" >Select a Package:</option> 

     <?php

    $dbh 
    mysql_connect(hostnameusernamepassword)

        or die(
    "Unable to connect to MySQL");

    mysql_select_db ("morenotravel"$dbh);



    $query sprintf("SELECT package_name, package_id FROM packages ORDER BY package_name ASC");




    $resultp mysql_query($query);


    if (!
    $resultp) {

        
    $message  'Invalid query: ' mysql_error() . "\n";

        
    $message .= 'Whole query: ' $query;

        die(
    $message);

    }


    while (
    $row mysql_fetch_assoc($resultp)) {

    if (
    $row['package_id']==$categorypackage['package_id']){$selected =  " selected=true ";} else {$selected "";}
    echo 
    "<option value=\"".$row['package_id']. "\""$selected ">".$row['package_name']."</option>";

    }
    ?>
    </select><br><br>


    </td>

    <td>
    <select name="category_id"  />  <option value="disabled" >Select a Category:</option> 

     <?php

    $dbh 
    mysql_connect(hostnameusernamepassword)

        or die(
    "Unable to connect to MySQL");

    mysql_select_db ("morenotravel"$dbh);



    $query sprintf("SELECT category, category_id FROM category ORDER BY category ASC");




    $resultc mysql_query($query);


    if (!
    $resultc) {

        
    $message  'Invalid query: ' mysql_error() . "\n";

        
    $message .= 'Whole query: ' $query;

        die(
    $message);

    }


    while (
    $row mysql_fetch_assoc($resultc)) {

    if (
    $row['category_id']==$categorypackage['category_id']){$selected =  " selected=true ";} else {$selected "";}
    echo 
    "<option value=\"".$row['category_id']. "\""$selected ">".$row['category']."</option>";

    }
    ?>
    </select><br><br>

    </td></tr>

    <?php

    ++$i;
    }
    print 
    "<tr><td><input type='submit' value='submit' /></td></tr>";
    print 
    "</form>";
    mysql_close();
    ?>

    </table>
    Update code:

    PHP Code:
    <?php
    require_once('connectvars.php');

    $dbh mysql_connect(hostnameusernamepassword)

        or die(
    "Unable to connect to MySQL");

    mysql_select_db ("morenotravel"$dbh);

    // find out how many records there are to update
    $size count($_POST['catpkg_id']);
    //echo "number:" . $size;
    // start a loop in order to update each record
    $i 0;
    while (
    $i $size) {
    // define each variable
    $package_id$_POST['package_id'][$i];
    $category_id$_POST['category_id'][$i];
    $catpkg_id $_POST['catpkg_id'][$i];

    // do the update and print out some category just to provide some visual feedback
    // you might need to remove the single quotes around the field names, for example bookcategory = '$bookcategory' instead of `bookcategory` = '$bookcategory'
    $query "UPDATE categorypackagelink SET `package_id` = '$package_id', `category_id` = '$category_id' WHERE `catpkg_id` = '$catpkg_id' LIMIT 1";

    mysql_query($query) or die ("Error in query: $query");
    ++
    $i;
    }
    mysql_close();
    ?>
    Sorry for the long code.. I normally try to narrow it down to where I think the problem is, but I just have no idea.

    Here's what the database looks like:



    Anyone happen to have any ideas on what I would need to adjust to make this actually work? Thanks in advance! Much <3!

  2. #2
    SitePoint Enthusiast LSC-Rob's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, NSW Australia
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do a var_dump of the post array, is it sending what you're expecting?
    Rob G.
    ShopManager - Sales & Repair Business Management Software

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid I'm not familiar with that. I'll have to read up a bit and try it out =)

    thanks.


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
  •