Jump to content

PHP Edit form help


PRodgers4284

Recommended Posts

I am having difficulty with an edit record form, i want the records to be posted to an edit form page but i cant seem to get it to post the records to the form. I have a view form which using a while loop to display all the record that a user has entered and i then i have a edit link "<a href="editjob.php?username=$username&&id=$id">" which selects the record bases on the username and id of the record. Once the link is selected it goes the the editjob.php page which posts the data into the form, but i cant seem to get this to work. Can anyone please help or provide some advice, i would really appreciate it.

 

My code for the editjob.php is:

 

<?php  

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

$error_stat = 0;  
$jobtitle_message = ''; 
$jobcatergory_message = ''; 
$joblocation_message = ''; 
$employmenttype_message = ''; 
$salary_message = ''; 
$date_message = ''; 
$educationallevel_message = ''; 
$description_message = ''; 
$filesize_message = '';
$filetype_message = '';


$jobtitle = trim($_POST['jobtitle']);  
$jobcatergory = trim($_POST['jobcatergory']);  
$joblocation = trim($_POST['joblocation']); 
$employmenttype = trim($_POST['employmenttype']);  
$salary = trim($_POST['salary']);  
$date = trim($_POST['date']);  
$educationallevel = trim($_POST['educationallevel']);  
$description = trim($_POST['description']); 



//Error checking  



// Job Title check)  
if (!$jobtitle) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a username 
    $jobtitle_message = '*Please enter a job title*'; 
} 

else if (ctype_digit($jobtitle)) {  
   $error_stat = 1;  
   $jobtitle_message .= '*Invalid Job Title*';  
}  

else if ( preg_match( '/\W/', $jobtitle)){ 
     $error_stat = 1;  
    $jobtitle_message = '*Invalid jobtitle, letters only, no spaces*';  

}  

$jobtitle = $_POST['jobtitle'];  
$jobtitle = trim($jobtitle);  

   if (strlen($jobtitle) > 30){  
   $error_stat = 1;  
   $jobtitle_message = '*Job Title must be 20 characters or less*';  
}  






//  Job Catergory Check)   
if ($jobcatergory == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $jobcatergory_message = '*Please select a Job Catergory*'; 
} 





//    Job Location Check)   
if ($joblocation == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $joblocation_message = '*Please select a Job location*'; 
} 


// Employment Type Check)   
if ($employmenttype == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $employmenttype_message = '*Please select Employment type*'; 
} 





// Salary check)  
if (!$salary) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a username 
$salary_message = '*Please enter job salary*'; 
} 

else if (!ctype_digit($salary)) {  
   $error_stat = 1;  
   $salary_message .= '*Invalid salary*';  
} 




//Date check)  
if (empty($date)) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a dob 
$date_message = '*Please enter job closing date*'; 
} 

//Check the format and explode into $parts 
  elseif (!ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$",  
          $date, $parts)){ 
     $error_stat = 1;      

//Set the message to tell the user the date is invalid 
$date_message = '*Invalid date, must be DD/MM/YYYY format*'; 
} 
     
  elseif (!checkdate($parts[2],$parts[1],$parts[3])) 
  { 
  $error_stat = 1;  
   
  //Set the message to tell the date is invalid for the month entered 
    $date_message = '*Invalid date, month must be between 1-12*'; 
} 
   

// Job Description check)  
if (!$description) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a username 
    $description_message = '*Please enter a job description*'; 
} 

$description = $_POST['description'];  
$description = trim($description);  

   if (strlen($description) > 150){  
   $error_stat = 1;  
   $description_message = '*Job Title must be 150 characters or less*';  
}  

   
   
   
//    Educational Level Check)   
if ($educationallevel == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $educationallevel_message = '*Please select Educational level required*'; 
} 


if( $_FILES['userfile']['size'] > 2000000 ){
    //Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;
$filesize_message = '*Filesize too large *';

}

$fileTypes = array("application/pdf", "application/msword");

if( !in_array("{$_FILES['userfile']['type']}", $fileTypes) ){
    $error_stat = 1;
$filetype_message = '*Filetype not allowed *';

}




$uploadDir = 'applicationforms/';  

