Monday 27 June 2016

Second form submit button seems to be skipped(?) PHP / HTML



I need help about this, I have a PHP page, which searches for records, based on their first and last names, if sql finds the data then the second form comes out, which has lots of textboxes, to update the 'searched'information. And when I click the submit button of the second form , it does nothing, and even if I have syntax or whatever errors I have put on condition if(isset($_POST['submit'])), they end up being disregarded (no error messages will come up), after clicking the submit button, it just goes back to the page's original state, when it has to update the record I have searched for that was just edited. What exactly is the mistake on this part?



class.php - .php file that contains the operations for sql





$months = array('January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',

'September',
'October',
'November',
'December');

class EmployeeProfile {


public
function openConnection() {

$conn = mysqli_connect("localhost", "root", "", "db_employee");

if (mysqli_connect_errno()) {
echo "Failed to connect to database server";
}


return $conn;

}


public
function insert($query) {

if (mysqli_query($this - > openConnection(), $query) == 1) {
echo "Profile successfully registered!";
} else {
echo "Register failed";
}


}

public
function display($query) {

$result = mysqli_query($this - > openConnection(), $query);
echo "

";
if ($result - > num_rows == 1) {
while ($row = $result - > fetch_assoc()) {
echo "";

echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";

echo "";
echo "";
echo "
First Name: ".$row["firstname"]."
Middle Name: ".$row["middlename"]."
Last Name: ".$row["lastname"]."
Date of Birth: ".$row["dateofbirth"]."
Age: ".$row["age"]."
School: ".$row["school"]."
Highest Educational Attainment: ".$row["educ"]."
Year Last Attended: ".$row["yearattended"]."
Skills: ".$row["skills"]."
Previous Company: ".$row["prevcompany"]."
Position: ".$row["position"]."
Date of Employment: ".$row["dateofemployment"]."
";
}
} else

{

echo "Profile not found";
}


}

public
function edit($query) {

$result = mysqli_query($this - > openConnection(), $query);

}


}

?>


edit.php - the page itself.




Edit Profile








Enter first or last name


include("class.php");

if(isset($_POST['search2'])):

$status = "hidden";
$query = "select * from employee WHERE firstname='".$_POST['search']."' OR lastname='".$_POST['search']."' ";
$emp = new EmployeeProfile();
$emp->openConnection();

$result = mysqli_query($emp->openConnection(), $query);



if($result->num_rows == 1):




?>

































if(isset($_POST['submit'])):
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];

$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];
$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];


$row = $result->fetch_assoc();
$usr = $row["firstname"];


$query2 = "UPDATE employee SET firstname='$firstname', middlename='$middlename', lastname='$lastname', dateofbirth='$dateofbirth', age='$age', school='$school',
educ='$educ', yearattended='$yearattended', skills='$skills', prevcompany='$prevcompany', position='$position', dateofemployment='$dateofemployment',
WHERE firstname='$usr'";

mysqli_query($emp->openConnection(), $query2);


endif;





else:
echo "Profile not found";
endif;

endif;


?>

Edit your profile:
*Enter first name:
Enter middle name:
*Enter last name:
*Date of Birth:
*Age:
*School:
*Highest Educational Attainment:
*Year Last Attended:
*Skill(s):
Previous Company:
Position:
*Date of Employment:
* - Required







and I do really think that this line and beyond gets ignored.



This is part of the edit.php file that is shown above.




if(isset($_POST['submit'])):
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];

$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];
$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];


$row = $result->fetch_assoc();
$usr = $row["firstname"];


$query2 = "UPDATE employee SET firstname='$firstname', middlename='$middlename', lastname='$lastname', dateofbirth='$dateofbirth', age='$age', school='$school',
educ='$educ', yearattended='$yearattended', skills='$skills', prevcompany='$prevcompany', position='$position', dateofemployment='$dateofemployment',
WHERE firstname='$usr'";

mysqli_query($emp->openConnection(), $query2);



endif;

Answer



In general, there are two kinds of errors present.




  1. HTML tag order errors, which are extensive

  2. Syntax errors.
    - > must be -> to be properly parsed
    must be to be properly parsed




The syntax errors are present in both files.



Note: the following code contains some debug statements.





echo "

In myquery.php header

";

error_reporting(E_ALL);
//echo "

" . var_dump($_POST); . "

";
//echo "

" . var_dump($_GET); . "

";
?>

include("class.php");
?>

Edit Profile




echo "

In myquery.php body

";
?>








Enter first or last name






echo "

about to check _post for search2

";

if(isset($_POST['search2'])):
echo "

found _post for search2

";

$status = "hidden";
$query = "select * from employee WHERE firstname='".$_POST['search']."' OR lastname='".$_POST['search']."' ";
echo "

about to open DB

";
$emp = new EmployeeProfile();
$emp->openConnection();

echo "

about to place find query

";
$result = mysqli_query($emp->openConnection(), $query);

echo "

about to check for successful query

";
if($result->num_rows == 1):

echo "

successful search query

";
?>





























Edit your profile:
*Enter first name:
Enter middle name:
*Enter last name:
*Date of Birth:
*Age:
*School:
*Highest Educational Attainment:
*Year Last Attended:
*Skill(s):
Previous Company:
Position:
*Date of Employment:
* - Required



echo "

about to check for submit second form

";


if(isset($_POST['submit'])):

echo "

found submit for second form

";

$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];

$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];

$row = $result->fetch_assoc();
$usr = $row["firstname"];



$query2 =
"UPDATE employee
SET firstname='$firstname',
middlename='$middlename',
lastname='$lastname',
dateofbirth='$dateofbirth',
age='$age',
school='$school',
educ='$educ',

yearattended='$yearattended',
skills='$skills',
prevcompany='$prevcompany',
position='$position',
dateofemployment='$dateofemployment',
WHERE firstname='$usr'";

echo "

about to update DB

";

mysqli_query($emp->openConnection(), $query2);

endif;
else:
echo "

search query failed

";

echo "Profile not found";
endif;
endif;
?>





No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...