<?php 

 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $employeeno = mysql_real_escape_string(htmlspecialchars($_POST['txtempno']));
 $employeename = mysql_real_escape_string(htmlspecialchars($_POST['txtempname']));
 $position = mysql_real_escape_string(htmlspecialchars($_POST['txtpos']));
 $salary = mysql_real_escape_string(htmlspecialchars($_POST['txtsal']));
 $allottee = mysql_real_escape_string(htmlspecialchars($_POST['txtaname']));
 $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 $relation = mysql_real_escape_string(htmlspecialchars($_POST['txtremp']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['txtage']));
 
 // check to make sure both fields are entered
if ($firstname == '' || $lastname == '' ||$position=='' ||$salary=='' || $allottee=='' || $address=='' ||$relation=='' || $age=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($employeeno, $employeename, $position, $salary,$allottee,$address,$relation,$age, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT profiles SET employee no='$employeeno', employee name='$employeename', position='$position', salary='$salary', allottee name='$allottee', address='$address', relation='$relation', age='$age'")
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: admin_record2.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
 
?>

Kindly help me, in the closing tag of php "?>" it displays "Unexpected $end". Thanks

In line 31 the allottee name should be like this `allottee name` or remove the space between allottee and name.

it doesn't work :(

In which line did you get that error? Please repost your recent updated code....

On line 31 remove ALL spaces or parenthesis to them

Line 31 should look like this:

mysql_query("INSERT profiles SET 'employee no'='$employeeno', 'employee name'='$employeename', position='$position', salary='$salary', 'allottee name'='$allottee', address='$address', relation='$relation', age='$age'")

Your query is incorrect - you cannot have spaces in column names so you need to change the column names in your query to what they actually are in your database, either as all one word or with an underscore between. For example employee_no or employeeno (as I say it depends on your column names).

You are also missing INTO in the query;

INSERT INTO profiles SET

This may not be causing your problem but needs correcting.

<?php 

 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $employeeno = mysql_real_escape_string(htmlspecialchars($_POST['txtempno']));
 $employeename = mysql_real_escape_string(htmlspecialchars($_POST['txtempname']));
 $position = mysql_real_escape_string(htmlspecialchars($_POST['txtpos']));
 $salary = mysql_real_escape_string(htmlspecialchars($_POST['txtsal']));
 $allottee = mysql_real_escape_string(htmlspecialchars($_POST['txtaname']));
 $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 $relation = mysql_real_escape_string(htmlspecialchars($_POST['txtremp']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['txtage']));
 
 // check to make sure both fields are entered
if ($firstname == '' || $lastname == '' ||$position=='' ||$salary=='' || $allottee=='' || $address=='' ||$relation=='' || $age=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($employeeno, $employeename, $position, $salary,$allottee,$address,$relation,$age, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT INTO profiles SET 'employeeno'='$employeeno', 'employeename'='$employeename', position='$position', salary='$salary', 'allotteename'='$allottee', address='$address', relation='$relation', age='$age'")
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: admin_record2.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
?>

this is my updated code. Sorry for late reply.

In which line did you get that error? Please repost your recent updated code....

in line 43. "Parse error: syntax error, unexpected $end in C:\wamp\www\ONLINE_PAYROLL\admin_addrecord.php on line"

You also shouldn't have ' ' around your column names in the query (use nothing around them or ` `).

I think what is causing your error though as that you have a line break between your query and

or or die(mysql_error());

- code must all be on one line without line breaks before the semi-colon.

<?php 

 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $employeeno = mysql_real_escape_string(htmlspecialchars($_POST['txtempno']));
 $employeename = mysql_real_escape_string(htmlspecialchars($_POST['txtempname']));
 $position = mysql_real_escape_string(htmlspecialchars($_POST['txtpos']));
 $salary = mysql_real_escape_string(htmlspecialchars($_POST['txtsal']));
 $allottee = mysql_real_escape_string(htmlspecialchars($_POST['txtaname']));
 $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 $relation = mysql_real_escape_string(htmlspecialchars($_POST['txtremp']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['txtage']));
 
 // check to make sure both fields are entered
if ($firstname == '' || $lastname == '' ||$position=='' ||$salary=='' || $allottee=='' || $address=='' ||$relation=='' || $age=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($employeeno, $employeename, $position, $salary,$allottee,$address,$relation,$age, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT INTO profiles SET 'employeeno'='$employeeno', 'employeename'='$employeename', position='$position', salary='$salary', 'allotteename'='$allottee', address='$address', relation='$relation', age='$age'")
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: admin_record2.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
?>

this is my updated code. Sorry for late reply.

Your code is working fine here without showing any error.

<?php 

 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $employeeno = mysql_real_escape_string(htmlspecialchars($_POST['txtempno']));
 $employeename = mysql_real_escape_string(htmlspecialchars($_POST['txtempname']));
 $position = mysql_real_escape_string(htmlspecialchars($_POST['txtpos']));
 $salary = mysql_real_escape_string(htmlspecialchars($_POST['txtsal']));
 $allottee = mysql_real_escape_string(htmlspecialchars($_POST['txtaname']));
 $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 $relation = mysql_real_escape_string(htmlspecialchars($_POST['txtremp']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['txtage']));
 
 // check to make sure both fields are entered
if ($firstname == '' || $lastname == '' ||$position=='' ||$salary=='' || $allottee=='' || $address=='' ||$relation=='' || $age=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($employeeno, $employeename, $position, $salary,$allottee,$address,$relation,$age, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT INTO profiles SET employeeno='$employeeno', employeename='$employeename', position='$position', salary='$salary', allotteename='$allottee', address='$address', relation='$relation', age='$age'") or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header('Location:admin_record2.php'); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
?>

this is my updated code again, i change it what you've said. Same error

just add

<?php } ?> or <?php { ?>

to the end (almost the end) of your page, one at a time.this should solve it.

The error you are getting means you are missing a closing } somewhere so as I can see nowhere in your code that has this problem I would suggest checking your include file as well.

<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/
 
 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($employeeno, $employeename, $position, $salary,$allottee,$address,$relation,$age, $error)
 {
 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Record</title>
<style type="text/css">
<!--
.style1 {
	font-family: Verdana;
	font-weight: bold;
	font-size: 12px;
}
.style2 {font-size: 10px}
.style3 {
	font-size: 10px;
	font-family: Verdana;
}
.style4 {
	font-size: 10px;
	font-weight: bold;
	font-family: Verdana;
}
.style6 {font-size: 11px; font-weight: bold; font-family: Verdana; }
-->
</style>
</head>

<body>



<table width="1216" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
  <!--DWLayoutTable-->
  <tr>
    <td width="251" height="45">&nbsp;</td>
    <td width="678">&nbsp;</td>
    <td width="4">&nbsp;</td>
    <td width="283">&nbsp;</td>
  </tr>
  <tr>
    <td height="76">&nbsp;</td>
    <td colspan="2" valign="top"><img src="cfsharp/header.png" width="680" height="76" /></td>
    <td>&nbsp;</td>
  </tr>
  
  
  <tr>
    <td height="18"></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td height="402"></td>
    <td valign="top" bgcolor="#FFFFFF"><p class="style1">ADD NEW EMPLOYEE</p>
    
    <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
 
      <p class="style1">ALLOTMENT CORNER</p>      <form id="form1" name="form1" method="post" action="">
        <p class="style1"><span class="style2">&nbsp;Employee No:</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;       
          <label>
          <input name="txtempno" type="text" class="style3" id="txtempno" maxlength="50"/>
          </label>
        </p>
        <p class="style4">&nbsp;Employee Name:&nbsp; &nbsp; &nbsp;
          <label>
          <input name="txtempname" type="text" class="style3" id="txtempname" maxlength="50"/>
          </label>
        </p>
        <p class="style4">&nbsp;Position: &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;  
          <label></label>
          <label>
          <select name="txtpos" class="style3" id="txtpos">
            <option>Choose One</option>
            <option>Teppanyaki Cook</option>
            <option>Deck Assistant</option>
            <option>Electrician</option>
            <option>Pump Man</option>
            <option>Chief Officer</option>
            <option>2nd Officer</option>
            <option>3rd Officer</option>
            <option>Chief Engineering</option>
            <option>Master</option>
            <option>Chief Cooks</option>
            <option>Cooks</option>
            <option>Assistant Cook</option>
            <option>2nd Engineering</option>
            <option>3rd Engineering w/ 3 LIC</option>
            <option>3rd Engineering</option>
            <option>Engine Assistant</option>
          </select>
          </label>
        </p>
        <p class="style4">&nbsp;Salary: 
          <label>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="txtsal" type="text" class="style3" id="txtsal" />
          </label>
        </p>
        <p class="style4">&nbsp;Allottee Name: 
          <label>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input name="txtaname" type="text" class="style3" id="txtaname" maxlength="50"/>
          </label>
        </p>
        <p class="style4">&nbsp;Address: 
          <label>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <textarea name="address" id="address" cols="35" rows="3" class="style3" maxlength="60"></textarea>
          </label>
        </p>
        <p class="style4">&nbsp;Relation to employee: 
          <label></label>
          <label>
          <select name="txtremp" class="style3" id="txtremp">
            <option>Mother</option>
            <option>Father</option>
            <option>Sister</option>
            <option>Brother</option>
            <option>Cousin</option>
            <option>Nephew</option>
            <option>Brother-in-law</option>
            <option>Sister-in-law</option>
            <option>Grandfather</option>
            <option>Grandmother</option>
            <option>Son</option>
            <option>Daughter</option>
          </select>
          </label>
        </p>
        <p class="style4">&nbsp;Age:
          <label>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input name="txtage" type="text" class="style3" id="txtage" maxlength="2"/>
          </label>
        </p>
        <p align="right" class="style4">
          <label>
          <input type="submit" name="button" id="button" value="Next" />
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          </label>
        </p>
        <label></label>
      </form></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td height="459"></td>
    <td>&nbsp;</td>
    <td></td>
    <td></td>
  </tr>
</table>


<?php
}
 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 	// get form data, making sure it is valid
 	$employeeno = mysql_real_escape_string(htmlspecialchars($_POST['txtempno']));
 	$employeename = mysql_real_escape_string(htmlspecialchars($_POST['txtempname']));
 	$position = mysql_real_escape_string(htmlspecialchars($_POST['txtpos']));
 	$salary = mysql_real_escape_string(htmlspecialchars($_POST['txtsal']));
 	$allottee = mysql_real_escape_string(htmlspecialchars($_POST['txtaname']));
 	$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 	$relation = mysql_real_escape_string(htmlspecialchars($_POST['txtremp']));
 	$age = mysql_real_escape_string(htmlspecialchars($_POST['txtage']));
 
		 // check to make sure both fields are entered
		if ($firstname == '' || $lastname == '' ||$position=='' ||$salary=='' || $allottee=='' || $address=='' ||$relation=='' || $age=='')
 		{
 			// generate error message
 			$error = 'ERROR: Please fill in all required fields!';
 
 			// if either field is blank, display the form again
 			renderForm($employeeno, $employeename, $position, $salary,$allottee,$address,$relation,$age, $error);
 		}
 		else
 		{
			 // save the data to the database
 			mysql_query("INSERT INTO profiles SET employeeno='$employeeno', employeename='$employeename', position='$position', 	salary='$salary', allotteename='$allottee', address='$address', relation='$relation', age='$age'") or die(mysql_error()); 
 
 			// once saved, redirect back to the view page
			 header('Location:admin_record2.php'); 
		 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
?>

this are the codes.

:?:

You need to go through your code and count the number of opening { and closing } (indentation helps) and make sure you have the same number of each

You need to go through your code and count the number of opening { and closing } (indentation helps) and make sure you have the same number of each

it's working. i just replace this

else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','','');
 }
?>

but, it's not adding into the database

it's working. i just replace this

else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','','');
 }
?>

but, it's not adding into the database

Just echo out your insert query and check the printed query with Phpmyadmin's SQL menu.

On line 31 remove ALL spaces or parenthesis to them

Line 31 should look like this:

mysql_query("INSERT profiles SET 'employee no'='$employeeno', 'employee name'='$employeename', position='$position', salary='$salary', 'allottee name'='$allottee', address='$address', relation='$relation', age='$age'")

I think you forgot to add

WHERE id='$id'
<?php 

 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $employeeno = mysql_real_escape_string(htmlspecialchars($_POST['txtempno']));
 $employeename = mysql_real_escape_string(htmlspecialchars($_POST['txtempname']));
 $position = mysql_real_escape_string(htmlspecialchars($_POST['txtpos']));
 $salary = mysql_real_escape_string(htmlspecialchars($_POST['txtsal']));
 $allottee = mysql_real_escape_string(htmlspecialchars($_POST['txtaname']));
 $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 $relation = mysql_real_escape_string(htmlspecialchars($_POST['txtremp']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['txtage']));
 
 // check to make sure both fields are entered
if ($firstname == '' || $lastname == '' ||$position=='' ||$salary=='' || $allottee=='' || $address=='' ||$relation=='' || $age=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($employeeno, $employeename, $position, $salary,$allottee,$address,$relation,$age, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT INTO profiles SET 'employeeno'='$employeeno', 'employeename'='$employeename', position='$position', salary='$salary', 'allotteename'='$allottee', address='$address', relation='$relation', age='$age'")
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: admin_record2.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
?>

this is my updated code. Sorry for late reply.

what is this to do, insert or update?

mysql_query("INSERT INTO profiles SET 'employeeno'='$employeeno', 'employeename'='$employeename', position='$position', salary='$salary', 'allotteename'='$allottee', address='$address', relation='$relation', age='$age'")

from the code, its for an update.Well, if for update then it should be like this

mysql_query("UPDATE profiles SET 'employeeno'='$employeeno', 'employeename'='$employeename', position='$position', salary='$salary', 'allotteename'='$allottee', address='$address', relation='$relation', age='$age' WHERE id='$id'")

see this eg.

$sql = "UPDATE parts SET 
		 name_part = '$name_part',p_no = '$p_no',m_type = '$m_type',man = '$man',vendor = '$vendor',qt = '$qt',price = '$price',dr_optor = '$dr_optor',remarks = '$remarks' WHERE id='$id'";	 
		 mysql_query($sql);

have yu been able to solve this?

unexpected $end

move "}" on line 37 to line 27... there i hope that works...

check 'connect-db.php' page. problem may be on that page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.