PHP/MySQL - Update query not functioning [solved] The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How to output MySQL query results in CSV format?PHP: Delete an element from an arrayShould I use the datetime or timestamp data type in MySQL?startsWith() and endsWith() functions in PHPMySQL - UPDATE query based on SELECT QueryHow can I do an UPDATE statement with JOIN in SQL?Reference — What does this symbol mean in PHP?MySQL error code: 1175 during UPDATE in MySQL WorkbenchWhy shouldn't I use mysql_* functions in PHP?

A hang glider, sudden unexpected lift to 25,000 feet altitude, what could do this?

Is it a bad idea to plug the other end of ESD strap to wall ground?

pgfplots: How to draw a tangent graph below two others?

What is the difference between 'contrib' and 'non-free' packages repositories?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

Gödel's incompleteness theorems - what are the religious implications?

Why did the Drakh emissary look so blurred in S04:E11 "Lines of Communication"?

Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?

Another proof that dividing by 0 does not exist -- is it right?

How to pronounce fünf in 45

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

How badly should I try to prevent a user from XSSing themselves?

Ising model simulation

Is a linearly independent set whose span is dense a Schauder basis?

Gauss' Posthumous Publications?

How to show a landlord what we have in savings?

How did scripture get the name bible?

How does a dynamic QR code work?

Is it possible to create a QR code using text?

How to implement Comparable so it is consistent with identity-equality

Is this a new Fibonacci Identity?

Planeswalker Ability and Death Timing

How do I secure a TV wall mount?

Car headlights in a world without electricity



PHP/MySQL - Update query not functioning [solved]



The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How to output MySQL query results in CSV format?PHP: Delete an element from an arrayShould I use the datetime or timestamp data type in MySQL?startsWith() and endsWith() functions in PHPMySQL - UPDATE query based on SELECT QueryHow can I do an UPDATE statement with JOIN in SQL?Reference — What does this symbol mean in PHP?MySQL error code: 1175 during UPDATE in MySQL WorkbenchWhy shouldn't I use mysql_* functions in PHP?










0















I'm trying to develop a small "To Do List" application. The data for the app is stored in a database, and it needs to perform all CRUD operations. As it is right now, Select, Insert, and Delete work just fine. I'm stuck on updating though. The index.php page is shown below:



<?php
session_start();
require_once 'connect.php';

if (isset($_POST['DeleteTask']))
$sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
$stmtdelete->execute();
echo "<div>Task successfully deleted</div>";


if (isset($_POST['theSubmit']))
echo '<p>New task added</p>';

$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try
$sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
$stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtinsert->bindValue(':bvTaskDone', 0);
$stmtinsert->execute();
echo "<div><p>There are no errors. Thank you.</p></div>";
catch(PDOException $e)
echo 'ERROR!!!' .$e->getMessage();
exit();




$sqlselect = "SELECT * from Tasks";
$result = $db->prepare($sqlselect);
$result->execute();
?>


<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>

<h1><u>To-Do List</u></h1>
<table border>
<tr>
<th>Task</th>
<th>Deadline</th>
<th>Status</th>
<th>Complete</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while ($row = $result->fetch())
if ($row['dbTaskDone'] == 0)
$status = "Unfinished";
else
$status = "Finished";

echo '<tr><td>' . $row['dbTaskName']
. '</td><td>' . $row['dbTaskDue']
. '</td><td>' . $status;

/*if ($status == "Unfinished")
echo '</td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="CompleteTask" value="Complete Task">';
echo '</form>';
*/

echo '</td><td>';
echo '<form action="updateTask.php" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
echo '</form></td><td>';

echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="DeleteTask" value="Delete Task">';
echo '</td></tr>';

?>
</table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
<fieldset><legend>New Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $formfield['ffTaskName']; ?>"></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $formfield['ffTaskDue']; ?>"></td>
</tr>
</table>
<input type="submit" name = "theSubmit" value="Add Task">
</fieldset>
</form>
</body>
</html>


Each record displays an "Edit" button that grabs the PK from the "Tasks" table and sends it to the updateTask.php page:



<?php 
require_once 'connect.php';
$errormsg = "";
if (isset($_POST['EditTask']) )
$formfield['ffTaskID'] = $_POST['taskID'];
$sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
$result = $db->prepare($sqlselect);
$result->bindValue(':bvTaskID', $formfield['ffTaskID']);
$result->execute();
$row = $result->fetch();

