I have created a while loop
while($row = mysql_fetch_Array($result)) {
echo '<tr class="record"> <td></td><td>'
. $row['company'] .'</td><td>'
. $row['project'].'</td><td>'
. $row['assignee']. '</td><td>'
. $row['current_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['start_date'])).'</td><td>' ..'</td><td>'
. $row['next_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['next_date']))
.'</td></tr>' ;
}
This displays correctly but my question is:
How can the user select an individual row so he/she can edit it. I was hoping that the user can select which row they wanted and they would be taken to another screen where they can make the changes. I can figure out how to do the part where I can update the data. But it is the selecting the row and collecting correct data I dont understand how to do.
Embed an <a href='update.php?id=> somewhere in there which carries the record id to the update script as a parameter in the query string:
while($row = mysql_fetch_Array($result)) {
echo '<tr class="record"> <td></td><td>'
// Insert a link with an id parameter.
// Use whatever column value uniquely identifies your row
. ' Edit this! '
// etc...
// etc...
// etc...
.'</td></tr>' ;
}
And retrieve it on the update script via:
// Assuming an integer id was passed...
// if it is some other string value, escape and quote it accordingly.
$id = intval($_GET['id']);
And you will want to check that the user making the edit has permission to edit that record. In other words, verify that the record is owned by the user is logged in, since anyone could technically visit the URL with any id, whether or not it belongs to them. As in:
UPDATE yourtable SET col1 = 'whatever', col2 = 'whatever' WHERE id = $id AND user_id = 'logged_in_user'
Create some sort of primary key (a new field that will be different for each row/entry) in your database. Example: an ID field
Create a separate page for updating that accepts a GET parameter id. Ex: update.php
Validate and sanitize the id parameter (ensure it is a valid id, not an SQL injection attempt, and the user has the privileges to edit this id)
Load info from database for that id. Ex: SELECT fields FROM table WHERE id = $id
Display a form so user can edit the information.
Validate form input and update the table. Ex: UPDATE table SET field1=newVal WHERE id = $ID
First of all, you need to create a primary key in the table, suppose primary_key in this case, so as to access the particular row having a definite and unique ID.
Secondly, add one more column to your table:
while($row = mysql_fetch_Array($result)){
echo '<tr class="record"> <td></td><td>'
. $row['company'] .'</td><td>'
. $row['project'].'</td><td>'
. $row['assignee']. '</td><td>'
. $row['current_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['start_date'])).'</td><td>' ..'</td><td>'
. $row['next_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['next_date']))
.'</td>' ;
?>
<td>
<form action='update.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['primary_key']; ?>">
<input type="submit" value="Edit">
</form>
</td>
<?php
echo "</tr>";
}
?>
And now you know which row's details you need to display in the form at update.php
The code on update.php goes like:
<?php
$id = $_POST['id'];
$result = mysql_query("SELECT * FROM table WHERE primary_key = '$id'");
$info = mysql_fetch_array($result);
?>
<form action='target.php' method='post'>
Company: <input type="text" name="company" value="<?php echo $info['company']; ?>">
Project: <input type="text" name="project" value="<?php echo $info['project']; ?>">
.
.
<input type="hidden" name="id" value="<?php echo $info['primary_key']; ?>">
<input type="submit" value="Save Changes">
</form>
And then on target.php all you have to do is use the command:
mysql_query("UPDATE table SET company='".$_POST['company']."' AND project='".$_POST['project']."' WHERE primary_key='".$_POST['id']."');
Related
First of all, I'm aware that the title is not very clear, it's because I'm having trouble put the issue I have into words, so if anyone understands what I'm about to describe, please suggest a new title for me to edit.
Now for the main problem, I will do my best to explain it :
The user ( a teacher ) logs in, he then chooses one of the classes he teaches, the website displays a list of the students that are in that class as a table.
the table contains 3 columns : Name , lastName, Present.
Present is a column containing 2 radio buttons : Absent & Present.
I want the teacher to choose one of them for every student, then press "Submit".
The submit will result in the insertion of data into a table that contains the ID of the student that was absent, the current date, and the ID of the class. Here is the code for showing the table :
<?php if (isset($_GET['choice'])) {
$choice = $_GET['choice'];
$showquery = $conn->prepare('select Nom,Prenom,etudiant_ID from etudiant where classe_id = (select classe_id from classe where abreviation= ?)');
$showquery->execute(array($choice));
echo $choice;
echo ("<form>");
echo "<table border='1'>
<tr>
<th>Nom</th>
<th>Prenom</th>
<th> Presence</th>
</tr>";
while ($row = $showquery -> fetch(PDO::FETCH_ASSOC)) {
$counter = $row['etudiant_ID'];
echo "<tr>";
echo "<td>" . $row['Nom'] . "</td>";
echo "<td>" . $row['Prenom'] . "</td>"; ?>
<td> Present <input type="radio" name="presence<?=$counter?>" value='present' checked>
Absent <input type="radio" name="presence<?=$counter?>" value="absent">
</td>
<?php
echo "</tr>";
}
echo "</table>";
echo ("<input name='matiere' placeholder='matiere..' required > ");
echo ("<form>");
} ?>
</main>
Here is an image that could help you better understand :
The table that is inside the form
I'm building a wordpress plugin for fun, and I want something crazy, I think.
make a form with a foreach loop with an entire database table.
change the data of the whole database table
update the database data of this table
I've got this until now, but I'm stuck when I want to update the records.
<form method="post">
<input type="hidden" name="form_hidden" value="Y">
<table>
<tbody>
<?php
global $wpdb;
$post_id = $wpdb->get_results("SELECT * FROM tbl_name ORDER BY id ASC");
foreach($post_id as $row){
echo '<tr><td>' . $row->id . '</td><td><input type="text" name="' . $row->name . '" value="' . $row->name . '" /></td></tr>';
}
?>
</tbody>
</table>
<input type="submit" name="Submit" value="Update Options" />
</form>
<?php
if($_POST['form_hidden'] == 'Y') {
//update database
global $wpdb;
foreach($_POST['name'] as $item){
$wpdb->replace( 'tbl_name', ); // <- some kind of array here
}
}
There are multiple issues, I guess.
wpdb::replace(...) replaces the columns of all rows with ONE value;
that is not what you want, correct?
To update the correct row in your table, you should refer to the corresponding primary key. I guess "id" in your case.
To store multiple values in your form as an "array", you have to use field names like "name[]".
Lets put it all together:
// in your form creation loop...
foreach($post_id as $row){
echo '<tr><td><input type="text" name="id[]" value="' . $row->id . '" /></td><td><input type="text" name="name[]" value="' . $row->name . '" /></td></tr>';
}
// processing the new values
foreach($_POST['id'] as $I => $id) {
$sql = $wpdb->prepare("UPDATE tbl_name SET name=%s where id=%d"
, $_POST['name'][$I]
, $id
);
$wpdb->query($sql);
}
These are only the interesting parts - just combine it with your existing code. See
Useful links:
https://developer.wordpress.org/reference/classes/wpdb/prepare/
https://developer.wordpress.org/reference/classes/wpdb/query/
https://www.php.net/manual/en/faq.html.php
Good luck!
I want to fetch data from dropdown list. Like if I choose employee id 40 from the dropdown list it will fetch the data from database of that employee and will shown in the textboxes.
this is my dropdown code. please help me how can i get the selected value.
<?php
$con=mysqli_connect("localhost","root","","hct_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<label>Select Employee ID</label>
<select class="form-control" name="employee_id">
<?php
$result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");
while($row = mysqli_fetch_array($result))
echo "<option value='" . $row['employee_id'] . "'>" . $row['employee_id'] . "</option>";
?>
</select>
First of all give some id to you select option like this:
<select class="form-control" name="employee_id" id='employee'>
Add you textbox like this:
<input type='text' name='emp_name' id='emp_name' />
Than use jquery and ajax something like this:
$('#employee').change(function(){
var selected_id = $(this).val();
var data = {id:selected_id};
$.post('getemp_name.php',data,function(data){
$('#emp_name').val(data);
});
});
getemp_name.php
if(isset($_POST['id'])){
//fire query using this id and get the name of employee and echo it
echo $emp_name;
}
<?php
if(isset($_REQUEST['submit']))
{
$value=$_POST['employee_id'];
$query = mysql_query($con,"SELECT employee_name FROM employee where employee_id=$value");
$result=mysql_fetch_array($query);
$emp_name=$result['employee_name'];
}
?>
<form action="" method="post" name="form">
<label>Select Employee ID</label>
<select class="form-control" name="employee_id">
<?php $result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");
while($row = mysqli_fetch_array($result))
echo "<option value='" . $row['employee_id'] . "'>" .$row['employee_id'] . "</option>";
?>
</select>
<input type="submit" name="submit" value="submit">
</form>
<input type="text" value="<?=$emp_name?>" name="emp_name"/>
check this code as your need
To get your selected value, you have to reach the $_GET or $_POST supertables after the user submits the form.
So, after the submit, get it as, if you POST:
<?php
$employee_id = $_POST['employee_id']; ?>
If you GET:
<?php
$employee_id = $_GET['employee_id']; ?>
apply below function on onChange
function myFunction(mySelect) {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
example
That depends on what you want. If you want to use the selected ID in your query AFTER DOING A POST, you can use the following query:
"SELECT *
FROM `employee`
WHERE `employee`.`employee_id` = " . (int) $_POST['employee_id'] . "
LIMIT 1"
Assuming you are using the post method in your form.
You can easily learn how to fetch data from dropdown using this example by clicking Here This repository contains all the codes
I am getting data from a mysql DB using PHP and then displaying it inside a DIV on a web page.
the data Im fetching is a list of task and I want the users to be able to delete a task once completed.
Image here:
http://www.picpaste.com/Tasks-esmvwRPX.jpg
(apparently I need 10 reputation points before I can embed a picture in my post!)
This is my PHP code used to display the date:
<section class="tasks">
<div id="all-tasks">
<fieldset class="tasks-list">
<?php
$strSQL = "SELECT Tasks.bolComplete, Tasks.strTask, Tasks.datDueDate FROM Tasks WHERE Tasks.idPeople ='" . $_SESSION['UserID'] . "' AND Tasks.bolComplete ='0' ORDER BY Tasks.datDueDate";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo '<label class="tasks-list-item left">';
if ($row['bolComplete'] == 1) {
echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" checked>';
}
else {
echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" unchecked>';
}
echo '<span class="tasks-list-mark"></span>';
echo '<span class="tasks-list-desc">' . $row['strTask'] . '</span>';
// Convert Date format
$originalDate = $row['datDueDate'];
$newDate = date("d-m-y", strtotime($originalDate));
echo '<span class="tasks-list-date">' . $newDate . '</span>';
echo '</label>';
$i++;
}
?>
</fieldset>
</div>
</section>
How can I capture the index of the row displayed so I can do my delete query against the task that is selected?
I have a primary key on idTask (not displayed in the task list) which I need to capture for my delete statement.
Any help is greatly appreciated.
Thanks in advance.
M.
you can assign you idTask to your lable i.e.
<label id="idTaskXXX" class="tasks-list-item left">
...
<lable>
then where ever your code is which is checking for what row is clicked read the id field and use that for deletion
here is an example of how to get id from element: click here
<label id="task1" value="Change" onclick="get_id(this.id)">
Task1
</label>
<label id="task2" value="Change" onclick="get_id(this.id)">
Task2
</label>
javascript to get id:
function get_id(id){
alert('My Id is ' + id);
}
I have one table with 4 columns (id, name, surname, url). I want to create unique subpage for each row. (example.com/id?=444). So if I visit example.com?id=444 I will see data from row which has id 444.
Right now I have form where you add data into database:
> <form action="motogp.php" method="POST"> <input type="text"
> name="name" placeholder="Ime"> <input type="text" name="surname"
> placeholder="Priimek"> <input type="text" name="url" placeholder="URL
> do slike"> <button type="reset" class="ui button">Počisti</button>
> <button type="submit" class="ui positive button">Pošlji</button>
> </form>
Now it gives me just example.com/motogp.php not example.com/?id=444.
Results page code:
$sql="INSERT INTO person (name, surname, url)
VALUES
('$_POST[name]','$_POST[sruname]','$_POST[url]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
$result = mysqli_query($con,"SELECT * FROM person ORDER BY id DESC LIMIT 1");
while($row = mysqli_fetch_array($result))
{
echo "<h2>VIDEO: " . $row['name'] . . $row['surname'] . " z drugo zaporedno zmago zmanjšal zaostanek za Marquezom</h2>";
echo "<img src='images/avoter.png'>";
echo "<img src='" . $row['url'] ."'>";
}
Your query is
SELECT * FROM person ORDER BY id DESC LIMIT 1
That will retrieve the last row (highest-id row) from your table. If more than one user is using the system, that won't always be the row you want.
You probably want
SELECT * FROM person WHERE id = LAST_INSERT_ID()
This will pull out the data for the row you just inserted.
On the page where your URL includes ?id=444 you'll want
SELECT * FROM person WHERE id = ?
and bind the value of the ID in the URL to the SQL parameter, $_REQUEST['id'].