if (isset($_POST['submit']) && $error_stat == 0) { 


    $fileName = $_FILES['userfile']['name']; 
    $tmpName  = $_FILES['userfile']['tmp_name']; 
    $fileSize = $_FILES['userfile']['size']; 
    $fileType = $_FILES['userfile']['type']; 

    // the files will be saved in filePath  
    $filePath = $uploadDir . $fileName; 

    // move the files to the specified directory 
    // if the upload directory is not writable or 
    // something else went wrong $result will be false 
    $result    = move_uploaded_file($tmpName, $filePath); 
     
     
    include("database.php"); 

    if(!get_magic_quotes_gpc()) 
    { 
        $fileName  = addslashes($fileName); 
        $filePath  = addslashes($filePath); 
    }   
     
     
 mysql_query("UPDATE job SET username='" . $_POST["username"] . "',jobtitle='" . $_POST["jobtitle"] . "',jobcatergory='" . $_POST["jobcatergory"] . "',joblocation='" . $_POST["joblocation"] . "',employmenttype='" . $_POST["employmenttype"] . "',salary='" . $_POST["salary"] . "',date='" . $_POST["date"] . "',educationallevel='" . $_POST["educationallevel"] . "',description='" . $_POST["description"] . "',name='$fileName', type='$fileType', size='$fileSize', path='$filePath' WHERE username='" . $_SESSION["username"] . "'");  
?>  
           
<br /> 
<a href="index.php">Back to main page</a> 
<br /> 
<br /> 
<br /> 
You have successfully updated the Job record. 
<?php  
}  
else  
{  
   $account = mysql_fetch_array(mysql_query("SELECT * FROM job WHERE username='" . $_SESSION["username"] . "'"));  
?>  

Link to comment
Share on other sites

I have managed to get the code to post the first record in the database assigned to the user that is logged in. When i select a different record in the view page it displays the first record data in the edit form, i want the code to display the data of the record that has been selected from the view record page. Can anyone help?

Link to comment
Share on other sites

I cant seem to get the code to display the record in the edit page from the record id selected in the viewjob page, the edit link on the view job page is:

 

<a href="editjob.php?username=$username&&id=$id">Edit/Update Job</a>

 

My code for the edit page is:

 

<?php  
if (isset($_POST['submit'])) { 

$username = $_GET['username'];
$id = $_GET['id'];

$error_stat = 0;  
$jobtitle_message = ''; 
$jobcatergory_message = ''; 
$joblocation_message = ''; 
$employmenttype_message = ''; 
$salary_message = ''; 
$date_message = ''; 
$educationallevel_message = ''; 
$description_message = ''; 
$filesize_message = '';
$filetype_message = '';

$jobtitle = trim($_POST['jobtitle']);
$jobcatergory = trim($_POST['jobcatergory']);
$joblocation = trim($_POST['joblocation']);
$employmenttype = trim($_POST['employmenttype']);
$salary = trim($_POST['salary']);
$date = trim($_POST['date']);
$educationallevel = trim($_POST['educationallevel']);
$description = trim($_POST['description']);

//Error checking  



// Job Title check)  
if (!$jobtitle) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a username 
    $jobtitle_message = '*Please enter a job title*'; 
} 

else if (ctype_digit($jobtitle)) {  
   $error_stat = 1;  
   $jobtitle_message .= '*Invalid Job Title*';  
}  

else if ( preg_match( '/\W/', $jobtitle)){ 
     $error_stat = 1;  
    $jobtitle_message = '*Invalid jobtitle, letters only, no spaces*';  

}  

$jobtitle = $_POST['jobtitle'];  
$jobtitle = trim($jobtitle);  

   if (strlen($jobtitle) > 30){  
   $error_stat = 1;  
   $jobtitle_message = '*Job Title must be 20 characters or less*';  
}  






//  Job Catergory Check)   
if ($jobcatergory == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $jobcatergory_message = '*Please select a Job Catergory*'; 
} 





//    Job Location Check)   
if ($joblocation == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $joblocation_message = '*Please select a Job location*'; 
} 


// Employment Type Check)   
if ($employmenttype == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $employmenttype_message = '*Please select Employment type*'; 
} 





// Salary check)  
if (!$salary) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a username 
$salary_message = '*Please enter job salary*'; 
} 

else if (!ctype_digit($salary)) {  
   $error_stat = 1;  
   $salary_message .= '*Invalid salary*';  
} 




//Date check)  
if (empty($date)) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a dob 
$date_message = '*Please enter job closing date*'; 
} 

//Check the format and explode into $parts 
  elseif (!ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$",  
          $date, $parts)){ 
     $error_stat = 1;      

//Set the message to tell the user the date is invalid 
$date_message = '*Invalid date, must be DD/MM/YYYY format*'; 
} 
     
  elseif (!checkdate($parts[2],$parts[1],$parts[3])) 
  { 
  $error_stat = 1;  
   
  //Set the message to tell the date is invalid for the month entered 
    $date_message = '*Invalid date, month must be between 1-12*'; 
} 
   

// Job Description check)  
if (!$description) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a username 
    $description_message = '*Please enter a job description*'; 
} 

$description = $_POST['description'];  
$description = trim($description);  

   if (strlen($description) > 150){  
   $error_stat = 1;  
   $description_message = '*Job Title must be 150 characters or less*';  
}  

   
   
   
//    Educational Level Check)   
if ($educationallevel == 'Please Select'){ 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 
    $educationallevel_message = '*Please select Educational level required*'; 
} 


