Reputation: 35
I'm just a beginner in terms of programming, so I'm just referring all my codes through tutorials. Luckily, I found this online tutorial in youtube where users are allowed to add, update, and delete data in mysql using php. I follow all his instructions, I got it working but then it stopped when I added css on it.
This is not a general issue, I just need some help. If anyone can help me, much appreciated. Thank you so much.
Maintenance.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "rssfeed";
$connect = mysqli_connect($servername, $username, $password, $database);
if (!$connect) {
die("Cannot connect: " . mysqli_connect_error());
}
mysqli_select_db($connect, $database);
$sql = "SELECT * FROM `maintenance`";
$data = mysqli_query($connect, $sql);
if (isset($_POST['update'])) {
$update = "UPDATE `maintenance` SET `name`='".$_POST['name']."', `url`='".$_POST['url']."', `description`='".$_POST['desc']."' WHERE `name`='".$_POST['hidden']."'";
mysqli_query($connect, $update);
}
echo""
. "<table>"
. "<tr>"
. "<th>Name</th>"
. "<th>URL</th>"
. "<th>Description</th>"
. "<th colspan=2>Action</th>"
. "</tr>";
while ($record = mysqli_fetch_array($data)) {
echo""
. "<form action=maintenance.php method=POST>"
. "<tr>"
. "<td><input class=textbox type=text name=name value='" . $record['name'] . "'> </td>"
. "<td><input class=textbox size=50 type=url name=url value='" . $record['url'] . "'> </td>"
. "<td><textarea class=textbox rows=3 cols=50 wrap=physical name=desc>" . $record['description'] . "</textarea></td>"
. "<input type=hidden name=hidden value=" . $record['name'] . ">"
. "<td><input type=submit name=update value=update></td>"
. "<td><input type=submit name=delete value=delete></td>"
. "</tr>"
. "</form>";
}
echo""
. "</table>";
mysqli_close($connect);
?>
I tried if and else statement if there's something wrong with my code or not. Technically, it says successful, but it doesn't. It's not updating thru mysql and not updating in my input fields. ANY HELP WOULD DO! PLEASE
I got it working. There was an error on my input fields code where the value of my $_POST['hidden'] didn't match the value of the name on my table. Printing the update query using var_dump it helped me look thru my code and got what exactly is the problem.
Error Code
"<input class=textbox type=text name=name value='" . $record['name'] . "'>"
"<input type=hidden name=hidden value=" . $record['name'] . ">"
Fixed Code
"<input type=hidden name=hidden value='" . $record['name'] . "'>"
Upvotes: 0
Views: 4859
Reputation: 35
I got it working. There was an error on my input fields code where the value of my $_POST['hidden'] didn't match the value of the name on my table. Printing the update query using var_dump it helped me look thru my code and got what exactly is the problem.
Error Code
"<input class=textbox type=text name=name value='" . $record['name'] . "'>"
"<input type=hidden name=hidden value=" . $record['name'] . ">"
Fixed Code
"<input type=hidden name=hidden value='" . $record['name'] . "'>"
Thanks to Dinesh who gave me the idea.
Upvotes: 2
Reputation: 4752
There is one obvious problem with your code:
$data = mysqli_query($connect, "SELECT * FROM `maintenance`");
When you issue the SELECT
query, the database server goes to work, collects all the results, and puts them in neat little boxes for you to pick up later.
When mysqli_query()
returns, all the work has been done, everybody has gone home, and only the night guard is left waiting for you to pick up your results.
mysqli_query($connect, "UPDATE `maintenance` ...");
Another query, another day at the factory. This query doesn't affect the results from yesterday in any way.
while ($record = mysqli_fetch_array($data)) {
...
}
And here, you are picking up yesterdays results.
If you want the updated results to appear on the web page, you need to run the UPDATE
before the SELECT
.
Upvotes: 1
Reputation: 3105
Output your SQL query as a string and run it manually on your database and check if its throwing an error. Also check the parameters passed are the one you wanted.
In your if (isset($_POST['update'])
after the variable $update
is assigned with a query, just echo it out using echo $update
or var_dump($update)
, whichever you prefer. Now run a test on your browser and see what is printed.
Once you have that string on your browser and if everything looks fine query your database, either by using command prompt/terminal or application like Workbench, and see what happens. If you are comfortable with posting that string here than do it so we can check if it was created properly.
Upvotes: 4