if( isset($_POST['theEdit']) )

$formfield['ffTaskID'] = $_POST['taskID'];
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try

$sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
dbTaskDue = :bvTaskDue
WHERE dbTaskID = :bvTaskID";
$stmtUpdate = $db->prepare($sqlUpdate);
$stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
$stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
$stmtUpdate->execute();

catch(PDOException $e)

echo 'ERROR!!!' .$e->getMessage();
exit();




?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
<fieldset><legend>Edit Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $row['dbTaskName'];?>" ></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $row['dbTaskDue']; ?>"></td>
</tr>
<tr>
<th>Submit Changes</th>
<input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
<td><input type="submit" name="theEdit" value="Submit Changes">
</table>

</fieldset>
</form>
</body>
</html>


The Name and Deadline fields populate appropriately based on the PK value passed from the last page. However, whenever I press the "Submit Changes" button, the update doesn't seem to execute. The page just refreshes and I see the table data remains unchanged in the database.



Any suggestions and advice would be appreciated.



Edit:



I jumped the gun on believing I had a solution. It turns out that simply un-nesting the second if-statement from the first on the update page didn't fix the problem. The reason I initially believed it was solved was due to me replacing




$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);




...with...




$stmtUpdate->bindvalue(':bvTaskID', 16);




...in my efforts to troubleshoot. So with a specific number declared for the primary key, the update query ran. So this leads me to believe there's an issue with setting the formfield value to the bound value.



Also, there's a second issue. On the index.php page, only the first record's "Edit" button sends the user to the update page. For some reason, clicking the "Edit" button for any other records in the table just causes the page to refresh.



Now I'm even more lost than I was before :/



Edit 2:



I solved part of the problem. The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




$_formfield




..should have been..




$formfield




NOW, the update works correctly. Now the only thing I need to figure out is why only the first record's "Edit" button works where the rest just refresh the page.



FINAL EDIT



The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this. Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.










share|improve this question



















  • 1





    What debugging have you already done?

    – Patrick Q
    Mar 21 at 20:10











  • if (isset($_POST['EditTask']) ) - I don't see any form element with the name EditTask so that if-statement will never evaluate as true.

    – Magnus Eriksson
    Mar 21 at 20:14












  • There is but in wrong file i think echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';

    – Alex
    Mar 21 at 20:15







  • 1





    @PatrickQ - Ah. Now I see. The issue is that one form contains EditTask (making the first if-statement true) but are missing the other required form fields the OP are looking for. Then, in the form in the second file, those form fields exists, but not EditTask, which means that it will never reach the update code.

    – Magnus Eriksson
    Mar 21 at 20:22







  • 1





    @Jack This is why you should have separation of concerns. One bit of code should handle just your display, and another should handle just your update, etc.

    – Patrick Q
    Mar 21 at 20:24















0















I'm trying to develop a small "To Do List" application. The data for the app is stored in a database, and it needs to perform all CRUD operations. As it is right now, Select, Insert, and Delete work just fine. I'm stuck on updating though. The index.php page is shown below:



<?php
session_start();
require_once 'connect.php';

if (isset($_POST['DeleteTask']))
$sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
$stmtdelete->execute();
echo "<div>Task successfully deleted</div>";


if (isset($_POST['theSubmit']))
echo '<p>New task added</p>';

$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try
$sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
$stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtinsert->bindValue(':bvTaskDone', 0);
$stmtinsert->execute();
echo "<div><p>There are no errors. Thank you.</p></div>";
catch(PDOException $e)
echo 'ERROR!!!' .$e->getMessage();
exit();




$sqlselect = "SELECT * from Tasks";
$result = $db->prepare($sqlselect);
$result->execute();
?>


<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>

<h1><u>To-Do List</u></h1>
<table border>
<tr>
<th>Task</th>
<th>Deadline</th>
<th>Status</th>
<th>Complete</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while ($row = $result->fetch())
if ($row['dbTaskDone'] == 0)
$status = "Unfinished";
else
$status = "Finished";

echo '<tr><td>' . $row['dbTaskName']
. '</td><td>' . $row['dbTaskDue']
. '</td><td>' . $status;

/*if ($status == "Unfinished")
echo '</td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="CompleteTask" value="Complete Task">';
echo '</form>';
*/

