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
Related
The Scenario
I have a scoring card that is populated from another table in the MTSQL with an unknown amount of players. each week the players needs to be scored and those scores needs to be input into the mySQL.
The dynamic html table create for the Mysql table call member.
<form method="post" action="scores-add" id="member-add-form" enctype="multipart/form-data">
<div class="table-responsive">
<table id="data-table-1" class="table table-striped table-bordered">
<thead>
<tr>
<th class="align-middle">Name</th>
<th class="align-middle">Team</th>
<th class="align-middle">Week</th>
<th class="align-middle">Score 1</th>
<th class="align-middle">Score 2</th>
<th class="align-middle">Score 3</th>
<th class="align-middle">Score 4</th>
<th class="align-middle">Score 5</th>
<th class="align-middle">Score 6</th>
<th class="align-middle">Score 7</th>
<th class="align-middle">Score 8</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM member ORDER BY member_name ASC";
$result = mysqli_query($db,$sql) or die("Database access failed: " . mysqli_error());
while ($team = mysqli_fetch_assoc($result)) {
?>
<tr >
<td class="align-middle"><input type="text" name="scoring_1" value="<?php echo $team["member_name"]; ?>" readonly hidden> <?php echo $team["member_name"]; ?></td>
<td class="align-middle"><input type="text" name="scoring_2" value="<?php echo $team["member_team"]; ?>" readonly hidden> <?php echo $team["member_team"]; ?></td>
<td class="align-middle"><input type="text" name="scoring_3" value="" readonly hidden> </td>
<td class="align-middle"><input type="checkbox" name="scoring_4" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_5" value="10"></td>
<td class="align-middle"><input type="checkbox" name="scoring_6" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_7" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_8" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_9" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_10" value="20"></td>
<td class="align-middle"><input type="checkbox" name="scoring_11" value="50"></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="form-row mb-3">
<button type="submit" class="btn btn-primary btn-block" name="score_add_btn">Add Score</button>
</div>
</form>
The function to add the table to the SQL
/ call the team_edit() function if team_edit_btn is clicked
if (isset($_POST['score_add_btn'])) {
score_add();
}
// LOGIN USER
function score_add(){
global $db, $errors, $member_id, $member_name_edit, $member_team_edit, $members_view_url;
// grap form values
$scoring_1 = $_POST['scoring_1'];
$scoring_2 = $_POST['scoring_2'];
$scoring_3 = $_POST['scoring_3'];
$scoring_4 = $_POST['scoring_4'];
$scoring_5 = $_POST['scoring_5'];
$scoring_6 = $_POST['scoring_6'];
$scoring_7 = $_POST['scoring_7'];
$scoring_8 = $_POST['scoring_8'];
$scoring_9 = $_POST['scoring_9'];
$scoring_10 = $_POST['scoring_10'];
$scoring_11 = $_POST['scoring_11'];
// make sure form is filled properly
if (empty($scoring_1)) {
array_push($errors, "Name is required");
}
if (count($errors) == 0) {
$query = "INSERT INTO scoring (scoring_1, scoring_2, scoring_3, scoring_4, scoring_5, scoring_6, scoring_7, scoring_8, scoring_9, scoring_10, scoring_11 )
VALUES ('$scoring_1', '$scoring_2', '$scoring_3', '$scoring_4', '$scoring_5', '$scoring_6', '$scoring_7', '$scoring_8', '$scoring_9', '$scoring_10', '$scoring_11')";
mysqli_query($db, $query);
$_SESSION['success'] = "<h4>Done</h4>";
header("location: $leaderboard_menmber_url");
}else {
$_SESSION['failure'] = "Error updating record: " . $db->error;
header("location: $leaderboard_menmber_url");
}
$db->close();
}
The Problem
The function only adds the last line from the dynamic html table to the database.
The desired outcome.
Each entry in the dynamic HTML table needs to be added to the MySQL as its own entry.
<input type="text" name="scoring_1" value="<?php echo $team["member_name"]; ?>
here you set your variable name to scoring_1 but the next row of table will override that (Instead the browser may send the data, but php will create a variable scoring_1 and assign one of the given values to them. To solve this problem set the name to scoring_1[]
<input type="text" name="scoring_1[]" value="<?php echo $team["member_name"]; ?>
This say php to make an array and store the values there. Futher you can inprove your code when you use something like the team id as row index and used a fixed number as column in a 2 dimensional array
<input type="text" name="scoring[<?php echo $team["id"];>][1]" value="<?php echo $team["member_name"]; ?>
Hello so i'm learning php and basiclly i want that any field that has been already added to the database to be "editable"
Let me make it more clear with an example
My from looks like this (ver basic)
Firstname [input field]
Lastname [input field]
ZIP Code [input field]
send button
<form method="post" action="">
<table>
<tr>
<td>Vorname</td>
<td><input type="text" name="firstname"></td>
</tr>
<tr>
<td>Nachname</td>
<td><input type="text" name="lastname"></td>
</tr>
<tr>
<td>PLZ</td>
<td><input type="number" name="plz"></td>
</tr>
</table>
<button type="submit" name="send">Send</button>
</form>
When the send button is pressed the output is beeing shown in a table below like so
|---------------------|------------------|------------------|
| Firstname | Lastname | Zip Code |
|---------------------|------------------|------------------|
| Tomas | Möller | 123123 |
|---------------------|------------------|------------------|
and next to those to rows i still have these two (which didn't fit)
|---------------------|------------------|
| Update | Delete |
|---------------------|------------------|
| update | delete |
|---------------------|------------------|
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">PLZ</th>
<th scope="col">Delete</th>
<th scope="col">Update</th>
</tr>
</thead>
<tbody>
<?php
/**
* #var $contact ContactDto
*/
$contacts = $contactRepository->findAll();
foreach ($contacts as $contact) {
?>
<tr>
<th scope="row"><?php echo $contact->getId()?></th>
<td><?php echo $contact->getFirstname() ?></td>
<td><?php echo $contact->getLastname() ?></td>
<td><?php echo $contact->getPlz() ?></td>
<td><a href=delete.php?id=<?php echo (int)$contact->getId()?> >Delete</a></td>
<td><a href=update.php?id=<?php echo (int)$contact->getId()?> >Update</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
So basicaly each row has this own update and delte button.
Here is my problem..im not sure how to do this the right way.
I want when the update is clicked, the value from to fields "go back" to the top field and when send is pressed again the values are updated.
Here is my query
public function update(ContactDto $contactDto) {
$stmt = $this->pdo->prepare("UPDATE contacts SET firstname=:firstname,
lastname=:lastname,
plz=:plz
WHERE id=:id");
$stmt->bindValue(':firstname', $contactDto->getFirstname(), PDO::PARAM_STR);
$stmt->bindValue(':lastname', $contactDto->getLastname(), PDO::PARAM_STR);
$stmt->bindValue(':plz', $contactDto->getPlz(), PDO::PARAM_INT);
$stmt->bindValue(':id', $contactDto->getId(), PDO::PARAM_INT);
$stmt->execute();
}
I did try and make a new update.php file where the data is send, and the fields get filled like so (but that didnt quite work)
this is my update.php file
$contactRepository = new ContactRepository(
DatabaseConnection::getConnection()
);
$contactDto = new ContactDto();
$contactDto->setId($_GET['id']);
$contactDto->setFirstName($_POST['firstname']);
$contactDto->setLastName($_POST['lastname']);
$contactDto->setPlz($_POST['plz']);
$contactRepository->update($contactDto);
?>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $contactDto->getId(); ?>" />
<?PHP
?>
<table>
<tr>
<th>First Name: </th>
<td>
<input type="text" id="vorname" name="firstname" placeholder="Dein Vorname" size="35" value="<?php echo $contactDto->getFirstName(); ?>">
</td>
</tr>
<tr>
<th>Last Name: </th>
<td>
<input type="text" id="nachname" name="lastname" placeholder="Dein Nachname" size="35" value="<?php echo $contactDto->getLastName(); ?>">
</td>
</tr>
</table>
<input type="submit" name="save" value="Update" >
Which leaves me with..
>
Notice: Undefined index: firstname in C:\xampp\htdocs\test\update.php on line >14
Notice: Undefined index: lastname in C:\xampp\htdocs\test\update.php on line >15
Notice: Undefined index: plz in C:\xampp\htdocs\test\update.php on >line 16
About the update.php page: the error messages you're getting are saying that 'firstname', 'lastname' and 'plz' are not defined.
This is because you tried to define them using POST:
$contactDto->setFirstName($_POST['firstname']);
$contactDto->setLastName($_POST['lastname']);
$contactDto->setPlz($_POST['plz']);
But in fact, the values of firstname, lastname and plz were not posted to this page. POST is only 'activated' when you click a submit button in a form where you wrote
<form method="POST"...
But you didn't get on the update.php page via a form, but via a hyperlink in this table you made:
foreach ($contacts as $contact) {
?>
<tr>
<th scope="row"><?php echo $contact->getId()?></th>
<td><?php echo $contact->getFirstname() ?></td>
<td><?php echo $contact->getLastname() ?></td>
<td><?php echo $contact->getPlz() ?></td>
<td><a href=delete.php?id=<?php echo (int)$contact->getId()?> >Delete</a></td>
<td><a href=update.php?id=<?php echo (int)$contact->getId()?> >Update</a></td>
</tr>
<?php
}
?>
This is not wrong tho! This table is very good.
But on the update.php page, you can't define 'firstname', 'lastname' and 'plz' using $_POST['....'].
The only thing on the update.php page that IS defined, is 'id'. This is because 'id' was the only thing that you sent to this page using the get method. Again: this is the right way to do it so don't change that.
**
What you DO need to change, is this:
**
change your code:
$contactDto = new ContactDto();
$contactDto->setId($_GET['id']);
$contactDto->setFirstName($_POST['firstname']);
$contactDto->setLastName($_POST['lastname']);
$contactDto->setPlz($_POST['plz']);
To this:
$contactDto = new ContactDto();
$contactDto->setId($_GET['id']);
So only 'id' is defined.
Now define 'firstname', 'lastname' and 'plz' by getting those values from your database, using a query.
Like this:
$getstuff = "SELECT * FROM yourtablename WHERE id = $thevariablethatcontainstheid";
(I guess the variable that contains the 'id' is $contactDto->setId(); ??)
Then execute the query on the next line:
$results = mysqli_query($yourconnectingthing,$getstuff);
And write this below:
$row = $results->fetch_assoc();
And then you can define 'firstname', 'lastname' and 'plz'! By writing this:
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$plz = $row['plz'];
Now the last thing you need to change is the values in the editing form.
This was your code:
<tr>
<th>First Name: </th>
<td>
<input type="text" id="vorname" name="firstname" placeholder="Dein Vorname" size="35" value="<?php echo $contactDto->getFirstName(); ?>">
</td>
</tr>
change it to:
<tr>
<th>First Name: </th>
<td>
<input type="text" id="vorname" name="firstname" placeholder="Dein Vorname" size="35" value="<?php echo $firstname ?>">
</td>
</tr>
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 am fairly new to PHP and SQL.
I want to be able to assign a task to a certain user and the update that row in the database with the user's name assigned to that task.
Here is my code:
<table border="0" width="1100px" style= "font-size: 12px" >
<thead>
<tr valign="top" align="left">
<th height="20"></th>
<th height="20">Customer</th>
<th>Vehicle</th>
<th>Appt Time</th>
<th>Notes</th>
<th>Assign</th>
<th>Action</th>
<tr><td valign="top" colspan="6"><hr><br></td></tr>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr valign='center'>
<td width='50'><b>{$row['id']}</td>
<td width='220' height='70'><b>{$row['firstname']}
</td>
<td width='240'><b>{$row['car']}</b> <br>{$row['reg']}</td>
<td width='170'>Monday<br>24 September<br>17:00</td>
<td width='240'>{$row['notes']}<br><b>Status:</td>
<td width='240'>
<form action='bookings.php' method='post'>
<select style='width:90px' class='reg' name='assign'required>
<option value=''></option>
<option value='User1'>User1</option>
<option value='User2'>User2</option>
<option value='User3'>User3</option>
<option value='User4'>User4</option>
<option value='User5'>User5</option>
</select><input type='submit' value='>' class='assignButton'/></form>
</td><td>
<button class='myButton'>Edit</button>
</td>
<tr><td colspan='6'><hr class='hrTitle'></td></tr>
</tr>\n";
}
?>
</tbody>
</table>
As you can see, I have a number of users that can be selected, I want to be able to assign that task to a user from the select list.
Any help is much appreciated.
Try the following steps.
Give a name to your submit button.
<input type='submit' value='>' name='submit' class='assignButton'>
Add a hidden input field to your form. Give it the value of the id of the current row. Give it a name. I chose id.
This is very important so you can know which customer to edit. Please double check the value that you will set. From your code, I see it is $row['id'].
<input type="hidden" name="id" value='$row["id"]''>
In your PHP file bookings.php (assuming you have a connection to your database), process the submitted form.
if(isset($_POST["submit"])){ //checks if the form was submitted
$id = $_POST["id"]; //id of the customer
$assign = $_POST["assign"]; //your selected value
$query = "UPDATE table SET columnToModify = '$assign' WHERE id = '$id'";
$result = $connection->query($query); //run the query
}
Hope it helps.
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>