Page 1 of 1

dinamic UPDATE using input text

Posted: Thu Nov 11, 2004 2:00 pm
by ratamaster
Hi
I’m developing a web site, there is one php page wich displays a series of input text, the input text amount displayed depends on the number of rows found in a db
e.g:
If there were 5 matches from a query, the php file shows me 5 input text
The next code, I wrote it to create input text with diferent names

Code: Select all

$i=1;
While($row=mysql_fetch_object($result)) {
..........................      
            <td>
                 <p align= 'center'><input type='text' name='newO".$i++."'size='1' value=0 maxlength='2'></p>
            </td>";
&#125;
Note the detail : 'newO".$i++
While the ‘while’ sentence run, the input text created will have diferent names. E.g:
the first one will be called newO1, next newO2,3,4,5, and so on.
My intention is to insert numbers in these input text, when it’s done, I press a submit button and I wish to run a SQL UPDATE using the numbers I wrote.
Thise UPDATE will be in the table called “imagen”, in the column “orden”
This is the code I wrote to achieve this (I dosen’t work as I want):

Code: Select all

if($_POST&#1111;'submit'])&#123;
$k=1;
$result=mysql_query("SELECT * FROM imagen") or die("wrong1");

while($row=mysql_fetch_object($result))&#123;
       $n = $_POST&#1111;'newO'.$k];
       mysql_query("UPDATE imagen SET orden = $n WHERE PicNum LIMIT 1") or die ("wrong2 ".mysql_error());
       $k++;
       &#125;
&#125;
I just can change a value, and not all the values I wrote before I do the submit
I hope someone can help me
Thanks

Alvaro

Posted: Thu Nov 11, 2004 2:12 pm
by rehfeld
do you want to update everything or not?

btw- this is not your problem, but i would do this:

Code: Select all

if (isSet($_POST['newO'.$k])) {
       $n = $_POST['newO'.$k];
       mysql_query("UPDATE imagen SET orden = $n WHERE PicNum LIMIT 1") or die ("wrong2 ".mysql_error()); 
}
it could help save the db one day....

Posted: Thu Nov 11, 2004 2:15 pm
by josh

Code: Select all

if($_POST['submit']){
$k=1;
$result=mysql_query("SELECT * FROM imagen") or die("wrong1");

while($row=mysql_fetch_object($result)){
       $n = $_POST['newO'.$k];
         //ADD THIS
         $query="UPDATE `imagen` SET `orden` = '$n' WHERE `PicNum` LIMIT 1";
       //CHANE THIS
          mysql_query($query) or die ("wrong2 ".mysql_error());
       //ADD THIS
        echo $query;
       $k++;
       }
//ADD THIS
echo $k;
}

I added a few things to output some "debugging info" and changed some stuff around, If $k == 1 at the end of the script then it is probably because of your WHERE clause "WHERE PicNum"

Should it be "WHERE PicNum = xxxxxx" where xxxxxxxx is a value??