I am making a page where I can edit the MySQL records via a PHP page. However, it wont display from the database. It keeps telling me the query was empty. However, I know there is stuff there.
And then, I do not know where to start with the page to edit it, like the HTML form. Can anyone help me?
<?php
session_start();
if(!isset($_SESSION['logged_in']) ||
!isset($_SESSION['username'])) {
header("location: login.php");
}
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*****", $con);
$result = mysql_query("SELECT name,location,bought,warranty,comments FROM equipment");
$data = mysql_fetch_array($result);
?>
code here to display.
<?php
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
Just to be sure, have you checked the database directly to see if it updates? You are running the display before you run the update call, so it is going to display the same data until you load the page again. You should move your update code above this line:
$sql=“SELECT * FROM $tbl_name”;
If that doesn’t work, then you should check for an error or echo your query and make sure that it looks like it is supposed to.
On a side note:
header(“location:update_multiple.php”);
Is going to generate an error message, because you will be calling the header() function after output has been generated. Per the PHP documentation: “Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.”
It looks like, if this is the entire script, that you are depending on register_globals to be turned on. If that is the case, then you should start reworking your code now, because that is a function that is turned off by default now as it was a security risk.
As I suggested before, echo your query to see what query is actually being run when you are trying to update. That will, most likely, help you see why your records are not being updated.
You script probably assumes that register_globals is on. And then there is a serious flaw in your code: What does the variable $id contain? You did not pass the ids as hidden variable.
I think webdesignhouston had a right caught. The only problem I also doubt on is with that you are working in register_globals ON and the coding seems pretty old style.
Few suggestions:
Always use PHP full tag <?php not <?
Do not use multiple tags having same id (i.e. id=“name” it should be something like id=“name_<id>”).
See the update SQL statement, there is an error field name ‘bought’ and a comma after that are missing (location=‘$location[$i]’, =‘$bought[$i]’ warranty=‘$warranty[$i]’).
Use specific super global variables like POST, GET in respective cases. While updating you may need to do something like this (untested):
if($_SERVER['REQUEST_METHOD'] == 'POST'){ // ensure if the form was posted.
$errors = array();
$name = $_POST['name'];
$location = $_POST['location'];
$bought = $_POST['bought'];
$warranty = $_POST['warranty'];
$comments = $_POST['comments'];
$count = count($name);
for($i = 0; $i < $count; $i++){
$sql = "UPDATE $tbl_name SET
name='$name[$i]',
location='$location[$i]',
bought='$bought[$i]',
warranty='$warranty[$i]',
comments='$comments[$i]',
WHERE id='$id[$i]'";
if(!mysql_query($sql)){
$errors[] = "There was an error with '" . $name[$i] . "' with the error " . mysql_error();
}
}
if(count($errors)){
print_r($errors);
}
}
Always put this type of update/insert script on top of the page (not at the bottom of the page) and redirect to the same page or different after successful action. This will avoid the header already sent warnings.
Always terminate the script with die() or exit() after header location redirection.