futrose Posted October 26, 2010 Share Posted October 26, 2010 I have been pulling my hair out for about 2 weeks with this problem. I am doing this on a localhost set up, not a live server. Here are the fields in the categories table in question: id, cattitle, caturl, catdesc, orderby, parentid What I am trying to accomplish is to create a dropdown list from the values in cattitle and parentid fields which I have done successfully. Here is the code from my "categories_modify.php" file where the dropdown is displayed. <form action="" method="POST"> <table cellspacing="5" cellpadding="1" border="0"> <tr> <td> Change Parent Category To: </td> <td> <select name="location"> <?php foreach ($categories as $loc): ?> <option name="parid" value="<?php htmlout($loc['parentid']); ?>"><?php htmlout($loc['cattitle']); ?></option> <?php endforeach; ?> </select> </td> </tr> <tr> <td> </td> <td> <input type="hidden" name="id" value="<?php htmlout($id); ?>" /> <input type="submit" value="Update" /> </td> </tr> </table> </form> The code generating the values in the form is in my controller script called index.php. Here is the code for that part. **Note that the top section of the script below populates a series of text boxes with current values in the database for editing/updating I did not show "edit" form in the code above as it works fine. The part of code populating my dropdown box values starts with the "locat... line except for my db.inc.php database connection at the top (which also works fine). if (isset($_POST['action']) and $_POST['action'] == 'Edit') { include $_SERVER['DOCUMENT_ROOT'] . './includes/db.inc.php'; { $id = mysqli_real_escape_string($link, $_POST['id']); $sql = "Select id, cattitle, orderby, caturl, catdesc, parentid from categories where id = '$id'"; $result = mysqli_query($link, $sql); if (!$result) { $error = 'Error fetching category details ' . mysqli_error($link); include 'error.php'; exit(); } $row = mysqli_fetch_array($result); $pagetitle = 'Edit Category'; $action = 'editform'; $id = $row['id']; $cattitle = $row['cattitle']; $orderby = $row['orderby']; $caturl = $row['caturl']; $catdesc = $row['catdesc']; $button = 'Update'; $locate = "Select id, cattitle, parentid from categories order by cattitle asc"; $res = mysqli_query($link, $locate); if (!$res) { $error = 'Error fetching category details ' . mysqli_error($link); include 'error.php'; exit(); } while ($locat = mysqli_fetch_array($res)) { $categories[] = array('id' => $locat['id'], 'cattitle' => $locat['cattitle'], 'parentid' => $locat['parentid']); } } include 'categories_modify.php'; exit(); } Now for the code that gets the POSTED info from the dropdown window. if (isset($_GET['location'])) { include $_SERVER['DOCUMENT_ROOT'] . './includes/db.inc.php'; $id = mysqli_real_escape_string($link, $_POST['id']); $parentid = mysqli_real_escape_string($link, $_POST['parid']); $parentid++; $sql = "Update categories set parentid = '$parentid' where id = '$id'"; if (!mysqli_query($link, $sql)) { $error = 'Error updating categories ' . mysqli_error($link); include 'error.php'; exit(); } header('Location: .'); exit(); } The goal is to take the parentid value and increase it by 1 so I can create subcategories based on the parentid value. (maybe not the best way but I'd still like to figure out why I can't make this work.) This line is where I think the problem is, or atleast part of it: $parentid = mysqli_real_escape_string($link, $_POST['parid']); I believe it is coming back blank because when I re-run the script for my "test" value it will always comeback as 1 (the default value for parentid is 0) 0 + 1 = 1. If I update the form again where the parentid now equals 1 it still equals 1, can't get it to 2. BTW I have tried every variation of the above line I could think of with no luck hence this post. Thanks for any help. I hope this was clear enough. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 26, 2010 Share Posted October 26, 2010 I would suggest edit your post and put code in code brackets. Quote Link to comment Share on other sites More sharing options...
futrose Posted October 26, 2010 Author Share Posted October 26, 2010 Hi revraz, first post on this forum, not sure how to edit, there wasn't a button to do that. Do I have to repost? Quote Link to comment Share on other sites More sharing options...
objnoob Posted October 26, 2010 Share Posted October 26, 2010 Where is the value of $_POST['action'] coming from? I don't see it coming from that particular form. Quote Link to comment Share on other sites More sharing options...
objnoob Posted October 26, 2010 Share Posted October 26, 2010 Oh.. <select name='Var_Name'> <option value='Var_Val1'>Val1</option> <option value='Var_Val2'>Val2</option> <option value='Var_Val3'>Val3</option> <select> $_POST['Var_Name'] is the value of option. $_POST['parid'] should be $_POST['location']?? $id = mysqli_real_escape_string($link, $_POST['id']); $parentid = mysqli_real_escape_string($link, $_POST['parid']); Quote Link to comment Share on other sites More sharing options...
futrose Posted October 26, 2010 Author Share Posted October 26, 2010 $_POST['action'] is coming from my categories.php file which has a list of all the categories and an "edit" button and a "delete" button. So if "edit" is pressed then it calls the categories_modify.php page and displays the current values of the category chosen to edit in a form and below that form has the dropdown form with the problem. Here is the $_POST['action'] script. Categories: <ul> <?php foreach ($cat as $cattitle): ?> <li> <form action="" method="post"> <div> <?php htmlout($cattitle['cattitle']); ?> <input type="hidden" name="id" value="<?php echo htmlout($cattitle['id']); ?>" /> <input type="submit" name="action" value="Edit" /> <input type="submit" name="action" value="Delete" /> </div> </form> </li> <?php endforeach; ?> </ul> Quote Link to comment Share on other sites More sharing options...
objnoob Posted October 26, 2010 Share Posted October 26, 2010 Be sure to close that </select> in my code Quote Link to comment Share on other sites More sharing options...
futrose Posted October 26, 2010 Author Share Posted October 26, 2010 So using your code example, what should my if (isset($_POST['locat'])) be set to? I had <form action="" method="POST"> but changed it to <form action="?locat" method="POST"> form code looks like this now <form action="?locat" method="POST"> <table cellspacing="5" cellpadding="1" border="0"> <tr> <td> Change Parent Category To: </td> <td> <select name="location"> <?php foreach ($categories as $loc): ?> <option name="parid" value="<?php htmlout($loc['parentid']); ?>"><?php htmlout($loc['cattitle']); ?></option> <?php endforeach; ?> </select> </td> </tr> <tr> <td> </td> <td> <input type="hidden" name="id" value="<?php htmlout($id); ?>" /> <input type="submit" value="Update" /> </td> </tr> </table> </form> and controller code looks like this if (isset($_POST['locat'])) { include $_SERVER['DOCUMENT_ROOT'] . './includes/db.inc.php'; $id = mysqli_real_escape_string($link, $_POST['id']); $parentid = mysqli_real_escape_string($link, $_POST['location']); $parentid++; $sql = "Update categories set parentid = '$parentid' where id = '$id'"; if (!mysqli_query($link, $sql)) { $error = 'Error updating categories ' . mysqli_error($link); include 'error.php'; exit(); } header('Location: .'); exit(); } still doesn't work though. here is what the html page source looks like on the categories_modify.php page incase it is helpful. <form action="?locat" method="POST"> <table cellspacing="5" cellpadding="1" border="0"> <tr> <td> Change Parent Category To: </td> <td> <select name="location"> <option value="0">About BBC</option> <option value="0">BBC Cares</option> <option value="0">Home</option> <option value="0">Ministries</option> <option value="0">New To BBC</option> <option value="0">News & Events</option> <option value="0">Resources</option> <option value="0">Sermons</option> <option value="1">test</option> </select> </td> </tr> <tr> <td> </td> <td> <input type="hidden" name="id" value="17" /> <input type="submit" value="Update" /> </td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
futrose Posted October 26, 2010 Author Share Posted October 26, 2010 GOT IT!!!! I had to set both of these lines to $_POST['location'] if (isset($_POST['location'])) and $parentid = mysqli_real_escape_string($link, $_POST['location']); objNoob... your post helped, thanks a lot!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.