-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Before you can update a task, you need to be able to specify which task you want to update. Then you have to get the data for that task and check the checkboxes for the employees already assigned to that task. Once that is done the existing data can be edited and saved. As you see, it's not just changing INSERT to UPDATE.
-
Looks like your php version is out of date. Instead of [$task_id, $emp] you will need array($task_id, $emp), and same with the other line. [] is short form of array()from PHP5.4+ http://uk3.php.net/manual/en/language.types.array.php
-
Easiest way in this situation is a form with task description task status a list of checkboxes for each employee that can be assigned. On posting, insert new task and get the generated id using lastInsertId() Loop through the posted checkbox values (emp_ids) and insert assignment record for each Here's an example $db = new PDO("mysql:host=localhost;dbname=DBNAME",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if ($_SERVER['REQUEST_METHOD']=='POST') { // was data sent if ($_POST['descrip'] != '') { try { $db->beginTransaction(); $sql = "INSERT INTO task (description, status) VALUES (?,?)"; $stmt = $db->prepare($sql); $stmt->execute([$_POST['descrip'], $_POST['status']]); $task_id = $db->lastInsertId(); // get the id of new task // now insert employees assigned to the task $sql = "INSERT INTO assignment(task_id, emp_id) VALUES (?,?)"; $stmt = $db->prepare($sql); foreach ($_POST['emp_id'] as $emp) { $stmt->execute([$task_id, $emp]); } $db->commit(); } catch (PDOException $e) { $db->rollBack(); die($e->getMessage()); } } } 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> <meta name="generator" content="PhpED 14.0 (Build 14039, 64bit)"> <title>Add Task</title> <meta name="author" content="Barand"> <meta name="creation-date" content="11/21/2015"> <style type="text/css"> .label { width: 150px; display: inline-block; } </style> </head> <body> <div id='title'> <h1>Add Task</h1> <form method="post"> <fieldset> <legend>Task</legend> <div class='label'><label for='descrip'>Description</label></div> <input type="text" name="descrip" id="descrip" size="50" /> <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"> </form> </div> </body> </html>
-
you need to use fetchObject() and not fetchColumn(). This worked for me $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()) { // display records in a table echo "<table>"; // set table headers echo "<tr> <th>Task ID</th> <th>Description</th> <th>Status</th> <th>Emp ID</th> <th>Status</th> <th colspan='1'>Actions</th> </tr>"; do { // set up a row for each record echo "<tr>"; echo "<td>" . $row->task_id . "</td>"; echo "<td>" . $row->description . "</td>"; echo "<td>" . $row->status . "</td>"; echo "<td>" . $row->emp_id . "</td>"; echo "<td>" . $row->status . "</td>"; echo "<td><a href='records.php?task_id=" . $row->task_id . "'>Edit</a></td>"; echo "</tr>"; } while ($row = $sql->fetchObject()); echo "</table>"; } // if there are no records in the database, display an alert message else { echo "No results to display!"; }
-
So you now have two functions which do exactly the same thing, good thinking. The correct solution was to define the function once then call it each time you need it.
-
Your connection object is $db, but you are trying to use $mysqli->prepare()
-
When you specify an int length in MySQL what does it mean?
Barand replied to greenace92's topic in MySQL Help
http://dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html -
Do you still have this line? while ($row = $result->fetch_object()) because you don't have a $result object, you have $sql as a statement object. Do it like you did in the query above that one in your code.
-
Does your assignment table contain any records for emp_id = 2?
-
why have you changed "prepare" to "query"?
-
$loggedInUser is just an example to demonstrate the query syntax. So in your case it will probably be $sql=$dbh->prepare("SELECT e.emp_id , e.emp_name , 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']));
-
yes
-
When the user logs in, store their id in $_SESSION variable. Then you uses a WHERE clause to get that users data SELECT e.emp_id , e.emp_name , 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 = $loggedInUser
-
You join table on the matching fields. So join employee to assignment using the emp_id fields and join task to assignment using the task_id fields. SELECT e.emp_id , e.emp_name , t.description FROM employee e JOIN assignment a ON e.emp_id = a.emp_id JOIN task t ON a.task_id = t.task_id
-
Not a difficult problem. It's basically maintaining three tables +-------------+ +--------------+ | employee | | task | +-------------+ +--------------+ | emp_id (PK) | | task_id (PK) | | empname | | description | +-------------+ | status | | +--------------+ | | | | | | | +----------------+ | | | assignment | | | +----------------+ | +---------------------<| assign_id (PK) |>------------------+ | emp_id | | task_id | +----------------+ Use the assignment table to find the tasks assigned to an employee
-
It should have. That's THE PHP MANUAL. What have you been reading?
-
http://php.net/manual/en/function.file.php Parameter section flags subsection
-
Creating/getting a url to a dynamically created file...
Barand replied to enveetee's topic in PHP Coding Help
Store your pdf file locations relative to the root, not relative to your application. Then it doesn't matter which folder the application runs from -
As I pointed out before, you input lines ($r) have eol at the end, so the second "0" will be at beginning of next line. Take out the newlines when you read. $readin=file('proverb_load.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES ); $handle = fopen("proverb_load.txt", 'a'); foreach($readin as $r){ $num=0; $count=0; fwrite($handle,$num.$r.$count.PHP_EOL); } fclose($handle); Input jkhkjj jkhlkhjljk your word for the day we wish you a merry christmas good tightings to you i love football After processing jkhkjj jkhlkhjljk your word for the day we wish you a merry christmas good tightings to you i love football 0jkhkjj0 0jkhlkhjljk0 0your word for the day0 0we wish you a merry christmas0 0good tightings to you0 0i love football0
-
The lines in your input already have EOL at the end. You are adding an extra when you write. Also, open the output file before the loop, write records in the loop, close file after the loop
-
How can I get client timezone using PHP/JS
Barand replied to aHMAD_SQaLli's topic in PHP Coding Help
Javascript Date class has getTimezoneOffset() method;- 4 replies
-
- 1
-
- php
- javascript
-
(and 1 more)
Tagged with:
-
try using $readin=file('proverb_load.txt', FILE_SKIP_EMPTY_LINES );
-
Try a bit of debugging. Examine the content of the error message given by mysqli_error() after executing the delete query
-
A new argument is the last thing you need, you are already passing too many. The third argument is the ID of the one you want selected. You effectively have two third parameters. Instead of passing 27 after the $_GET() parameter, pass it instead of the $_GET().
-
Yes, where did you get this from VALUES (?,?,NOW(),?,?) when you need to put values into 14 columns? And here, when you bind, you specify 14 variables but only define types ('ssi') for 3, none of which is an integer. And where, in the examples in the manual, did you see VALUES() in a bind statement syntax?