echo '</td><td>';
echo '<form action="updateTask.php" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
echo '</form></td><td>';

echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="DeleteTask" value="Delete Task">';
echo '</td></tr>';

?>
</table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
<fieldset><legend>New Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $formfield['ffTaskName']; ?>"></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $formfield['ffTaskDue']; ?>"></td>
</tr>
</table>
<input type="submit" name = "theSubmit" value="Add Task">
</fieldset>
</form>
</body>
</html>


Each record displays an "Edit" button that grabs the PK from the "Tasks" table and sends it to the updateTask.php page:



<?php 
require_once 'connect.php';
$errormsg = "";
if (isset($_POST['EditTask']) )
$formfield['ffTaskID'] = $_POST['taskID'];
$sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
$result = $db->prepare($sqlselect);
$result->bindValue(':bvTaskID', $formfield['ffTaskID']);
$result->execute();
$row = $result->fetch();

if( isset($_POST['theEdit']) )

$formfield['ffTaskID'] = $_POST['taskID'];
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try

$sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
dbTaskDue = :bvTaskDue
WHERE dbTaskID = :bvTaskID";
$stmtUpdate = $db->prepare($sqlUpdate);
$stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
$stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
$stmtUpdate->execute();

catch(PDOException $e)

echo 'ERROR!!!' .$e->getMessage();
exit();




?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
<fieldset><legend>Edit Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $row['dbTaskName'];?>" ></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $row['dbTaskDue']; ?>"></td>
</tr>
<tr>
<th>Submit Changes</th>
<input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
<td><input type="submit" name="theEdit" value="Submit Changes">
</table>

</fieldset>
</form>
</body>
</html>


The Name and Deadline fields populate appropriately based on the PK value passed from the last page. However, whenever I press the "Submit Changes" button, the update doesn't seem to execute. The page just refreshes and I see the table data remains unchanged in the database.



Any suggestions and advice would be appreciated.



Edit:



I jumped the gun on believing I had a solution. It turns out that simply un-nesting the second if-statement from the first on the update page didn't fix the problem. The reason I initially believed it was solved was due to me replacing




$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);




...with...




$stmtUpdate->bindvalue(':bvTaskID', 16);




...in my efforts to troubleshoot. So with a specific number declared for the primary key, the update query ran. So this leads me to believe there's an issue with setting the formfield value to the bound value.



Also, there's a second issue. On the index.php page, only the first record's "Edit" button sends the user to the update page. For some reason, clicking the "Edit" button for any other records in the table just causes the page to refresh.



Now I'm even more lost than I was before :/



Edit 2:



I solved part of the problem. The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




$_formfield




..should have been..




$formfield




NOW, the update works correctly. Now the only thing I need to figure out is why only the first record's "Edit" button works where the rest just refresh the page.



FINAL EDIT



The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this. Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.










share|improve this question



















  • 1





    What debugging have you already done?

    – Patrick Q
    Mar 21 at 20:10











  • if (isset($_POST['EditTask']) ) - I don't see any form element with the name EditTask so that if-statement will never evaluate as true.

    – Magnus Eriksson
    Mar 21 at 20:14












  • There is but in wrong file i think echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';

    – Alex
    Mar 21 at 20:15







  • 1





    @PatrickQ - Ah. Now I see. The issue is that one form contains EditTask (making the first if-statement true) but are missing the other required form fields the OP are looking for. Then, in the form in the second file, those form fields exists, but not EditTask, which means that it will never reach the update code.

    – Magnus Eriksson
    Mar 21 at 20:22







  • 1





    @Jack This is why you should have separation of concerns. One bit of code should handle just your display, and another should handle just your update, etc.

    – Patrick Q
    Mar 21 at 20:24













0












0








0








I'm trying to develop a small "To Do List" application. The data for the app is stored in a database, and it needs to perform all CRUD operations. As it is right now, Select, Insert, and Delete work just fine. I'm stuck on updating though. The index.php page is shown below:



<?php
session_start();
require_once 'connect.php';

if (isset($_POST['DeleteTask']))
$sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
$stmtdelete->execute();
echo "<div>Task successfully deleted</div>";


if (isset($_POST['theSubmit']))
echo '<p>New task added</p>';

