Jump to content

Php Array help


patsman77

Recommended Posts

Hello All,

 

I am trying to update a form, which pulls from mysql database. The form displays correctly, but now I want to be able to update the form by changing some values as I go.

 

<body style="text-align: center">

<center><strong>League Payment Update Sheet</strong><br></center><br>

<?php
$host="localhost"; // Host name 
$username="xxx"; // Mysql username 
$password="xxxx"; // Mysql password 
$db_name="xxxx"; // Database name 
$tbl_name="xxxx"; // Table name  

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT Name, Total, Paid, Confirmed FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

// Count table rows 
$count=mysql_num_rows($result);
?>
<table width="33" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr> 
<td>
<table width="33" border="0" cellspacing="1" cellpadding="0">

<div><center>
<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Total</strong></td>
<td align="center"><strong>Paid</strong></td>
<td align="center"><strong>S</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>

<td align="center"><input name="Name[0]" type="text" size=20 id="Name" value="<? echo $rows['Name']; ?>"></td>
<td align="center"><input name="Total[0]" type="int" size=6 id="Total" value="<? echo $rows['Total']; ?>"></td>
<td align="center"><input name="Paid[0]" type="int" size=6 id="Paid" value="<? echo $rows['Paid']; ?>"></td>
<td align="center"><input name="Confirmed[0]" type="int" size=1 id="Confirmed" value="<? echo $rows['Confirmed']; ?>"></td>
</tr></center></div>

<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this 
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET Name='$Name[$i]', Total='$Total[$i]', Paid='$Paid[$i]', Confirmed='$Confirmed[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:uptest.php");
}
mysql_close();
?>

 

When I hit submit it just shows the current values and not the updated info. Any help available on this one? Not sure if matters, but there are other fields in the tablethat I don't vcall for or want updated.

 

Thanks,

 

Patsman77

Link to comment
https://forums.phpfreaks.com/topic/160921-php-array-help/
Share on other sites

I see that, i changed to

if (isset($_POST['Submit']))

 

but now when I load page I get error:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/swammisp/public_html/uptest.php:3) in /home/swammisp/public_html/uptest.php on line 69

 

and the update forces the values to empty text or 0 for integers. So the update is doing something, just not putting the array into the update.

 

Thanks,

 

Patsman77

Link to comment
https://forums.phpfreaks.com/topic/160921-php-array-help/#findComment-849236
Share on other sites

You can't use header after any output sent to the browser. That means all your HTML before it is causing it to not work. Even if the output sent is a single space, header still won't work. You need to put any header you have prior to any output to the browser.

 

As for the array, what is $Name, $Total, $Paid and $Confirmed? Are they arrays? If so -

$sql1="UPDATE $tbl_name SET Name='{$Name[$i]}', Total='{$Total[$i]}', Paid='{$Paid[$i]}', Confirmed='{$Confirmed[$i]}'";

 

But just so you know, that will update all rows in the table!

Link to comment
https://forums.phpfreaks.com/topic/160921-php-array-help/#findComment-849241
Share on other sites

I tried a couple things...

 

If i start $i=2; then the header error is gone, but still updates empty for name and 0 for other fields.

 

I set back to $i=0; and removed header line; get same output.

 

Yes - Name, Total, Paid, and Confirmed are all arrays pulled from mysql.

 

Thanks,

 

Patsman77

Link to comment
https://forums.phpfreaks.com/topic/160921-php-array-help/#findComment-849293
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.