/    Sign up×
Community /Pin to ProfileBookmark

update a task

Hi

I have managed to get the record inserted successfully to the database but am stuck with updating the record using a php form, the data is stored across multiple tables, my database looks like the following

assignment table has the following columns
assign_id
emp_id
task_id

employee table has the following columns
emp_id
username
password
psalt
emp_name

task table has the following columns
task_id
description
status

I’t don’t show any errors, it just shows the h2 title and the label

I have attached a screenshot of how it looks and the current code I have

[CODE]
<?php
ini_set(‘display_startup_errors’,1);
ini_set(‘display_errors’,1);
error_reporting(-1);
?>

<?
session_start();

if($_SESSION[‘user’]==”){
header(“Location:login.php”);
}else{
include(“config.php”);
$sql=$dbh->prepare(“SELECT * FROM employee WHERE emp_id=?”);
$sql->execute(array($_SESSION[‘user’]));
while($r=$sql->fetch()){
echo “<div class=’home-content’>”;
echo “<center><h2>Hello, “.$r[‘username’].”</h2>”;
echo “<a href=’logout.php’>Log Out</a></center>”;
echo “</div>”;
}
}
?>

<?php
include(“config.php”);

$sql=$dbh->prepare(“SELECT
e.emp_id
, t.task_id
, t.description
, t.status
FROM employee e
JOIN assignment a ON e.emp_id = a.emp_id
JOIN task t ON a.task_id = t.task_id
WHERE e.emp_id = ?”);
$sql->execute(array($_SESSION[‘user’]));

// were any rows found?
if ($row = $sql->fetchObject())
{

var_dump($sql);

?>

<html>
<head>
<title>Add Task</title>
</head>
<body>
<div id=’title’>
<h1>Edit Task</h1>

<form method=”post” action=”update-staff-task.php”>

<fieldset>
<legend>Task</legend>
<div class=’label’><label for=’descrip’>Description</label></div>
<input type=”text” name=”descrip” id=”descrip” size=”50″ value=”<?php echo $row[‘description’]; ?>” />
<br>
<div class=’label’><label for=’status’>Status</label></div>
<select name=’status’ id=’status’>
<option value=’0′>Not started</option>
<option value=’1′>In progress</option>
<option value=’2′>Completed</option>
</select>
</fieldset>
<br>
<fieldset>
<legend>Assign to</legend>
<?= emps($db) ?>
</fieldset>
<input type=”submit” name=”btnSubmit” value=”Submit”>
<?php } ?>
</form>
</div>
</body>
</html>

<?php
function emps($db)
/*******************************************
* function to list employees with checkboxes
********************************************/
{
$sql = “SELECT emp_id, emp_name
FROM employee
ORDER BY emp_name”;
$emps=”;
foreach($db->query($sql) as $row) {
$emps .= “<input type=’checkbox’ name=’emp_id[]’ value='{$row[’emp_id’]}’> {$row[’emp_name’]}<br>”;
}
return $emps;
}
?>
[/CODE]

I don’t get it and can’t work it out where I have gone wrong, can someone help me please

Thank you in advance

Ian

[canned-message]attachments-removed-during-migration[/canned-message]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ianhaneyauthorNov 23.2015 — Hi

I have a update, I have managed to get the form displaying but got a error in the description input field

The error is below

<br /><b>Notice</b>: Undefined variable: row in <b>/home/sites/it-doneright.co.uk/public_html/admin/staff-tasks/update-staff-task.php</b> on line <b>76</b><br />

The code I have now is below

[CODE]
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>

<?
session_start();

if($_SESSION['user']==''){
header("Location:login.php");
}else{
include("config.php");
$sql=$dbh->prepare("SELECT * FROM employee WHERE emp_id=?");
$sql->execute(array($_SESSION['user']));
while($r=$sql->fetch()){
echo "<div class='home-content'>";
echo "<center><h2>Hello, ".$r['username']."</h2>";
echo "<a href='logout.php'>Log Out</a></center>";
echo "</div>";
}
}
?>

<?php
$hostname='';
$username='';
$password='';