$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try
$sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
$stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtinsert->bindValue(':bvTaskDone', 0);
$stmtinsert->execute();
echo "<div><p>There are no errors. Thank you.</p></div>";
catch(PDOException $e)
echo 'ERROR!!!' .$e->getMessage();
exit();




$sqlselect = "SELECT * from Tasks";
$result = $db->prepare($sqlselect);
$result->execute();
?>


<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>

<h1><u>To-Do List</u></h1>
<table border>
<tr>
<th>Task</th>
<th>Deadline</th>
<th>Status</th>
<th>Complete</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while ($row = $result->fetch())
if ($row['dbTaskDone'] == 0)
$status = "Unfinished";
else
$status = "Finished";

echo '<tr><td>' . $row['dbTaskName']
. '</td><td>' . $row['dbTaskDue']
. '</td><td>' . $status;

/*if ($status == "Unfinished")
echo '</td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="CompleteTask" value="Complete Task">';
echo '</form>';
*/

echo '</td><td>';
echo '<form action="updateTask.php" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
echo '</form></td><td>';

echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="DeleteTask" value="Delete Task">';
echo '</td></tr>';

?>
</table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
<fieldset><legend>New Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $formfield['ffTaskName']; ?>"></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $formfield['ffTaskDue']; ?>"></td>
</tr>
</table>
<input type="submit" name = "theSubmit" value="Add Task">
</fieldset>
</form>
</body>
</html>


Each record displays an "Edit" button that grabs the PK from the "Tasks" table and sends it to the updateTask.php page:



<?php 
require_once 'connect.php';
$errormsg = "";
if (isset($_POST['EditTask']) )
$formfield['ffTaskID'] = $_POST['taskID'];
$sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
$result = $db->prepare($sqlselect);
$result->bindValue(':bvTaskID', $formfield['ffTaskID']);
$result->execute();
$row = $result->fetch();

if( isset($_POST['theEdit']) )

$formfield['ffTaskID'] = $_POST['taskID'];
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try

$sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
dbTaskDue = :bvTaskDue
WHERE dbTaskID = :bvTaskID";
$stmtUpdate = $db->prepare($sqlUpdate);
$stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
$stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
$stmtUpdate->execute();

catch(PDOException $e)

echo 'ERROR!!!' .$e->getMessage();
exit();




?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
<fieldset><legend>Edit Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $row['dbTaskName'];?>" ></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $row['dbTaskDue']; ?>"></td>
</tr>
<tr>
<th>Submit Changes</th>
<input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
<td><input type="submit" name="theEdit" value="Submit Changes">
</table>

</fieldset>
</form>
</body>
</html>


The Name and Deadline fields populate appropriately based on the PK value passed from the last page. However, whenever I press the "Submit Changes" button, the update doesn't seem to execute. The page just refreshes and I see the table data remains unchanged in the database.



Any suggestions and advice would be appreciated.



Edit:



I jumped the gun on believing I had a solution. It turns out that simply un-nesting the second if-statement from the first on the update page didn't fix the problem. The reason I initially believed it was solved was due to me replacing




$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);




...with...




$stmtUpdate->bindvalue(':bvTaskID', 16);




...in my efforts to troubleshoot. So with a specific number declared for the primary key, the update query ran. So this leads me to believe there's an issue with setting the formfield value to the bound value.



Also, there's a second issue. On the index.php page, only the first record's "Edit" button sends the user to the update page. For some reason, clicking the "Edit" button for any other records in the table just causes the page to refresh.



Now I'm even more lost than I was before :/



Edit 2:



I solved part of the problem. The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




$_formfield




..should have been..




$formfield




NOW, the update works correctly. Now the only thing I need to figure out is why only the first record's "Edit" button works where the rest just refresh the page.



FINAL EDIT



The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this. Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.










share|improve this question
















I'm trying to develop a small "To Do List" application. The data for the app is stored in a database, and it needs to perform all CRUD operations. As it is right now, Select, Insert, and Delete work just fine. I'm stuck on updating though. The index.php page is shown below:



<?php
session_start();
require_once 'connect.php';

if (isset($_POST['DeleteTask']))
$sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
$stmtdelete->execute();
echo "<div>Task successfully deleted</div>";


if (isset($_POST['theSubmit']))
echo '<p>New task added</p>';

$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try
$sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
$stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtinsert->bindValue(':bvTaskDone', 0);
$stmtinsert->execute();
echo "<div><p>There are no errors. Thank you.</p></div>";
catch(PDOException $e)
echo 'ERROR!!!' .$e->getMessage();
exit();




