Hi all,

I have trouble in updating my data using update query in which i need to update the a particular student id.Currently in my database table, I have the attendance table below and i need to update it using php update statement.

ID Date
143 1/1/10
143 2/1/10
143 3/1/10
157 1/1/10
157 2/1/10

Since I have no problem extracting data of the particular student id, you can ignore the select statement and I would only show you part of the php code that I need help with the update query part .

Part of my first page of php code:

<?php
include 'dbconfig.inc.php';
$studentid = $_GET['ID'];
$class = $_GET['class'];
$sql = "SELECT ........";
$result = mysqli_query($link,$sql)or die(mysqli_error($link));
$entries = mysqli_num_rows($result);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>
 Attendance
    </title>
    <link rel="stylesheet" type="text/css" href="default.css">
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  </head>
  <body>
    <div id="article">
<script type="text/javascript" src="datetimepicker.js"></script>
<form method="post" action="doupdate.php" name="form1"> 

<table border ="1" cellspacing="3" cellpadding="3"> 

<tr> 
<td colspan="2"><b>Date of Attendance> </b></td>
</tr> 
<?php
while($row = mysqli_fetch_assoc($result)){ 
?>
<input type="hidden" name="ID[]" value="<?php echo $row['ID'];?>" /> 
<tr> 
<td><label for="date[]">Date and Time:</label></td>
<td><input type="text" id="date[]" name="date[]" value="<?php echo $row['Date'];?>"/>
<a href="javascript:NewCssCal('date[]','yyyymmdd','dropdown',true)">
		<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
	    		
</tr>

<?php
}
?>

<tr> 
<td align="center" colspan="2"><input type="submit" name="submit" value="Submit" /></td> 
</tr> 
</table> 
</form>

Code for doupdate.php page:

<?php

include 'dbconfig.inc.php';
if(isset($_POST['submit'])){ 
$date = $_POST['date'];  
 $studentid = $_POST['ID'];
		
for($i=0;$i<$entries;$i++){ 
$query ="UPDATE attendance SET Date ='".$date[$i]."' WHERE ID = '".$studentid[$i]."'";
$result = mysqli_query($link,$query) or die(mysqli_error($link));
}
}

?>

In addition to that, i also need help with the javascript calender in which if i click the second and other rows of the calendar, only the value of first date of the textbox change but not the corresponding texbox that is align with the calender.I would appreciate it if someone can help me identify where I have gone wrong in my coding.

Thanks in advanced.

Member Avatar for brewbuff

Try: $query ="UPDATE attendance SET Date ='$date[$i]' WHERE ID = '$studentid[$i]'";

Hi brewbuff,

I've had tried your suggestion and it does not work.The updated value is not stored in the database.

Member Avatar for brewbuff
$i = 0;
$studentid = array(0 => 143);
$date = array(143 => "1/1/10");
$id = $studentid[$i];
$query ="UPDATE attendance SET Date ='$date[$id]' WHERE ID = '$id'"; 
echo $query;

Produces:
UPDATE attendance SET Date ='1/1/10' WHERE ID = '143'

Is that the update query you are looking for? If not, perhaps ID should be numeric (without quotes). I don't know how you defined it.

Try it:

<?php
$query ="UPDATE attendance SET Date ='".$date[$id]."' WHERE ID = ".$id;
if(@mysql_db_query('db',$query,$connection))
{
  echo "Success!";
} else{
  echo "Error: ".mysql_error();
}
?>

Use this function!

mysql_db_query('databse_name','query','connection');

Hi all,

I have tried all the suggestion here but unfortunately my update still does not work. When I went to view page source, the value of being updated is being stored in an array but unfortunately it does not retain the updated value in the form as well as in the database.

This is from my view source page:

UPDATE attendance SET Date ='Array' WHERE ID = 'Array'Array
(
    [ID] => Array
        (
            [0] => 143
            [1] => 143
        )
 
    [date] => Array
        (
            [0] => 2010-12-13 00:00:00
            [1] => 2010-12-12 20:00:00
        )
 
    [submit] => Submit
)
1

I also have followed this website example(http://www.phpeasystep.com/mysql/10.html)but unfortunately it does not work for me too.Anyone else has anymore suggestion?
Thanks in advance.

Member Avatar for diafol

That update suggested will update ALL records for the particular user if it works at all.

ALso place all your fields in backticks: `date`. DATE is a mysql keyword.

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.