Page 1 of 1

Update multiple rows in postgres database

Posted: Tue Jun 03, 2008 10:54 am
by broun
I have a couple of tables with different input column values. I am trying to update all the table at once but am failing. My problem looks to be my php skills. Please help!!!!!
I have this form code which returns the editable data:

Code: Select all

$rows = pg_num_rows($result);
    for ($k = 0; $k < $rows; $k++)
   {
 for ($j = 0; $j < $i; $j++) {
       $fieldname = pg_field_name($res, $j);
$retu = pg_fetch_result($result,$k,$j) ;
  if ($fieldname == "gid")
echo '<td class="hr">'. htmlspecialchars($retu) ;
else
echo '<td class="hr"><input name="'.$fieldname.$k. '" type="text" value="'. $retu .'">';
 
And this is the code for updating the database table:

Code: Select all

$norows = pg_num_rows($res);
 $i = pg_num_fields($res);
if(isset($_POST['update']))
{
$r = 0;
while ($r < $norows)
{
 
$sql1 = 'UPDATE'." $editable SET ";
 for ($j = 0; $j < $i; $j++)
{ $type .= pg_field_type($res, $j);
 
  $fieldname = pg_field_name($res, $j);
 
if ($j==$i-1 )
 {
 
 $sql1 .= " $fieldname =  ".@$_POST.'[ \''. $fieldname.$r.'\' ] ';
  }
  else
{
  $sql1 .= " $fieldname = ".@$_POST.'[ \''. $fieldname.$r.'\' ] '.', ' ;
}
}
$s =  ($r+1) ;
$sql1 .= "WHERE gid=$s".'; ';
//$result1= pg_query($conn,$sql1);
echo "$sql1";
++$r;
}
}
else
But then i get this output!
UPDATE material_parameters SET gid = Array[ 'gid0' ] , name = Array[ 'name0' ] , manning = Array[ 'manning0' ] , eqrough = Array[ 'eqrough0' ] , hazenwilliam = Array[ 'hazenwilliam0' ] , darcyweisbach = Array[ 'darcyweisbach0' ] WHERE gid=1; etc etc

Re: Update multiple rows in postgres database

Posted: Tue Jun 03, 2008 7:34 pm
by nowaydown1
I believe the problem is with your string concatenation. Try this is in place of your conditional and see what you get:

Code: Select all

 
if ($j==$i-1 ) {
    $sql1 .= sprintf("%s = '%s'", $_POST[$fieldName.$r]);
} else {
    $sql1 .= sprintf("%s = '%s', ", $_POST[$fieldName.$r]);
}