Beginner- passing multipal values problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Beginner- passing multipal values problem

Post by Addos »

Hi,
I have a form (sample image: http://www.ahamay.com/sort.htm ) that has a sort column in it. I need to find out how to pass the values of the sort column to the database once the ‘Update record’ button is clicked. My problem is that my very limited knowledge of PHP is stopping me understand how to pass the multiple ‘sort’ values to the following string:

Code: Select all

$updateSQL = sprintf("UPDATE compositions_solo
SET sort=%s
WHERE solo_ID=%s",
                      GetSQLValueString($_POST['sort']), 
                       GetSQLValueString($_POST['soloid']));

I guess I need some kind of array but I’m really unsure as to where to find out how to do this. I have read and read up on this but nothing points me to the solution. Can anyone help/show/point me in beginners language as to what I need to do.

I know I need to pass something like the following but as I say I’m stuck with not knowing what syntax to use.

UPDATE compositions_solo
SET sort=5, 3, 2, 4
WHERE solo_ID=14, 12, 15, 16


The code below is a snip of what I’m using to return the values in the http://www.ahamay.com/sort.htm sample.

Code: Select all

mysql_select_db($database_*****, $****);
$query_GetSolo_Inst = "SELECT * FROM compositions_solo";
$GetSolo_Inst = mysql_query($query_GetSolo_Inst, $****) or die(mysql_error());
$row_GetSolo_Inst = mysql_fetch_assoc($GetSolo_Inst);
$totalRows_GetSolo_Inst = mysql_num_rows($GetSolo_Inst);

  <form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<?php do { ?>
       <?php echo $row_GetSolo_Inst['Solo_Title']; ?>
		<? $sorte = array($row_GetSolo_Inst['sort']); 
       <input type="text" name="sort" value="<?php echo $sorte[0]; ?>" size="5">
     	    <? $soloID = array($row_GetSolo_Inst['solo_ID']); ?>   
    <input type="hidden" name="soloid" value="<?php echo $soloID[0]; ?>">
      <?php } while ($row_GetSolo_Inst = mysql_fetch_assoc($GetSolo_Inst)); ?>
      
      <input type="submit" value="Update record">
<input type="hidden" name="MM_update" value="form1">
  </form>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'm not sure what you're asking, and your image confused me even more. However, I'll assume that you're after a sort of 'weight' for your records, where you give them a weight value and they are ordered numerically according to the weight, correct?

If so, you only need to do two things.

The first thing is to add a new column to the database table called 'weight.' When you click update records, save the weight into that column for each table record that you are updating.

The second thing is to use a simple ORDER BY clause when retrieving the data from the database table.

Code: Select all

SELECT `tableName`.* FROM `tableName` ORDER BY `tableName`.`weight` ASC
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks for the reply and sorry if I’ve not been clear. Basically in my form I need to be able to ‘sort’ out the order of items in a numerical order. So for example at the moment the image of the form I posted has the following:
Last edited by Addos on Tue Aug 12, 2008 2:14 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Addos wrote:All that I’m stuck with is passing the multiple numerical (sort) values to the database in one go.
With one query for each item. You can do it with a loop. You won't be able to do it with one query, if that's what you're asking.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Ok I got this sorted. Here's what I did.

Code: Select all

for($i=0; $i < $_POST["sortCounter"]; $i++) {
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
if($_POST["sort"][$i] == ""){$_POST["sort"][$i] = 0;}
	  $updateSQL = sprintf("UPDATE compositions_solo
	   SET sort=%s
	   WHERE solo_ID=%s",
	   $_POST["sort"][$i],
	   $_POST["solo_ID"][$i]);
FROM

Code: Select all

<?php do { ?>
              <?php echo $row_GetSolo_Inst['Solo_Title']; ?>
        <input name="sort[]" type="text"  value="<?php echo $row_GetSolo_Inst['sort']; ?>" size="10">
         <input type="hidden" name="solo_ID[]" value="<?php echo $row_GetSolo_Inst['solo_ID']; ?>">
      <?php } while..........
Works a treat now!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Good.
Naturally, the next step would be to clean up the SQL injection possibilities.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks for the pointers and yep this page is under a secure login for a private client so I think it should be ok as it's not open to the general public.

Thanks again and by the way I really love your site and will enjoy reading through the tutiorials there.
Thanks again.
B
Post Reply