$sqlselect = "SELECT * from Tasks";
$result = $db->prepare($sqlselect);
$result->execute();
?>


<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>

<h1><u>To-Do List</u></h1>
<table border>
<tr>
<th>Task</th>
<th>Deadline</th>
<th>Status</th>
<th>Complete</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while ($row = $result->fetch())
if ($row['dbTaskDone'] == 0)
$status = "Unfinished";
else
$status = "Finished";

echo '<tr><td>' . $row['dbTaskName']
. '</td><td>' . $row['dbTaskDue']
. '</td><td>' . $status;

/*if ($status == "Unfinished")
echo '</td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="CompleteTask" value="Complete Task">';
echo '</form>';
*/

echo '</td><td>';
echo '<form action="updateTask.php" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
echo '</form></td><td>';

echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="DeleteTask" value="Delete Task">';
echo '</td></tr>';

?>
</table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
<fieldset><legend>New Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $formfield['ffTaskName']; ?>"></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $formfield['ffTaskDue']; ?>"></td>
</tr>
</table>
<input type="submit" name = "theSubmit" value="Add Task">
</fieldset>
</form>
</body>
</html>


Each record displays an "Edit" button that grabs the PK from the "Tasks" table and sends it to the updateTask.php page:



<?php 
require_once 'connect.php';
$errormsg = "";
if (isset($_POST['EditTask']) )
$formfield['ffTaskID'] = $_POST['taskID'];
$sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
$result = $db->prepare($sqlselect);
$result->bindValue(':bvTaskID', $formfield['ffTaskID']);
$result->execute();
$row = $result->fetch();

if( isset($_POST['theEdit']) )

$formfield['ffTaskID'] = $_POST['taskID'];
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);

if(empty($formfield['ffTaskName']))$errormsg .= "<p>Task field is empty.</p>";
if(empty($formfield['ffTaskDue']))$errormsg .= "<p>Deadline field is empty.</p>";

if ($errormsg != "")
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
else
try

$sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
dbTaskDue = :bvTaskDue
WHERE dbTaskID = :bvTaskID";
$stmtUpdate = $db->prepare($sqlUpdate);
$stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
$stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
$stmtUpdate->execute();

catch(PDOException $e)

echo 'ERROR!!!' .$e->getMessage();
exit();




?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
<fieldset><legend>Edit Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $row['dbTaskName'];?>" ></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $row['dbTaskDue']; ?>"></td>
</tr>
<tr>
<th>Submit Changes</th>
<input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
<td><input type="submit" name="theEdit" value="Submit Changes">
</table>

</fieldset>
</form>
</body>
</html>


The Name and Deadline fields populate appropriately based on the PK value passed from the last page. However, whenever I press the "Submit Changes" button, the update doesn't seem to execute. The page just refreshes and I see the table data remains unchanged in the database.



Any suggestions and advice would be appreciated.



Edit:



I jumped the gun on believing I had a solution. It turns out that simply un-nesting the second if-statement from the first on the update page didn't fix the problem. The reason I initially believed it was solved was due to me replacing




$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);




...with...




$stmtUpdate->bindvalue(':bvTaskID', 16);




...in my efforts to troubleshoot. So with a specific number declared for the primary key, the update query ran. So this leads me to believe there's an issue with setting the formfield value to the bound value.



Also, there's a second issue. On the index.php page, only the first record's "Edit" button sends the user to the update page. For some reason, clicking the "Edit" button for any other records in the table just causes the page to refresh.



Now I'm even more lost than I was before :/



Edit 2:



I solved part of the problem. The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




$_formfield




..should have been..




$formfield




NOW, the update works correctly. Now the only thing I need to figure out is why only the first record's "Edit" button works where the rest just refresh the page.



FINAL EDIT



The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this. Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.







php mysql database sql-update crud






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 0:01







Jack Phillips

















asked Mar 21 at 20:07









Jack PhillipsJack Phillips

63