if( $_FILES['userfile']['size'] > 2000000 ){
    //Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;
$filesize_message = '*Filesize too large *';

}

$fileTypes = array("application/pdf", "application/msword");

if( !in_array("{$_FILES['userfile']['type']}", $fileTypes) ){
    $error_stat = 1;
$filetype_message = '*Filetype not allowed *';

}




$uploadDir = 'applicationforms/';  

if (isset($_POST['submit']) && $error_stat == 0) { 


    $fileName = $_FILES['userfile']['name']; 
    $tmpName  = $_FILES['userfile']['tmp_name']; 
    $fileSize = $_FILES['userfile']['size']; 
    $fileType = $_FILES['userfile']['type']; 

    // the files will be saved in filePath  
    $filePath = $uploadDir . $fileName; 

    // move the files to the specified directory 
    // if the upload directory is not writable or 
    // something else went wrong $result will be false 
    $result    = move_uploaded_file($tmpName, $filePath); 
     
     
    include("database.php"); 

    if(!get_magic_quotes_gpc()) 
    { 
        $fileName  = addslashes($fileName); 
        $filePath  = addslashes($filePath); 
    }   
}    
    
    

 mysql_query("UPDATE job SET username='" . $_POST["username"] . "',jobtitle='" . $_POST["jobtitle"] . "',joblocation='" . $_POST["joblocation"] . "',employmenttype='" . $_POST["employmenttype"] . "',salary='" . $_POST["salary"] . "',date='" . $_POST["date"] . "',educationallevel='" . $_POST["educationallevel"] . "',description='" . $_POST["description"] . "', name='$fileName', type='$fileType', size='$fileSize', path='$filePath' WHERE username='" . $_SESSION["username"] . "'");  
?>  
           
