...

View Full Version : Like named fields updating at same time



Meltdown
01-28-2008, 01:18 AM
Take results from query, allow individual row information to be changed (while view several columns)
Once you peek at the code, you'll understand what I'm doing. For some reason, though, it's not working. It doesn't seem to return any error, either. I don't know what else to do, really...it's probably something simple I'm missing..

--
username is already defined


<?
// Connect database.
$sql="SELECT * FROM media WHERE username='$username' AND flagged='1' AND description='' ORDER BY dateadded DESC";
$result=mysql_query($sql) or die(mysql_error());

// Count table rows
$count=mysql_num_rows($result);
?>
<table width="95%" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<tr>
<td>
<table width="95%" border="0" cellspacing="1" cellpadding="0">
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?><br><a href="thumb.php?src=<? echo $rows['imagename']; ?>&x=600&y=600&f=0"><img src="thumb.php?src=<? echo $rows['imagename']; ?>&x=200&y=200&f=0"></a></td>
<td align="center"><center><input name="description[]" type="text" id="description" value="<? echo $rows['description']; ?>"><br><b><? echo $rows['dateadded']; ?></b><br><? echo $rows['gallery']; ?></center></td>
</tr>
<?php
}
?>
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE media SET description='$description[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1) or die(mysql_error());
}
}

if($result1){
header("location:wizard3.php");
}

mysql_close();
}
?>

Fou-Lu
01-28-2008, 01:55 AM
Two possible options I can think of off the top of my head.
The first one is that it is actually updating, but because you are extracting the information to show first before updating you don't see the changes until you reload.
The seconds is the most likely problem: register_globals. By default, register globals are disabled in PHP, meaning $Submit and $description don't actually exist. You can test this by echoing out from the if ($submit) clause.
Instead, try like so:


if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE media SET description='$description[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1) or die(mysql_error());
}
}

if($result1){
header("location:wizard3.php");
}

mysql_close();
}

becomes:


if (isset($_POST['Submit']))
{
for ($i = 0; $i < $count; $i++)
{
$desc = mysql_real_escape_string($_POST['description'][$i]);
$sql='UPDATE media SET description=\'' . $desc . '\' WHERE id=' . (int)$id[$i];
mysql_query($sql) OR die(mysql_error());
}
mysql_close();
// Don't need to capture anything, since any errors will be trapped in a die.
header("Location: wizard3.php");
}
mysql_close();

On a side note, I believe that mysql supports multiple updates. This means you can go like so:


for (...)
{
$qry .= 'UPDATE media SET description=\'' . $desc . '\' WHERE id = ' . (int)$id[$i] . ';';
}
mysql_query($qry);

If I'm not mistaken.


I made a horrible assumption as well. The ID that you use is an integer value in the code that I used, if its not, you will need to filter it through a mysql_real_escape_string as well and drop the (int) typecasting.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum