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();
}
Related
I have a table which fetch the data from database except the issued column. The number of row in that table is not fixed. There is an update button and which update the record. After entering the value in issued column i update that record.My data is not updating in the table . I have doubt how to give name to to the td element of the table.Code for table is
<form role="form" method="post" class="form-inline">
<table >
<thead>
<tr >
<th width="50px" >Sl.No.</th>
<th style="display:none;" >Txn ID.</th>
<th width="175px">Stationery Type</th>
<th width="73px">REQUESTED</th>
<th width="73px">ISSUED</th>
</tr>
</thead>
<tbody>
<tr class="table-row-2" >
<td> <?php echo htmlentities($cnt);?></td>
<td style="display:none;"><?php echo htmlentities($result->txnid);?></td>
<td><?php echo htmlentities($result->stationerytype);?></td>
<td><?php echo htmlentities($result->REQUESTED);?></td>
<td><input type="number" name="stationeryqtyissued" > </td>
</TABLE>
<input type="submit" name="submit" value="Submit" class="btn pull-right" style="margin-left:375px; margin-bottom:30px;">
</form>
Mysql code to update database is
if(isset($_POST['submit']))
{
$samplecount=$_GET["total"];
for($i=0;$i<$samplecount;$i++){
$status="issued";
$txnid=$_POST['txnid'];
$stationeryqtyissued=$_POST['stationeryqtyissued'];
$sql="update tblstationerystock set status=:status,stationeryqtyissued=:stationeryqtyissued where txnid=:txnid";
$query = $dbh->prepare($sql);
$query->bindParam(':status',$status,PDO::PARAM_STR);
$query->bindParam(':stationeryqtyissued',$stationeryqtyissued[$i],PDO::PARAM_STR);
$query->bindParam(':txnid',$txnid[$i],PDO::PARAM_STR);
$query->execute();
$_SESSION['msg']="Stationery Added successfully";
Add the following line to you form
<input type="hidden" id="txnid" name="txnid" value="<?php echo htmlentities($result->txnid);?>">
If you want to send an array
<input type="hidden" name="txnid[]" value="<?php echo htmlentities($result->txnid);?>">
then you would have more than one row for that to work
and you can then
get it by
$txnid=$_POST['txnid'];
But that is no array, so that should be valid
$query->bindParam(':stationeryqtyissued',$stationeryqtyissued,PDO::PARAM_STR);
$query->bindParam(':txnid',$txnid,PDO::PARAM_STR);
So i am unclear why you are running a loop for the update over $total that i also don't see in your form.
<td style="display:none;"><?php echo htmlentities($result->txnid);?></td>
Would not send data to your php with POST or GET
What is wrong with this code? I tried it several times but my DB is not updating after submitting.. I have a table with in the last column check-boxes. The check-boxes show correctly the value from the DB, but I also want the option to update the DB with the check-boxes. So if the value is 1 then the check-box is checked, but I want to be able to uncheck it (or check another that's unchecked) and click on the submit button below to change the value in the DB.
$sql = "SELECT * FROM registered ORDER BY datum" ;
$myData=mysql_query($sql) ;
?>
<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px"><b>naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>
<?php
if(isset($_POST['submit'])){
$betaald = $_POST['betaald'];
mysql_query($con,"UPDATE registered SET betaald='$betaald'");
header("Location: index.php");
} ?>
<?php
while($record = mysql_fetch_array($myData))
{
?>
<tr>
<td> <?php echo $record['naam'] ?></td>
<form method="post" action="<?php echo $PHP_SELF;?>">
<td> <input type='checkbox' name='betaald' id='betaald' value='1' <?php echo ($record['betaald']==1 ? 'checked' : '')?>> </td>
</tr>
<?php
}
mysql_close($con);
?>
</table></p>
<input style="border:1px solid black" name="submit" type="submit" value="Save changes">
</form>
You passed wrong parameters to mysql_query().
The syntax is mysql_query($query); The second parameter is optional
Remove $con from the function.
According to the manual of php.net , the first param is query ,and the second is link identifier(as your $con) which is optional
mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )
I am new to PHP(loving it already)
I have a form that looks up a table that sends 'golf hole' info back and allows a golfer to input their score of the hole. Problem I have is that I can present the first hole by looking up the hole_detail table but then cant figure out how loop through the table for hole 2, 3.....18 when the form is submitted. I have searched stackoverflow but cant find anything that specific about it. I have tried an if statement, if (isset($_POST['Submit'])) to try increment the $hole_id. Am I completely going about it the wrong way? Thanks in advance.
<?php
include ('../scripts/dbconfig.php');
# get the most recent course name:
$get_course_name = mysql_query("SELECT course_name FROM comp ORDER BY PID DESC LIMIT 1");
$show_course_name = mysql_fetch_array($get_course_name);
if (isset($_POST['Submit'])) {
$hole_id =1;
else {
$hole_id = $hole_id + 1;
}
}
# get the hole yardage and SI from most recent selected golf course:
$get_course_detail = mysql_query("SELECT * FROM `course_detail` WHERE course_name = '". $show_course_name['course_name'] . "'");
$show_course_detail = mysql_fetch_array($get_course_detail);
$get_hole_detail = mysql_query("SELECT * FROM `course_detail`,`phoenix_hole` WHERE Course_ID = 6 AND hole_id = $hole_id");
$show_hole_detail = mysql_fetch_array($get_hole_detail);
?>
</head>
<body>
<table width="300" cellspacing="0" cellpadding="0">
<tr>
<td width="40"><?php echo $show_course_name['course_name'];?></td>
</tr>
<tr>
<td width="20">HOLE <?php echo $show_hole_detail['hole_id']?></td>
<td width="5"> PAR <?php echo $show_hole_detail['hole_par'];?></td>
</tr>
<tr>
<td width="20">Yards</td>
<td width="20">S.I</td>
</tr>
<tr>
<td bgcolor="yellow"><?php echo $show_hole_detail['yellow_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td border="1px" bgcolor="white"><?php echo $show_hole_detail['white_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td bgcolor="red"><?php echo $show_hole_detail['red_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
</table>
</p>
<form id="game_form" name="game_form" method="post" action="game_form.php">
<table width="300" border="0" align="left" cellpadding="2" cellspacing="0">
<tr>
<td><b>Hole Shots</b></td>
<td><input name="hole_shots" type="text" class="textfield" id="hole_shots" maxlength="2" size="3" ></td>
<td><b>Putts</b></td>
<td><input name="putts" type="text" class="textfield" id="putts" maxlength="2" size="3"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Next Hole" align="center" /></td>
</tr>
</table>
</form>
</body>
</html>
Or you can use a hidden field that keeps the hole number and you can increment it from php.
$hole_id, in this scenario, will always be 1, because when a user clicks the Submit button, $_POST['Submit'] will always have a value. What you should do instead is have $_POST['Submit'] contain the value of $hole + 1. PHP is not going to "remember" what $hole_id was last time around; it's up to you to remind it. As soon as a request is sent to the browser--unless you're using sessions--PHP forgets everything about that request (HTTP is "stateless").
<?php
if (isset($_POST['Submit'])) {
$hole_id = (int)$_POST['Submit'];
} else {
$hole_id = 1;
}
# other code here
?>
You are on hole #<?php echo $hole_id; ?>.
<form>
<!-- form stuff here -->
<button type="submit" name="Submit" value="<?php echo $hole_id + 1; ?>">Next hole</button>
</form>
I want to delete multiple rows from my MYSQL database table. I have created this file to select various links and delete them using checkboxes.
This doesn't seem to delete any row. My data is populated in the table. I guess the problem is with my PHP code. Please check the below code and guide me to get out from this...
<html>
<head>
<title>Links Page</title>
</head>
<body>
<h2>Choose and delete selected links.</h2>
<?php
$dbc = mysqli_connect('localhost','root','admin','sample') or die('Error connecting to MySQL server');
$query = "select * from links ORDER BY link_id";
$result = mysqli_query($dbc,$query) or die('Error querying database');
$count=mysqli_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="3" bgcolor="#FFFFFF">
<strong>Delete multiple links</strong>
</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Link ID</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Link Name</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Link URL</strong>
</td>
</tr>
<?php
while ($row=mysqli_fetch_array($result)) {
?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox" type="checkbox" value="
<?php echo $row['link_id']; ?>">
</td>
<td bgcolor="#FFFFFF"> <?php echo $row['link_id']; ?> </td>
<td bgcolor="#FFFFFF"> <?php echo $row['link_name']; ?> </td>
<td bgcolor="#FFFFFF"> <?php echo $row['link_url']; ?> </td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
// Check if delete button active, start this
if(isset($_POST['delete']))
{
$checkbox = $_POST['checkbox'];
for($i=0; $i<count($checkbox); $i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM links WHERE link_id='$del_id'";
$result = mysqli_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo '<meta http-equiv="refresh" content="0;URL=view_links.php">';
}
}
mysqli_close($dbc);
?>
</body>
</html>
You should treat it as an array like this,
<input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>">
Then only, you can take its count and loop it for deletion.
You also need to pass the database connection to the query.
$result = mysqli_query($dbc, $sql);
Yours did not include it:
$result = mysqli_query($sql);
Use array notation like name="checkbox[]" in your input element. This will give you $_POST['checkbox'] as array. In the query you can utilize it as
$sql = "DELETE FROM links WHERE link_id in ";
$sql.= "('".implode("','",array_values($_POST['checkbox']))."')";
Thats one single query to delete them all.
Note: You need to escape the values passed in $_POST['checkbox'] with mysql_real_escape_string or similar to prevent SQL Injection.
<?php $sql = "SELECT * FROM guest_book";
$res = mysql_query($sql);
if (mysql_num_rows($res)) {
$query = mysql_query("SELECT * FROM guest_book ORDER BY id");
$i=1;
while($row = mysql_fetch_assoc($query)){
?>
<input type="checkbox" name="checkboxstatus[<?php echo $i; ?>]" value="<?php echo $row['id']; ?>" />
<?php $i++; }} ?>
<input type="submit" value="Delete" name="Delete" />
if($_REQUEST['Delete'] != '')
{
if(!empty($_REQUEST['checkboxstatus'])) {
$checked_values = $_REQUEST['checkboxstatus'];
foreach($checked_values as $val) {
$sqldel = "DELETE from guest_book WHERE id = '$val'";
mysql_query($sqldel);
}
}
}
Delete Multiple checkbox using PHP Code
<input type="checkbox" name="chkbox[] value=".$row[0]."/>
<input type="submit" name="delete" value="delete"/>
<?php
if(isset($_POST['delete']))
{
$cnt=array();
$cnt=count($_POST['chkbox']);
for($i=0;$i<$cnt;$i++)
{
$del_id=$_POST['chkbox'][$i];
$query="delete from $tablename where Id=".$del_id;
mysql_query($query);
}
}
Something that sometimes crops up you may/maynot be aware of
Won't always be picked up by by $_POST['delete'] when using IE. Firefox and chrome should work fine though. I use a seperate isntead which solves the problem for IE
As for your not deleting in your code above you appear to be echoing out 2x sets of check boxes both pulling the same data? Is this just a copy + paste mistake or is this actually how your code is?
If its how your code is that'll be the problem as the user could be ticking one checkbox array item but the other one will be unchecked so the php code for delete is getting confused. Either rename the 2nd check box or delete that block of html surely you don't need to display the same list twice ?
$deleted = $_POST['checkbox'];
$sql = "DELETE FROM $tbl_name WHERE id IN (".implode(",", $deleted ) . ")";
Rather noobish question but I have tried searching and just cannot find a solution. My problem is I have a table called Off, which stores all of the staffs holidays. However before a staff member may have that day off it has to be authorised. I have a query which gets all of the unauthorised holidays and I display them in a html table. The problem is that I need 2 radio buttons per record. one for authorise and one for deny. The radio buttons are displayed but when going through the records only one of the radio buttons is selectable. Is it possible to use radio buttons in this way?
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Apollo/dbc.php";
include_once($path);
$rs_results = mysql_query("SELECT * FROM off WHERE IsItAuthorised='0' and isitsick='0' ORDER BY DayOff");
?>
<html>
<head>
<title>Administration Main Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php
$limit = count($OffID);
if (isset($_POST['submit'])) {
//Assign each array to a variable
$id = $_POST['OffID'];
$answer = $_POST['radio'];
$limit = count($OffID);
$values = array(); // initialize an empty array to hold the values
for($k=0;$k<$limit;$k++){
$msg[] = "$limit New KPI's Added";
$query = "UPDATE Off SET IsItAuthorised = '{$answer[$k]}' WHERE OffID = '{$OffID[$k]}'";
}
$Event = "INSERT INTO events (UserName, Event ) VALUES ('$_SESSION[user_name]', 'Entered New KPI' )";
if (!mysql_query($query,$link)){
die('Error: ' . mysql_error());
} else {
mysql_query($Event);
echo "<div class=\"msg\">" . $msg[0] . "</div>";
}
}
?>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%" valign="top"><?php
?>
</td>
<td width="74%" valign="top" style="padding: 10px;">
<p><?php
if(!empty($msg)) {
echo $msg[0];
}
?></p>
<p>
<?php
$cond = '';
$sql = "select * from off ";
$rs_total = mysql_query($sql) or die(mysql_error());
$total = mysql_num_rows($rs_total);
?>
<p>
<form name "searchform" action="/Apollo/Admin/HolidayRequests" method="post">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="0">
<tr class="mytables">
<td width="4%"><font color="white"><strong>ID</font></strong></td>
<td width="4%"> <font color="white"><strong>Staff Member</font></strong></td>
<td width="10%"><font color="white"><strong>Day Off</font></strong></div></td>
<td width="10%"><font color="white"><strong>Is It Authorized</font></strong></div></td>
<td width="15%"> </td>
</tr>
<tr>
<td> </td>
<td width="10%"> </td>
<td width="17%"><div align="center"></div></td>
<td> </td>
</tr>
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input type="" name="id[]" id="id[]" size="4" value="<?php echo $rrows['OffID'];?>" /></td>
<td><?php echo $rrows['StaffMember']; ?></td>
<td><?php echo date('d/m/Y', strtotime($rrows['DayOff']));?></div></td>
<td> <span id="approve<?php echo $rrows['id']; ?>">
<?php if(!$rrows['IsItAuthorised']) { echo "Pending"; } else {echo "Authorized"; }?>
</span> </td>
<td>
<input type="radio" name="radio[0]" id="radio[0]" value="1" />Approve
<input type="radio" name="radio[1]" id="radio[1]" value="0" />Deny
</td>
</tr>
<?php } ?>
</table>
<input name="submit" type="submit" id="submit" value="Submit">
</form>
</p>
<?php
?>
<p> </p>
<p> </p>
<p> </p>
<p> </p></td>
<td width="12%"> </td>
</tr>
</table>
</body>
</html>
Your problem is to do with your use of the "name" attribute - this attribute is used in a special way on radio buttons.
When you click on a radio button, all the other radio buttons with the same name are deselected - in your case, you will have half of the radio buttons on the page in one group and half in another, which would probably explain why you can only select one. I'd imagine you can actually select two at the same time, if you click on the right radio buttons.
To fix this you'd need to have some kind of count for the while loop, ie:
$resultNumber = 0;
while ($rrows = mysql_fetch_array($rs_results)) {
Then you'd need to use this number in the name (and also id) of the radio buttons -
<input type="radio" name="radio<?php echo $resultNumber; ?>" id="radio<?php echo $resultNumber; ?>" value="1" />
Then simply increment the $resultNumber at the end of each iteration -
<?php
$resultNumber++;
}
?>
Alternatively, you could use the primary key of the row from the database table you're querying to distinguish groups, but not knowing the table structure I couldn't give sample code for that.
Further reading on radio buttons (also a source for the name attribute problem) :
http://www.echoecho.com/htmlforms10.htm
Instead of radio buttons, I used checkboxes, which I looped through. Each checkbox set a value to a different column. Should have been obvious to me to begin with.