$db = new PDO("mysql:host=$hostname;dbname=",$username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql=$dbh->prepare("SELECT
e.emp_id
, t.task_id
, t.description
, t.status
FROM employee e
JOIN assignment a ON e.emp_id = a.emp_id
JOIN task t ON a.task_id = t.task_id
WHERE e.emp_id = ?");
$sql->execute(array($_SESSION['user']));

$sql->setFetchMode(PDO::FETCH_ASSOC);

function emps($db)
/*******************************************
* function to list employees with checkboxes
********************************************/
{
$sql = "SELECT emp_id, emp_name
FROM employee
ORDER BY emp_name";
$emps='';
foreach($db->query($sql) as $row) {
$emps .= "<input type='checkbox' name='emp_id[]' value='{$row['emp_id']}'> {$row['emp_name']}<br>";
}
return $emps;
}

?>

<html>
<head>
<title>Update Task</title>
</head>
<body>
<div id='title'>
<h1>Edit Task</h1>

<form method="post" action="update-staff-task.php">

<fieldset>
<legend>Task</legend>
<div class='label'><label for='descrip'>Description</label></div>
<input type="text" name="descrip" id="descrip" size="50" value="<?php echo $row['description']; ?>" />
<br>

<div class='label'><label for='status'>Status</label></div>
<select name='status' id='status'>
<option value='0'>Not started</option>
<option value='1'>In progress</option>
<option value='2'>Completed</option>
</select>
</fieldset>
<br>
<fieldset>
<legend>Assign to</legend>
<?= emps($db) ?>
</fieldset>
<input type="submit" name="btnSubmit" value="Update">

</form>
</div>

</body>
</html>
[/CODE]


Can someone take a look at my code please and see where I am going wrong, thank you
Copy linkTweet thisAlerts:
@NogDogNov 23.2015 — $row is not available outside the scope of the emp() function in your current code. If you need those values outside of it, you could put them into $_SESSION. However, since $row is defined inside a foreach loop, I'm not sure that makes sense, since where you are using it is not within that loop -- or do those particular values in $row always have the same value?
Copy linkTweet thisAlerts:
@ianhaneyauthorNov 23.2015 — Hi

Sorry been at this for a while, I got a update, I now have no errors and got the employee who the task is assigned to but I can't work out why the description and status is not being pulled from the database, my code is below

[CODE]
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>

<?
session_start();

if($_SESSION['user']==''){
header("Location:login.php");
}else{
include("config.php");
$sql=$dbh->prepare("SELECT * FROM employee WHERE emp_id=?");
$sql->execute(array($_SESSION['user']));
while($r=$sql->fetch()){
echo "<div class='home-content'>";
echo "<center><h2>Hello, ".$r['username']."</h2>";
echo "<a href='logout.php'>Log Out</a></center>";
echo "</div>";
}
}
?>

<?php
$hostname='';
$username='';
$password='';

$db = new PDO("mysql:host=$hostname;dbname=",$username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// return the task which matches $_GET['task_id'];
$sql=$dbh->prepare("
SELECT task_id
description
status
FROM task
WHERE task_id = ?");

$sql->execute(array($_GET['task_id']));

$sql->setFetchMode(PDO::FETCH_ASSOC);
// fetch the row from the result
$row = $sql->fetch();

function emps_assigned_by_taskid($db, $task_id)
/*******************************************
* function to list employees with checkboxes - checkbox is checked if they are assigned to the task
********************************************/
{
$sql = "SELECT e.emp_id,
e.emp_name,
IF(a.emp_id IS NULL, 0, 1) as isAssigned
FROM employee e
LEFT JOIN assignment a ON e.emp_id = a.emp_id AND a.task_id = ?";
$stmt = $db->prepare($sql);

$stmt = $db->prepare($sql);
$stmt->execute(array($task_id));

$emps='';
foreach($stmt->fetchAll() as $row) {
// if isAssigned is set to 1 then set the checked attribute, otherwise leave blank
$checked = $row['isAssigned'] == 1 ? ' checked="checked" ' : '';
$emps .= "<input type='checkbox' name='emp_id[]' value='{$row['emp_id']}'{$checked}> {$row['emp_name']}<br>";
}
return $emps;
}

?>

<html>
<head>
<title>Update Task</title>
</head>
<body>
<div id='title'>
<h1>Edit Task</h1>

<form method="post" action="update-staff-task.php">

<fieldset>
<legend>Task</legend>
<div class='label'><label for='descrip'>Description</label></div>
<input type="text" name="descrip" id="descrip" size="50" value="<?php echo $row['description']; ?>" />
<br>
<br>
<div class='label'><label for='status'>Status</label></div>
<select name='status' id='status'>
<option value='0'>Not started</option>
<option value='1'>In progress</option>
<option value='2'>Completed</option>
</select>
</fieldset>
<br>
<fieldset>
<legend>Assign to</legend>
<?= emps_assigned_by_taskid($db, $_GET['task_id']) ?>
</fieldset>
<input type="submit" name="btnSubmit" value="Update">

</form>
</div>

</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@LandslydeNov 23.2015 — In you SELECT statement, use commas between the fields:

$sql=$dbh-&gt;prepare("
SELECT task_id, description, status
FROM task
WHERE task_id = ?");
Copy linkTweet thisAlerts:
@ianhaneyauthorNov 23.2015 — In you SELECT statement, use commas between the fields:

$sql=$dbh-&gt;prepare("
SELECT task_id, description, status
FROM task
WHERE task_id = ?");
[/QUOTE]


Thank you so much, that worked perfect

But as soon as I click update, I get the following errors

Notice: Undefined index: task_id in /home/sites/it-doneright.co.uk/public_html/admin/staff-tasks/update-staff-task.php on line 41

Notice: Undefined index: task_id in /home/sites/it-doneright.co.uk/public_html/admin/staff-tasks/update-staff-task.php on line 101

On them lines are the following

Line 41
[CODE]
$sql->execute(array($_GET['task_id']));
[/CODE]


Line 101
[CODE]
<?= emps_assigned_by_taskid($db, $_GET['task_id']) ?>
[/CODE]
Copy linkTweet thisAlerts:
@NogDogNov 24.2015 — Should those be $_POST elements instead $_GET?
Copy linkTweet thisAlerts:
@ianhaneyauthorNov 24.2015 — Hi

I changed them to $_POST instead of $_GET but had the same errors so changed it back to $_GET and the errors disappeared but does not work when I click update, I still get the same following errors after clicking update

Notice: Undefined index: task_id in /home/sites/it-doneright.co.uk/public_html/admin/staff-tasks/update-staff-task.php on line 41

Notice: Undefined index: task_id in /home/sites/it-doneright.co.uk/public_html/admin/staff-tasks/update-staff-task.php on line 101
Copy linkTweet thisAlerts:
@NogDogNov 24.2015 — Sounds like maybe you need to add those parameters to the form action's URL?
Copy linkTweet thisAlerts:
@ianhaneyauthorNov 24.2015 — I am winning, been playing about with the coding

I have sorted them errors and got it all updating in the database all ok but have added in a success and failed message but am getting the following error

Notice: Undefined property: PDOStatement::$rowCount in /home/sites/it-doneright.co.uk/public_html/admin/staff-tasks/update-staff-task.php on line 43

The coding I have now is below

[CODE]
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>

<?
session_start();

if($_SESSION['user']==''){
header("Location:login.php");
}else{
include("config.php");
$sql=$dbh->prepare("SELECT * FROM employee WHERE emp_id=?");
$sql->execute(array($_SESSION['user']));
while($r=$sql->fetch()){
echo "<div class='home-content'>";
echo "<center><h2>Hello, ".$r['username']."</h2>";
echo "<a href='logout.php'>Log Out</a></center>";
echo "</div>";
}
}
?>

<?php
$hostname='';
$username='';
$password='';

$db = new PDO("mysql:host=$hostname;dbname=",$username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// if form is submitted update task details
if ($_SERVER['REQUEST_METHOD']=='POST') { // was data sent
if ($_POST['descrip'] != '') {
try {
$sql = "UPDATE task SET description = ?, status = ? WHERE task_id = ?";
$stmt = $db->prepare($sql);

$result = $stmt->execute(array($_POST['descrip'], $_POST['status'], $_GET['task_id']));

if($result && $stmt->rowCount !== 0) {
$msg = 'Task has been updated successfully';
} else {
$msg = 'Sorry unable to update task.';
}

}
catch (PDOException $e) {
$db->rollBack();
die($e->getMessage());
}
}
}

// return the task which matches $_GET['task_id'];
$sql=$dbh->prepare("
SELECT task_id
, description
, status
FROM task
WHERE task_id = ?");

$sql->execute(array($_GET['task_id']));

$sql->setFetchMode(PDO::FETCH_ASSOC);
// fetch the row from the result
$row = $sql->fetch();

function emps_assigned_by_taskid($db, $task_id)
/*******************************************
* function to list employees with checkboxes - checkbox is checked if they are assigned to the task
********************************************/
{
$sql = "SELECT e.emp_id,
e.emp_name,
IF(a.emp_id IS NULL, 0, 1) as isAssigned
FROM employee e
LEFT JOIN assignment a ON e.emp_id = a.emp_id AND a.task_id = ?";
$stmt = $db->prepare($sql);

$stmt = $db->prepare($sql);
$stmt->execute(array($task_id));

$emps='';
foreach($stmt->fetchAll() as $row) {
// if isAssigned is set to 1 then set the checked attribute, otherwise leave blank
$checked = $row['isAssigned'] == 1 ? ' checked="checked" ' : '';
$emps .= "<input type='checkbox' name='emp_id[]' value='{$row['emp_id']}'{$checked}> {$row['emp_name']}<br>";
}
return $emps;
}

?>

<html>
<head>
<title>Update Task</title>
</head>
<body>
<div id='title'>
<h1>Edit Task</h1>

<?php if(isset($msg)): ?>
<p><?=$msg; ?></p>
<?php endif; ?>

<form method="post" action="">

<fieldset>
<legend>Task</legend>
<div class='label'><label for='descrip'>Description</label></div>
<input type="text" name="descrip" id="descrip" size="50" value="<?php echo $row['description']; ?>" />
</fieldset>
<br><br>
Current Status is: <strong><?php echo $row['status']; ?></strong>
<br><br>
<div class='label'><label for='status'>Status</label></div>
<select name='status' id='status'>
<option value='Not Started'>Not started</option>
<option value='In Progress'>In progress</option>
<option value='Completed'>Completed</option>
</select>
</fieldset>
<br><br>
<fieldset>
<legend>Assign to</legend>
<?= emps_assigned_by_taskid($db, $_GET['task_id']) ?>
</fieldset>
<input type="submit" name="btnSubmit" value="Update">

</form>
</div>

</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@ianhaneyauthorNov 24.2015 — Sorry I missed () after the rowcount

Is working perfect now
Copy linkTweet thisAlerts:
@lethanhkhuongNov 26.2015 — , I'm not sure that makes sense, since where you are using it is not within that loop -- or do those particular values in $row always have the same value?
×

Success!

Help @ianhaney spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...