63







  • 1





    What debugging have you already done?

    – Patrick Q
    Mar 21 at 20:10











  • if (isset($_POST['EditTask']) ) - I don't see any form element with the name EditTask so that if-statement will never evaluate as true.

    – Magnus Eriksson
    Mar 21 at 20:14












  • There is but in wrong file i think echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';

    – Alex
    Mar 21 at 20:15







  • 1





    @PatrickQ - Ah. Now I see. The issue is that one form contains EditTask (making the first if-statement true) but are missing the other required form fields the OP are looking for. Then, in the form in the second file, those form fields exists, but not EditTask, which means that it will never reach the update code.

    – Magnus Eriksson
    Mar 21 at 20:22







  • 1





    @Jack This is why you should have separation of concerns. One bit of code should handle just your display, and another should handle just your update, etc.

    – Patrick Q
    Mar 21 at 20:24












  • 1





    What debugging have you already done?

    – Patrick Q
    Mar 21 at 20:10











  • if (isset($_POST['EditTask']) ) - I don't see any form element with the name EditTask so that if-statement will never evaluate as true.

    – Magnus Eriksson
    Mar 21 at 20:14












  • There is but in wrong file i think echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';

    – Alex
    Mar 21 at 20:15







  • 1





    @PatrickQ - Ah. Now I see. The issue is that one form contains EditTask (making the first if-statement true) but are missing the other required form fields the OP are looking for. Then, in the form in the second file, those form fields exists, but not EditTask, which means that it will never reach the update code.

    – Magnus Eriksson
    Mar 21 at 20:22







  • 1





    @Jack This is why you should have separation of concerns. One bit of code should handle just your display, and another should handle just your update, etc.

    – Patrick Q
    Mar 21 at 20:24







1




1





What debugging have you already done?

– Patrick Q
Mar 21 at 20:10





What debugging have you already done?

– Patrick Q
Mar 21 at 20:10













if (isset($_POST['EditTask']) ) - I don't see any form element with the name EditTask so that if-statement will never evaluate as true.

– Magnus Eriksson
Mar 21 at 20:14






if (isset($_POST['EditTask']) ) - I don't see any form element with the name EditTask so that if-statement will never evaluate as true.

– Magnus Eriksson
Mar 21 at 20:14














There is but in wrong file i think echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';

– Alex
Mar 21 at 20:15






There is but in wrong file i think echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';

– Alex
Mar 21 at 20:15





1




1





@PatrickQ - Ah. Now I see. The issue is that one form contains EditTask (making the first if-statement true) but are missing the other required form fields the OP are looking for. Then, in the form in the second file, those form fields exists, but not EditTask, which means that it will never reach the update code.

– Magnus Eriksson
Mar 21 at 20:22






@PatrickQ - Ah. Now I see. The issue is that one form contains EditTask (making the first if-statement true) but are missing the other required form fields the OP are looking for. Then, in the form in the second file, those form fields exists, but not EditTask, which means that it will never reach the update code.

– Magnus Eriksson
Mar 21 at 20:22





1




1





@Jack This is why you should have separation of concerns. One bit of code should handle just your display, and another should handle just your update, etc.

– Patrick Q
Mar 21 at 20:24





@Jack This is why you should have separation of concerns. One bit of code should handle just your display, and another should handle just your update, etc.

– Patrick Q
Mar 21 at 20:24












1 Answer
1






active

oldest

votes


















0














Solved the problem!



There were several issues that I discovered.



1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.



2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




$_formfield




..should have been..




$formfield




At this point, the update query functions properly.



3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.



Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288507%2fphp-mysql-update-query-not-functioning-solved%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Solved the problem!



    There were several issues that I discovered.



    1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.



    2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




    $_formfield




    ..should have been..




    $formfield




    At this point, the update query functions properly.



    3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.



    Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.






    share|improve this answer





























      0














      Solved the problem!



      There were several issues that I discovered.



      1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.



      2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




      $_formfield




      ..should have been..




      $formfield




      At this point, the update query functions properly.



      3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.



      Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.






      share|improve this answer



























        0












        0








        0







        Solved the problem!



        There were several issues that I discovered.



        1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.



        2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




        $_formfield




        ..should have been..




        $formfield




        At this point, the update query functions properly.



        3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.



        Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.






        share|improve this answer















        Solved the problem!



        There were several issues that I discovered.



        1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.



        2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.




        $_formfield




        ..should have been..




        $formfield




        At this point, the update query functions properly.



        3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.



        Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 22 at 0:02

























        answered Mar 21 at 21:40









        Jack PhillipsJack Phillips

        63




        63





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288507%2fphp-mysql-update-query-not-functioning-solved%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현