daebat Posted January 28, 2010 Share Posted January 28, 2010 I have a cms set up for inputting an image, thumb, title, pdf, and eps. When input the database assigns an ID number. The front end displays each upload by ID number. I'm trying to set up the back end so that a user can re-order by changing the ID number. I can get it to display the ID number in an editable text area, display thumb and title for each entry. With the code below it doesn't update the database ID number when I save. What am I doing wrong here? (Just to let you know this code is a modified version of my 'delete entry' code that works) <?php if($session->logged_in){ mysql_connect("localhost", "yeee", "eeeee") or die(mysql_error()) ; mysql_select_db("eeeeee") or die(mysql_error()) ; if(!isset($cmd)) { $result = mysql_query("select * from test order by id"); while($r=mysql_fetch_array($result)) $title=$r["title"]; $id=$r["id"]; $thumb=$r["thumb"]; echo "<div style='display:block; width:400px; padding-left:25px;padding-right:25px; margin:auto; background-color:#ffffff;'> <img src='images/menu-top.jpg' style='width:450px;margin-left:-25px;'><br /> <form> <table> <tr> <td><input type='text' name='id' value='$id' size='3'></td><td width='350px'><strong>$title</strong></td> </tr> <td colspan='2' align='center'><img src='upload/test/$thumb'></td> </table></form>"; } } ?> <?php echo "<div style='text-align:center;'><a href='testorder.php?cmd=order&id=$id'>Submit</a></div>"; ?> <?php if($cmd=="order") { $sql = "UPDATE test SET id='$id'"; $result = mysql_query($sql); echo "UPDATED!"; echo "<a href='main.php'>Click here to return to the Administration Area.</a>"; } } else{ echo "[<a href='main.php'>Please Login</a>]"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/ Share on other sites More sharing options...
mapleleaf Posted January 28, 2010 Share Posted January 28, 2010 Your submit is a link and not part of your form Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003137 Share on other sites More sharing options...
daebat Posted January 28, 2010 Author Share Posted January 28, 2010 You know, I thought that would be the problem... so should I do this: <form method='post' action='testorder.php?cmd=order&id=$id' enctype='multipart/form-data'> <input TYPE='submit' name='upload' title='Add data to the Database' value='Submit'/> Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003139 Share on other sites More sharing options...
mapleleaf Posted January 28, 2010 Share Posted January 28, 2010 and close the </form> tag after the Upload(submit) button Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003142 Share on other sites More sharing options...
daebat Posted January 28, 2010 Author Share Posted January 28, 2010 I did this and when I click my submit button it doesn't do anything... new code: <?php if($session->logged_in){ if(!isset($cmd)) { $result = mysql_query("select * from test order by id"); while($r=mysql_fetch_array($result)) { //grab the title and the ID of the enws $title=$r["title"];//take out the title $id=$r["id"];//take out the id $thumb=$r["thumb"]; echo "<div style='display:block; width:400px; padding-left:25px;padding-right:25px; margin:auto; background-color:#ffffff;'> <img src='images/menu-top.jpg' style='width:450px;margin-left:-25px;'><br /> <form method='post' action='testorder.php?cmd=order&id=$id' enctype='multipart/form-data'> <table> <tr> <td><input type='text' name='id' value='$id' size='3'></td><td width='350px'><strong>$title</strong></td> </tr> <td colspan='2' align='center'><img src='upload/test/$thumb'></td> </table></form>"; } } ?> <?php echo "<div style='text-align:center;'><input TYPE='submit' name='update' title='Update the Database' value='Submit'/></div></form>"; ?> <?php if($cmd=="order") { $sql = "UPDATE test SET id='$id'"; $result = mysql_query($sql); echo "UPDATED!"; echo "</div>"; } } else{ echo "[<a href='main.php'>Please Login</a>]"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003145 Share on other sites More sharing options...
daebat Posted January 28, 2010 Author Share Posted January 28, 2010 Help! I can't figure this out! Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003215 Share on other sites More sharing options...
mapleleaf Posted January 29, 2010 Share Posted January 29, 2010 It looks like you need a WHERE statement in your update query. $sql = "UPDATE test SET id='$id'"; // you need to add a WHERE id = $id if that is what you are trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003499 Share on other sites More sharing options...
citricsquid Posted January 29, 2010 Share Posted January 29, 2010 You probably shouldn't be having an ID that isn't non-unique and auto_increment. If someone wants to link to a post the ID is normally what would use, but if that can be changed... Instead have a unique ID and an order value; order can simply reflect the ID until it's changed. Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003694 Share on other sites More sharing options...
aneesme Posted January 29, 2010 Share Posted January 29, 2010 Remove the </form> tag in this line. <td colspan='2' align='center'><img src='upload/test/$thumb'></td> </table></form>"; Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003703 Share on other sites More sharing options...
aneesme Posted January 29, 2010 Share Posted January 29, 2010 Also before this line, if(!isset($cmd)) Add this, $cmd = isset($_GET['cmd'])?$_GET['cmd']:null; Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003704 Share on other sites More sharing options...
daebat Posted January 29, 2010 Author Share Posted January 29, 2010 Remove the </form> tag in this line. Why would I not close the form? I should still close it further on down the page right? // you need to add a WHERE id = $id if that is what you are trying to do. I added this but my submit button is still not doing anything... Can I submit the form without the submit button? and just use the link? Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003740 Share on other sites More sharing options...
DavidAM Posted January 29, 2010 Share Posted January 29, 2010 Can I submit the form without the submit button? and just use the link? The short answer is No you can't do that. Why would I not close the form? You are closing the form when you output the submit button. However, there is a problem. You are creating a seaparte form for each row in the table: Psuedo Code IF User Is Logged In { IF NO Command was Issued { Select the rows ordered by ID WHILE (For Each Row Selected) { //grab the title and the ID of the enws Start a new DIV Show the Image Start a FORM Start a TABLE Start a ROW (in the Table) Show the ID and Title in separate cells NOTE: If there are multiple rows, they will ALL have the same NAME for the INPUT box End the ROW Show the Thumbnail in a CELL (NOTE you are NOT in a ROW of the TABLE here) Close the TABLE Close The FORM END of WHILE (For Each Row) END of IF NO COMMAND ISSUED START a new DIV SHOW the SUBMIT button CLOSE the FORM (NOTE There is NO FORM open at this point) NOTE: There are two DIVs openned that have NOT been closed IF COMMAND is ORDER { Execute SQL to UPDATE database (NOTE: This query "UPDATE test SET id='$id'" will UPDATE EVERY ROW in the table!!!!) Close the DIV (is there one OPEN here? END IF COMMAND is ORDER END IF User is Logged In ELSE (User is NOT Logged in) Show Login Link END ELSE (User NOT Logged In) You need to do one of two things. 1) Put the SUBMIT button inside the form (in a CELL in the TABLE) in the WHILE loop. This will show multiple Submit buttons and will only allow the user to change one ID at a time. 2) Start the FORM before the WHILE loop; Change the NAME of the ID's input box so you can tell which one is changed. I would use: <input type='text' name='id[$id]' value='$id' size='3'> so the posted data is an array indexed by the OLD ID (the one still in the database) and the value is the NEW ID (what the user wants it changed to). Then you have to change your UPDATE processing to handle the array. Note that this WILL be problematic if the user change (for instance) 3 to 4 and 4 to 3. Having said all that, I have to agree with citricsquid, that this is not the best way to do this. In fact, if ID in your table is AUTO INCREMENT, you can not change the value. You should have a separate column for sequence, and then update the sequence based on the ID. So the input box would look more like this: <input type='text' name='id[$id]' value='$seq' size='3'> Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003753 Share on other sites More sharing options...
daebat Posted January 29, 2010 Author Share Posted January 29, 2010 Thanks David. So there is no way to update several entries at once? The problem is that if I set ID number 10 to ID number 2 and two already exists... won't that cause a problem? I'm seeing that the more and more I do with this, the more I need to look into adding a field for ordering. Quote Link to comment https://forums.phpfreaks.com/topic/190124-re-order-by-id-number/#findComment-1003765 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.