Jump to content

Re-Order by ID Number


daebat

Recommended Posts

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>]";
}

?>

Link to comment
Share on other sites

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'/>

Link to comment
Share on other sites

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>]";
}

?>

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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'>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.