Reputation: 151
i made a table with few attribute code is
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Id</th>
<th>File-name</th>
<th>Purpose</th>
<th>Recieved-By </th>
<th>Processed-By</th>
<th>Adress</th>
<th>Contact-No</th>
<th>Date</th>
<th>Update</th>
</tr>`
and here is fetching data from database table in all column except update
<tbody>
<?php
if ( $search )
{
$p_query = "select * from files where recieved_by like '%$search%' or processed_by like '%$search%' or purpose like '%$search%' or file_name like '%$search%' order by id desc limit $page_start_from, $total_num_page";
}
else
{
$p_query = "select * from files order by id desc limit $page_start_from, $total_num_page";
}
$p_run=mysqli_query($con,$p_query);
if(mysqli_num_rows($p_run)){
while($row=mysqli_fetch_array($p_run))
{
$c_id=$row['id'];
$file=$row['file_name'];
$purpose=$row['purpose'];
$recieve=$row['recieved_by'];
$processed=$row['processed_by'];
$address=$row['address'];
$contact=$row['contact_no'];
$date=$row['date'];
$show_status=$row['show_status'];
?>
<tr>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $c_id;?></a></td>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $file;?></a></td>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $purpose;?></a></td>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $recieve;?></a></td>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $processed;?></a></td>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $address;?></a></td>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $contact;?></a></td>
<td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $date;?></a></td>
and now in update column there is changes when there is value in Update column from file table database then it will show in update column other wise it will open a action which is a button that have a form code is here
<td>
<?php if( !empty($show_status)){
echo $show_status;
}
else
{
?>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style=" padding-top: 3px; padding-bottom: 3px;">
Action <span class="caret"></span>
</button>
<ul class="dropdown-menu" style="margin-left:-50px;">
<?php
if(isset($_POST['submit-form']))
{
$update_date=$_POST['reason-date'];
$status=$_POST['reason'];
$p_id=$_POST['idvalue'];
$d_query="INSERT INTO update_table (id, reason, update_date) VALUES ('$p_id', '$status', '$update_date'); update files set show_status =' $status' where id=' $p_id'";
if(mysqli_multi_query($con,$d_query))
{
$msg="file have been submitted ";
header('location:index.php');
}
else
{
$error_msg="Already updated this file";
}
}?>
<script type="text/javascript">
function checkvalue()
{
var ureason=document.forms["sform"]["ureason"].value;
var udate=document.forms["sform"]["udate"].value;
if (ureason=="")
{
alert("Status Field is blank ");
return false;
}
if (udate=="")
{
alert("Date Field is blank ");
return false;
}
}
</script>
<form role="banner" class="actionform" action="index.php" method="POST" name="sform" onsubmit=" return checkvalue()">
<input type="hidden" name="idvalue" value="<?php echo $c_id; ?>">
<label style="font-weight: normal;">Enter Reason*</label><br>
<input type="text" name="reason" placeholder="call or letter or filed " id="ureason"><br>
<label style="font-weight: normal;">Update Date:*</label><br>
<input type="date" name="reason-date" placeholder="enter date here" id="udate">
<br><br>
<input type="submit" name="submit-form" id="actionid" value="Submit" style="background: #3596e0; ">
<?php
if(isset($error_msg))
{
echo "<span style='color:red; margin-bottom:10px;' class='pull-right'>$error_msg</span>";
exit();
}
else if(isset($msg))
{
echo "<span style='color:green;' class='pull-right'>$msg</span>";
exit();
}?>
</form>
</ul>
</div>
<?php
} ?>
</td>
</tr>
<?php
}
}
else {
echo "<h3> NO Related Table is Found Here </h3>";
}
?>
</tbody>
</table>
now problem is when i am filling at first update action form its going normal but when i am going for 2nd or 3rd action which have above row update function not filled its keep showing error of checking form column is empty , how to stop this , when i would fill in one row update column it should effect only that particular row, please someone help me , i stuck at this part for two days .
Upvotes: 0
Views: 79
Reputation: 397
This is happened because your form values not unique form so i have changed some form values:
<form role="banner" class="actionform" action="test.php" method="POST" name="sform" **onsubmit=" return checkvalue(<?php echo $c_id ?>**)">
<input type="text" name="reason" placeholder="call or letter or filed " id="ureason_<?php echo $c_id ?>">
<input type="date" name="reason-date" placeholder="enter date here" id="udate_<?php echo $c_id ?>">
Now all id's have unique value, And now javascript code would be:
<script type="text/javascript">
function checkvalue(id)
{
var id = id;
alert(id);
var ureason=document.getElementById('ureason_'+id).value;
var udate=document.getElementById('udate_'+id).value;
if (ureason=="")
{
alert("Status Field is blank ");
return false;
}
if (udate=="")
{
alert("Date Field is blank ");
return false;
}
}
</script>
And one more think you missed to update your table files
run this query at same time when your are inserting in update table:
$d_query=$db->query("INSERT INTO update_table (id, reason, update_date) VALUES ('$p_id', '$status', '$update_date')");
$d_query=$db->query("Update files set show_status='ok' where id='$p_id' ");
I hope this may help you.
Upvotes: 0
Reputation: 1990
Because of You are using INSERT
Method, use UPDATE
method for update.
For first time because of no data exist, your INSERT
method works as you want, but after that you have to use UPDATE
method for update.
For UPDATE see https://www.w3schools.com/sql/sql_update.asp
For INSERT see https://www.w3schools.com/sql/sql_insert.asp
Upvotes: 1