Jump to content

Update Textfile with Checkbox doesn't work


RomeoX

Recommended Posts

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> </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

Link to comment
Share on other sites

Your nested loops firstly loop through each checked item. Then for each checked item the inner loop sets `datereturned` equal to the value of the current checkbox. Thus when the loops are finished the value of every row with a `borrowid` id that was checked will equal the value of the last item in the retur[] array.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.