<br /> 
<a href="index.php">Back to main page</a> 
<br /> 
<br /> 
<br /> 
The Job record has been successfully updated. 
<?php  
}  
else  
{  
$account = mysql_fetch_array(mysql_query("SELECT * FROM job WHERE username='$username' AND id='$id'")); 
?>  
<form method="post" class="addform" action="" enctype="multipart/form-data"> 
<fieldset>  
<label for="cvtitle">Edit Job</label><fieldset> 
<p align="right"> </p>
</fieldset> 
<label for="username">Username:</label>  
<input readonly name="username" type="text" id="username" value="<?php echo $_SESSION["username"]; ?>" /><br />  
</fieldset>  

<hr class="hr_blue"/>

<fieldset> 
<label for="jobtitle">Job Title:</label> 
<input name="jobtitle" type="text" id="jobtitle" value="<?php echo $account['jobtitle']; ?>"/> 
<span class="redboldtxt"><?php echo "$jobtitle_message";?></fieldset></span>


<fieldset>    
<label for="jobcatergory">Job Catergory:</label><p></p>   
<select name="jobcatergory"> 
<option value="Please Select">Please Select</option> 
<?php 
  $jobcatergory_opts = array( 
    "Accountancy and Finance", 
    "Banking and Insurance", 
    "Construction", 
    "Customer Service", 
    "Engineering", 
    "Management",
    "Hotel and Catering", 
    "Information Technology",
    "Legal",
    "Marketing", 
    "Medical",
    "Retail",
    "Sales",
    "Secretarial",
    "Transport and Distribution",
    "Working from home",        
  ); 
  foreach($jobcatergory_opts as $opt){
    $selected = $account['jobcatergory'] == $opt ? " selected=true":"";
    print "<option value=\"{$opt}\"{$selected}>{$opt}</option>";
  }
?>
</select>
<span class="redboldtxt"><?php echo "$jobcatergory_message";?><?php echo $error['jobcatergory']; ?></span></fieldset> 

<fieldset> 
<label for="joblocation">Location:</label> 
<p></p>
<select name="joblocation">
<option value="Please Select">Please Select</option>
<?php
  $joblocation_opts = array(
    "Co.Antrim",
    "Co.Armagh",
    "Co.Down",
    "Co.Fermanagh",
    "Co.Londonderry",
    "Co.Tyrone",
  );
  foreach($joblocation_opts as $opt){
    $selected = $account['joblocation'] == $opt ? " selected=true":"";
    print "<option value=\"{$opt}\"{$selected}>{$opt}</option>";
  }
?>
</select>
<span class="redboldtxt"><?php echo "$joblocation_message";?><?php echo $error['joblocation']; ?></span></fieldset>  


<fieldset>  
<label for="employmenttype">Job Type:</label><p></p>  
<select name="employmenttype"> 
<option value="Please Select">Please Select</option> 
<?php 
  $employmenttype_opts = array( 
    "permanent fulltime", 
    "permanent parttime", 
    "temporary fulltime", 
    "temporary parttime", 
  ); 
  foreach($employmenttype_opts as $opt){
    $selected = $account['employmenttype'] == $opt ? " selected=true":"";
    print "<option value=\"{$opt}\"{$selected}>{$opt}</option>";
  }
?>
</select>
<span class="redboldtxt"><?php echo "$employmenttype_message";?><?php echo $error['employmenttype']; ?></span></fieldset> 


<fieldset> 
<label for="salary">Salary:</label> 
<input name="salary" type="text" id="salary" value="<?php echo $account['salary']; ?>"/> 
<span class="redboldtxt"><?php echo "$salary_message";?></span></fieldset>

<fieldset> 
<label for="date">Closing Date:</label> 
<input name="date" type="text" id="date" value="<?php echo $account['date']; ?>"/> 
<span class="redboldtxt"><?php echo "$date_message";?></span></fieldset> 


<fieldset>  
<label for="educationallevel">Qualification Level Required:</label><p></p>   
<select name="educationallevel"> 
<option value="Please Select">Please Select</option> 
<?php 
  $educationallevel_opts = array( 
    "GCSE", 
    "A-Level", 
    "Third Level Certification", 
    "Third Level Diploma", 
    "Third Level Degree", 
    "Post Graduate Qualification",
    "Masters",
    "PHD",
    "Professional Qualification",
    "Part Professional Qualification",
    "Trade Qualification", 
  ); 
  foreach($educationallevel_opts as $opt){
    $selected = $account['educationallevel'] == $opt ? " selected=true":"";
    print "<option value=\"{$opt}\"{$selected}>{$opt}</option>";
  }
?>
</select>
<span class="redboldtxt"><?php echo "$educationallevel_message";?><?php echo $error['educationallevel']; ?></span></fieldset> 

<hr class="hr_blue"/>

<fieldset> 
Job Description -<br /> 
</fieldset> 

<fieldset> 
<textarea rows="2" name="description" cols="20"><?php echo $account["description"]; ?></textarea><p></p> 
<span class="redboldtxt"><?php echo "$description_message";?></span></fieldset> 

Upload Application Form</label> 
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
<span class="redboldtxt"><?php echo "$filesize_message";?></span>
<span class="redboldtxt"><?php echo "$filetype_message";?></span>
</fieldset>

<p></p>
<fieldset> 
<p class="submit"><input type="submit" name="submit" value="Edit Job" />

</fieldset>
</form> 


 

Can anyone help?

Link to comment
Share on other sites

Is that a PHP echo or are you doing this in HTML?  Also, you only use 1 & not 2.

 

I cant seem to get the code to display the record in the edit page from the record id selected in the viewjob page, the edit link on the view job page is:

 

<a href="editjob.php?username=$username&&id=$id">Edit/Update Job</a>

 

Link to comment
Share on other sites

Is that a PHP echo or are you doing this in HTML?  Also, you only use 1 & not 2.

 

I cant seem to get the code to display the record in the edit page from the record id selected in the viewjob page, the edit link on the view job page is:

 

<a href="editjob.php?username=$username&&id=$id">Edit/Update Job</a>

 

 

Hey thanks for the reply

 

The edit job link is inside a form in the view job page

 

My code for the view job page is:

 

<?php   

$sessid = $_SESSION["username"] ;

$query = "SELECT * FROM job WHERE username='" . $sessid. "'";


$result = mysql_query($query);
if(!$result){
   // check if is something wrong
   print "Error";
}else{
if(mysql_num_rows($result) == 0){  
     print "No Jobs Exist";
}else{
while ($account = @mysql_fetch_array($result)) { 
    $id=$account["id"]; 
    $jobtitle=$account["jobtitle"]; 
    $jobcatergory=$account["jobcatergory"]; 
   ?>
   <form class="jobform" action="">  
<fieldset>  
<p class="edit"> 
</fieldset> 
<fieldset>  
<label for="jobtitle">Job Title:</label>  
<input readonly name="jobtitle" type="text" id="jobtitle" value="<?php echo $jobtitle ?>" /><br />  
</fieldset>
<fieldset style="width: 602; height: 58">  
<label for="jobcatergory">Job Catergory:</label>  
<input readonly name="jobcatergory" type="text" id="jobcatergory" value="<?php echo $jobcatergory?>" />  
</fieldset> 
<fieldset>  
<table border="0" align=right width="40%" id="table14">
<tr>
	<td align="right"><span class="navyboldtxt"><p align="right"> <a href="editjob.php?username=$username&&id=$id">Edit/Update Job</a></p></td>
	<td align="right"><span class="navyboldtxt"><p align="right"> <a href="editjob.php?username=$username&&id=$id">Delete Job</a></p>
</td>
</tr>
</table>
</fieldset> 
</form>  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.