Hi everybody,

Actually I stuck in updating a date field in my page that I don't know why it doesn't want to update. I'm retrieving a data from my DB and I I made one column has Textfield that I enter the new date so I press the submit and I want it to be updated. I named the Textfield name="retur[]" because as the data coming from DB so I used array but can't update. I'm also using CheckBox so I can point to which record I want to update.

this is my code in my first page:

<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php
include 'functions.php';
$r= @mysql_query("SELECT * FROM bookoutonloan");

$count=@mysql_num_rows($r);


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Return Page</title>
<style type="text/css">
.style2 {
	color:#FFF;
	font-weight: bold;
	
}

.style4 {  color:#999; 
		   font:Tahoma, Geneva, sans-serif}

</style>
<link href="my.css" rel="stylesheet" type="text/css" />
</head>
<form id="form1" name="form1" action="returnbook6.php" method="POST" >
<center>

<table width="99%" height="97" border="1" bordercolor="#BECDBF">
  <tr class="style2">

    <td width="6%" height="27" bgcolor="#92B0D1">Borrow ID</td>
    <td width="6%" bgcolor="#92B0D1">Stud ID</td>
    <td width="6%" bgcolor="#92B0D1">Book ID</td>
    <td width="11%" bgcolor="#92B0D1">User ID</td>
    <td width="9%" bgcolor="#92B0D1">Library ID</td>
    <td width="7%" bgcolor="#92B0D1">Date Loaned</td>
    <td width="9%" bgcolor="#92B0D1">datereturned</td>
    <td width="6%" bgcolor="#92B0D1">Amount of Fine</td>
    
    <td width="4%" bgcolor="#92B0D1">check for delete</td>
	
  </tr>
  <?php
  while($row=mysql_fetch_array($r , MYSQL_ASSOC))
  { 
 // extract($row)
	  ?>
  <tr>

    <td class="style4"><?php echo $row['borrowid']; ?></td>
    <td class="style4"><?php echo $row['stid']; ?></td>
    <td class="style4"><?php echo $row['bookid']; ?></td>
    <td class="style4"><?php echo $row['userid']; ?></td>
    <td class="style4"><?php echo $row['libraryid']; ?></td>
    <td class="style4"><?php echo $row['dateborrow']; ?></td>
    <td class="style4"><input name="retur[]" type="text" size="15" id="retur[]" /></td>
    <td class="style4">SR <?php echo $row['amountoffine']; ?></td>
    
        <td><input name="c[]" type="checkbox" id="c[]" value="<?php echo $row['borrowid']; ?>" /></td>
  </tr>
  <?php //value="<?php echo $row['datereturned']; " 
  }
  ?>
</table>
<center>
<p>
 <input name="checkfine" type="submit" value="Check For Fine" />
</p>
</form>
<body>
<center>

<p>&nbsp;</p>
</center>
</body>

</html>

My second page code

<?php
include 'functions.php';
$submit = $_POST['checkfine'];
$returned = $_POST["retur"];


if (isset($submit))
		  {
	for($i=0;$i<count($_POST['c']);$i++)
				
				{
					
	for($b=0; $b<count($_POST['retur']); $b++)
					
{


	$query = ("UPDATE bookoutonloan SET datereturned = '".$_POST['retur'][$b]."' where borrowid = '".$_POST['c'][$i]."' ") ;
	
	echo $query;
$final = mysql_query($query) or die(mysql_error()); 


}

}
		  }
?>

The result that output from these code that I tested on four records that I get from DB. The first three records I found that this is the output, borrowid 15-16-17

UPDATE bookoutonloan SET datereturned = '2010-04-04' where borrowid = '15' UPDATE bookoutonloan SET datereturned = '' where borrowid = '15'

But when I enter the date on borrow=14 the last record I found that I got the output result opposite than the previous one, which I don't know why it happens like that and I got two updates one so the previous code of output, the second update remove the date so I found that the DB is empty but this last record the first update is empty and the second update is correct which entered and it will be set in the DB:

UPDATE bookoutonloan SET datereturned = '' where borrowid = '14' UPDATE bookoutonloan SET datereturned = '2010-04-04' where borrowid = '14'

Is there any mistake please help to fix my code and direct me. Thank you very much

Sorry for my mistake for the topic that I meant TextField.

can you explain what do you need the second for() loop.


for($b=0; $b<count($_POST); $b++)

I think the second for() loop is not needed.

Dear as.bhanuprakash,
Thank you very much for replying to me. Actually I got a clue in in the result that output in the browser, that after when I have 2 records only from DB I found that the first record that I enter the date it will be entered but it will be removed because the code will take the value from my second textfield and it will find it empty so it will set the one that I choosed to '' so nothing will be. So when I wrote the second For loop I wanted to take the value of the array of TextField and I think what you are saying is correct. No need of the second loop but the question is, how can I take value array of TextField?. May be I need complex array I'm not sure. Hope if you could help to solve my problem. Thank you very much.

Member Avatar for diafol

I don't get the values as arrays in your html:

<input name="retur[]" type="text" size="15" id="retur[]" />
<input name="c[]" type="checkbox" id="c[]" value="<?php echo $row['borrowid']; ?>" />

Each 'id' attribute should be unique. You can however place array-type values for the 'name' attribute.

Member Avatar for diafol

unique ids can be done by count ($i) or by $id from DB:

loop...
$id = $result['id'];
echo "<input ... id=\"c_{$id}\" .../>";
...end loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.