Page 1 of 1

Beginner- passing multipal values problem

Posted: Tue Jan 08, 2008 3:47 am
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>

Posted: Tue Jan 08, 2008 3:39 pm
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

Posted: Tue Jan 08, 2008 4:43 pm
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:

Posted: Tue Jan 08, 2008 5:38 pm
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.

Posted: Tue Jan 08, 2008 5:40 pm
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!

Posted: Tue Jan 08, 2008 5:49 pm
by superdezign
Good.
Naturally, the next step would be to clean up the SQL injection possibilities.

Posted: Tue Jan 08, 2008 5:56 pm
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