I have set up an update query which will update values entered into text fields on a while loop.Then for some reason only the last row data in the loop will be updated and the rest will stay the same.i know the issue .cause the the last attribute row overwrite the other in form so when update update only the last row
code
<?php
include'../config/connect.php';
$id = $_SESSION['agent'];
$sql = "Select * from nps where agent_id = '$id'";
$result = $cont->query($sql);
if ( $result->num_rows > 0 ){
?>
<form action="update.php" method="post">
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['phone']; ?> </td>
<td> <?php echo $row['survey_date']; ?> </td>
<td> <input type="hidden" name="id" value="<?php echo $row['agent_id']; ?>"> <?php echo $row['agent_id']; ?> </td>
<td> <?php echo $row['agent_name']; ?> </td>
<td> <?php echo $row['nps_rating']; ?> </td>
<td> <?php echo $row['sats']; ?> </td>
<td> <?php echo $row['agent_satisfaction']; ?> </td>
<td> <?php echo $row['ir']; ?></td>
<td><textarea name="comment" ><?php echo $row['comment']; ?></textarea></td>
</tr>
<?php
}
};
?>
</table>
<div><input type="submit" name="submit" > </div>
</form>
update.php file
<?php
session_start();
include'../config/connect.php';
$comment = $_POST['comment'];
$id = $_POST['id'];
if($_POST['submit']){
$sql = "UPDATE nps SET comment='$comment' WHERE id='$id' ";
};
if ($cont->query($sql) === true){
echo 'done';
}else{
echo 'notdone';
}
?>
First of all, you have two input names: id and comment. Once submitted, the form will be processed from start to finish, meaning the values that are actually processed in your PHP will be the last ones.
You will want to make these unique, and since you’re ID is hidden, it is less important. My suggestion would be to dynamically create names, such as comment_{id} and than in your PHP with the query to update, you go through all of your $_POST values, replace out the comment_ from the key, and execute a query to update the values.
You are also open to SQL injection and should take steps to prevent that.
You must have have inputs with unique names. I suggest to make a input name from row’s ID and attribute name, as for e.g. comment will be like ‘1-comment’. Then in the update script you need to iterate over the data and make the update query for each data. Cheers.
Related
The web screenshot shows that the time value is fetched from database column settime, I want to insert my own value into the set time column and update that single row but upon clicking update it doesn't update.
If I were to replace the update query with delete query as shown below it works and delete that particular row. My question is how can I use same method to update that particular column value based on id.
<?php
$id =(isset($_GET['id']) ? $_GET['id'] : null);
if($id!=null) {
$query="delete from activity where id=$id";
$result=mysql_query($query);
}
?>
Web
database
timer.php
<?php include 'header.php'; ?>
<div class="main" >
<table width="900px" cellpadding="5" cellspacing="5">
<tr>
<td style="width:10px"></td>
<td style="background:#F2F2F2; height:400px">
<?php
$settime =(isset($_POST['settime']) ? $_POST['settime'] : null);
$id =(isset($_GET['id']) ? $_GET['id'] : null);
if($id!=null) {
//$settime =(isset($_POST['settime']) ? $_POST['settime'] : null);
$query="update feedtime set settime='$settime' where id=$id";
$result=mysql_query($query);
}
?>
<table width="100%" class="tableStyle">
<tr style="font-weight:bold">
<td>NO.</td>
<td>User</td>
<td>Time</td>
<td>Time Left</td>
<td></td>
</tr>
<?php
$i=0;
$query="select * from feedtime";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)){
$i+=1;
echo '
<tr style="height:25px">
<td>'.$i.'</td>
<td>'.$row['user'].'</td>
<td><input id="timer" type="text" name="settime" style="width:200px;" value='. $row['settime'].' /></td>
<td>'. $row['timeleft'] .'</td>';echo '
<td align="center"><input type="button" value="Update" style="font-size:10px;width:80px;height:25px;padding:0;margin:0" onclick="location.href=\'timer.php?id='.$row['id'].'\' " /></td>
</tr>';
}
if($i==0)echo '
<tr style="height:25px">
<td colspan="6">Record not found</td>
</tr>'; ?>
</table><br>
<?php echo mysql_error(); ?>
</td>
</tr>
</table>
</div>
<?php include 'footer.php'; ?>
Ignoring the implementation details for a second (like using the MySQL extension), your main problem appears to be submitting (by POST) a single id / settime pair of values.
I would simply make a form per row. Something like this (very simplified)
<?php while($row = fetch_some_data()) : ?>
<tr>
<td>
<input form="form<?= $row['id'] ?>" type="text" name="settime" value="<?= $row['settime'] ?>" />
<td>
<td>
<form id="form<?= $row['id'] ?>" method="post" action="timer.php">
<button type="submit" name="id" value="<?= $row['id'] ?>">UPDATE</button>
</form>
</td>
</tr>
<?php endwhile ?>
Note the form attribute on the <input> and id attribute on the <form> are the same. This lets you tie the input to that form without it being a descendant element, allowing you to include all the required data without bastardising the HTML to get the form to wrap the table-row.
With this, pressing any "UPDATE" button will submit a POST request with data like
id=1&settime=12%3A08%3A00
which you can retrieve in your script via $_POST['id'] and $_POST['settime'].
For example (and using MySQLi)
if (isset($_POST['id'], $_POST['settime'])) {
$stmt = $conn->prepare('UPDATE `feedtime` SET `settime` = ? where `id` = ?');
$stmt->bind_param('si', $_POST['settime'], $_POST['id']);
$stmt->execute();
}
I have searched this forum for over a week trying to find a solution to my problem. I can display the correct information if I enter a value in a text box but if I select the option in a dropdown list it only shows the last record. Following is my latest code, can someone help me correct this code so the selected information is displayed? I've only included the segment affected.
<form id="selected" name="selected" method="get" action="" >
<select name="id">
<?php
$result = $conn->query("select * from agreement");
while ($row = $result->fetch_assoc()) {
unset($id, $name, $ctpd, $hsprogram);
$id = $row['ID'];
$name = $row['HSName'];
$ctpd = $row['CTPD'];
$hsprogram = $row['HSProgram'];
echo '<option value="'.$id.'"' .$selected . '>'.$name.' </option>';
}
?>
</select>
<tr>
<td> <?php echo $ctpd ?> </td>
<td> <?php echo $name ?> </td>
<td> <?php echo $hsprogram ?> </td>
</tr>
</table>
I am trying to insert multiple rows to a database table if check box is selected. But in my code when I am trying to insert, new rows are inserting based on check box selection. But no data is passing. I need some advice on below code to modify:
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$qry="select * from pi";
$result=mysql_query($qry);
?>
<form action="check.php" method="post">
<table>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr><td><input type=checkbox name=name[] value='".$row['id']."'>".$row['PI_NO']."</td><td>".$row['CUSTOMER_NAME']."</td><td>".$row['PI_ADDRESS']."</td></tr>";
}
?>
<input type="submit" value="save" id="submit">
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$name=$_POST['name'];
foreach($_POST['name'] as $x)
{
$qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
}
?>
Notes:
You forgot to bind the name of your checkbox using a single tick (')
You used variables in your query which you didn't defined and assigned value with yet
You only passed on the value of name, and did not include the Pi Address and Customer name. I'll be passing them by hidden input using <input type="hidden">.
I'll change the way you check your passed on form by looping them and check them using for() and if()
Use mysql_real_escape_string() before using them in your queries to prevent some of the SQL injections. But better if you consider using mysqli prepared statement rather than the deprecated mysql_*.
Is your post a single file? If it is, you must enclose your query using an isset() to prevent error upon loading the page.
You didn't close your <form>
Here's your corrected while loop:
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td>
<input type="checkbox" name="name[]" value="<?php echo $row['id']; ?>">
<?php echo $row["PI_NO"]; ?>
<!-- HERE IS THE START OF YOUR TWO HIDDEN INPUT -->
<input type="hidden" name="piaddress[]" value="<?php echo $row["PI_ADDRESS"]; ?>">
<input type="hidden" name="customer[]" value="<?php echo $row["CUSTOMER_NAME"]; ?>">
</td>
<td><?php echo $row['CUSTOMER_NAME']; ?></td>
<td><?php echo $row['PI_ADDRESS']; ?></td>
</tr>
<?php
} /* END OF WHILE LOOP */
?>
<input type="submit" value="save" id="submit">
</form> <!-- YOU DID NOT CLOSE YOUR FORM IN YOUR POST -->
And your query:
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */
for($x=0; $x<=$counter; $x++){
if(!empty($_POST["name"][$x])){
$PI_NO = mysql_real_escape_string($_POST["name"][$x]);
$CUSTOMER_NAME = mysql_real_escape_string($_POST["customer"][$x]);
$PI_ADDRESS = mysql_real_escape_string($_POST["piaddress"][$x]);
$qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
} /* END OF CHECKING THE CHECKBOX IF SELECTED */
} /* END OF FOR LOOP */
?>
Lots of little problems. And some big ones.
as $x){ .. $x is not being used so I assume you just loop for the number of checked boxes.
These have no values: '$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS'
Missing </form>
Not being used: $name=$_POST['name'];
<?php
echo '<form action="check.php" method="post"><table><tr><th>A</th><th>B</th><th>C</th></tr>';
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$sql = "select `id`,`PI_NO`, `CUSTOMER_NAME` ,`PI_ADDRESS` from `pi`";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo "<tr><td><input type=\"checkbox\" name=\"name[]\" value=/"$row[0]/"'>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>";
}
echo '<input type="submit" value="save" id="submit"></form>';
foreach($_POST['name'] as $x){
$sql="INSERT INTO pi (`PI_NO`, `CUSTOMER_NAME`, `PI_ADDRESS`)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($sql);
}
?>
I have a table where each row in the table has a button that allows you to delete that particular row from the database. Although somehow my form only submits the last row and no the one I selected. Please ignore the deprecated tags, well aware of the issues.
<?php
$result = mysql_query("SELECT * FROM booking");
while($row = mysql_fetch_array($result))
{
?>
<form class='table-form' id='form' method='post'>
<tr>
<input id="bookid" type="hidden" name="bookid" value="<?php echo ($row['booking_id']); ?>" />
<td>
<?php echo ($row['booking_id']);?>
</td>
<td>
<?php echo ($row['user_id']); ?>
</td>
<td>
<?php echo ($row['event_id']); ?>
</td>
<td>
<?php echo ($row['payment_type']); ?>
</td>
<td>
<?php echo ($row['booking_date']); ?>
</td>
<center><button type="submit" id="submit" name="submit">Cancel</button><center>
<td>
</td>
</tr>
<?php
}
?>
</form>
</table>
</div>
<?php
if (isset($_POST['bookid'])){
$id = ($_POST['bookid']);
$result = mysql_query("DELETE FROM booking
WHERE booking_id = '$id'");
}
?>
If you preffer programing this way, move </form> above
<?php
}
?>
(to cycle body).
Your code needs a lot of clean up, style cleaning and html validation. :-P
I have a list of checkboxes created by a while loop but when the form is submitted the checkboxes are cleared. I have tried to use the unique id of the value in the record by putting it in a hidden field and then using it in an if query before marking the checkbox as checked but it does nothing
<?php
while($row = mysqli_fetch_array($result)){
$posted = $row['auditID'];
?>
<tr class="hover">
<td width="180"><? echo $row['auditName']; ?>
<input type="hidden" name="audit_id_confirm" value="<? echo $row['auditID'];?>">
</td>
<td width="33"><input type="checkbox" name="audit_selected[]"
value="<? echo $row['auditID'];?>"
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked"; }?>>
</td>
</tr>
<?php
}
?>
I sorted it by checking id=f the id number was in the arrays that had been posted.
<?php
$selected = $_POST['audit_selected'];
while($row = mysqli_fetch_array($result)){
$audit = $row['auditID'];
if(in_array($audit, $selected)) {
$check="checked='checked'";
}else{
$check = '';
} ?>
<tr class="hover">
<td width="180">
<?php echo $row['auditName'].' '.$num_audited; ?></td><td width="33"> <input type="checkbox" name="audit_selected[]" value="<?php echo $row['auditID'];?>" <?php echo $check; ?> >
</td>
</tr>
<?php }
?>
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked"; }?>>
Replace above line with this:
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked='checked'"; }?>>
You are not properly providing checked attribute to the Html entity.
One of the information you need to store in the table, is the state of the checkbox.
So, you can create a new column in your table, call it "checkboxstate".
Now inside your code use the following code to insert the checkbox:
<input type="checkbox" name="<?php echo($posted); ?>">
Thus far we have created several checkboxes whom names are the same as $posted, in other words each checkbox will have the name of its row's "$posted" value.
Now the only thing that is left is submitting the form and storing the checkbox info, which will return either TRUE or FALSE, and we will save that in our table, in the "checkboxstate" column.
So now in our table we will have a bunch of rows with several columns, one of the columns will be "checkboxstate" whom value will be either TRUE or FALSE.
At this point we have pretty much solved the problem, all that is left is to insert a simple if to either show a checked checkbox or an unchecked one.
<?php
while($row = mysqli_fetch_array($result)){
$posted = $row['auditID'];
$cbs = $row['checkboxstate'];
?>
<tr class="hover">
<td width="180"><? echo $row['auditName']; ?></td>
<td width="33">
<?php if($cbs == "true") echo "<input type='checkbox' name='$posted' checked='checked'>"; else echo "<input type='checkbox' name='$posted'>";
</td>
</tr>
<?php
}
?>