Hi,
I am an MS Access user trying to move an application on the web with php and mysql.
I have two questions:
#1
I have followed a tutorial and was able to establish a page. The problem I am having at this time is not being able to save the uploaded file with the correct file name(with time() function added to the filename.) to Mysql. it only save it as the name of the original file. Can you see why that is from the code?
#2 I used to have it all working nice but was wanting to change the validation rule in order to make the picture upload an option. the way the tutorial went, it gave no option, you have to upload an image. any idea I could do this efficiently?
(I may have moved some of the else{} on the nested if s that I have. it gets quite hard to make up after you move stuff around. lol
I am an MS Access user trying to move an application on the web with php and mysql.
I have two questions:
#1
I have followed a tutorial and was able to establish a page. The problem I am having at this time is not being able to save the uploaded file with the correct file name(with time() function added to the filename.) to Mysql. it only save it as the name of the original file. Can you see why that is from the code?
#2 I used to have it all working nice but was wanting to change the validation rule in order to make the picture upload an option. the way the tutorial went, it gave no option, you have to upload an image. any idea I could do this efficiently?
(I may have moved some of the else{} on the nested if s that I have. it gets quite hard to make up after you move stuff around. lol
Code:
<body>
<?php
//DB Connection//
include_once ("../include/connect.php");
include_once ("../include/appvar.php");
//constant to upload pictures//
//define('EMP_UPLOADPATH',"../image/employee");
//check to see if the form has been set//
if (isset($_POST['submit'])) {
//Variables for employee form//
$employerid= mysqli_real_escape_string($dbc,trim($_POST['employerid']));
$jobtitleid= mysqli_real_escape_string($dbc, trim($_POST['jobtitleid']));
$firstname= mysqli_real_escape_string($dbc, trim($_POST['firstname']));
$lastname= mysqli_real_escape_string($dbc, trim($_POST['lastname']));
$address= mysqli_real_escape_string($dbc, trim($_POST['address']));
$city= mysqli_real_escape_string($dbc, trim($_POST['city']));
$province= mysqli_real_escape_string($dbc, trim($_POST['province']));
$country= mysqli_real_escape_string($dbc, trim($_POST['country']));
$postalcode= mysqli_real_escape_string($dbc, trim($_POST['postalcode']));
$phone= mysqli_real_escape_string($dbc, trim($_POST['phone']));
$email= mysqli_real_escape_string($dbc, trim($_POST['email']));
$employeecomment = mysqli_real_escape_string($dbc, trim($_POST['employeecomment']));
$employeepic = mysqli_real_escape_string($dbc, trim($_FILES['employeepic']['name']));
$employeepic_type = $_FILES['employeepic']['type'];
$employeepic_size = $_FILES['employeepic']['size'];
//Validate picture//
if ((!empty($employerid)) ||(!empty($firstname)) ||(!empty($lastname))) {
if ((($employeepic_type == 'image/gif') || ($employeepic_type == 'image/jpeg') || ($employeepic_type == 'image/pjpeg') || ($employeepic_type == 'image/png'))
&& ($employeepic_size > 0) && ($employeepic_size <= EMP_MAXFILESIZE)) {
if ($_FILES['employeepic']['error'] == 0) {
// Move the file to the target upload folder
$target = EMP_UPLOADPATH .time(). $employeepic ;
if (move_uploaded_file($_FILES['employeepic']['tmp_name'], $target)) {
//query to populate employee form//
$query = "INSERT INTO employee (employerid, jobtitleid, firstname, lastname, address, city, province, country, postalcode," .
"phone, email, employeecomment, employeepic) VALUES ('$employerid', '$jobtitleid', '$firstname', '$lastname'," .
" '$address', '$city', '$province', '$country', '$postalcode', '$phone', '$email','$employeecomment','$employeepic')";
$result = mysqli_query($dbc, $query);
mysqli_close($dbc);
}
}else {
echo '<p class="error">Sorry, there was a problem uploading your screen shot image.</p>';
}
}
}else{
echo '<p class="error"> It would be a good idea to add a first name and last name for this new employee. Let us not forget an employer too </p>';
}
}
?>
<div class="gridContainer clearfix">
<header class="fluid header"><h1>Shannon Falls Retirement Residence Database</h1></header>
<nav class="fluid nav">This is the content for Layout Nav Tag "nav"</nav>
<aside class="fluid aside">
<div class="fluid" id="hero"><?php if (isset($_POST['submit'])){ echo ' <h1>'. $firstname . ' ' .$lastname.'</h1>'; }?></div>
<div class="fluid emp_pic_frame"> <?php if (isset($_POST['submit'])){
echo '<img src="' . EMP_UPLOADPATH . $employeepic . '" alt="Score image" />';
}?></div><hr>
</aside>
<div class="fluid content">
<h2>Employees Details</h2><br /><hr>
<form enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name"MAX_FILE_SIZE" value="<?php echo EMP_MAXFILESIZE; ?>" />
<label for="employerid" class="label" >Employer:</label>
<input type="text" class="input" id="employerid" name="employerid" value="<?php if (!empty($employerid)) echo $employerid; ?>"/>
<label for="jobtitleid" class="label" >Job Title:</label>
<input type="text" class="input" id="jobtitleid" name="jobtitleid" value="<?php if (!empty($jobtitleid)) echo $jobtitleid; ?>"/>
<label for="firstname" class="label" >First Name:</label>
<input type="text" class="input" id="firstname" name="firstname" value="<?php if (!empty($firstname)) echo $firstname; ?>"/>
<label for="lastname" class="label" >Last Name:</label>
<input type="text" class="input" id="lastname" name="lastname" value="<?php if (!empty($lastname)) echo $lastname; ?>"/>
<label for="address" class="label" >Address:</label>
<input type="text" class="input" id="address" name="address" value="<?php if (!empty($address)) echo $address; ?>"/>
<label for="city" class="label" >City:</label>
<input type="text" class="input" id="city" name="city" value="<?php if (!empty($city)) echo $city; ?>"/>
<label for="province" class="label" >Province:</label>
<input type="text" class="input" id="province" name="province" value="<?php if (!empty($province)) echo $province; ?>"/>
<label for="country" class="label" >Country:</label>
<input type="text" class="input" id="country" name="country" value="<?php if (!empty($country)) echo $country; ?>"/>
<label for="postalcode" class="label" >Postal Code:</label>
<input type="text" class="input" id="postalcode" name="postalcode" value="<?php if (!empty($postalcode)) echo $postalcode; ?>"/>
<label for="phone" class="label" >Phone:</label>
<input type="text" class="input" id="phone" name="phone" value="<?php if (!empty($phone)) echo $phone; ?>"/>
<label for="email" class="label" >Email:</label>
<input type="text" class="input" id="email" name="email" value="<?php if (!empty($email)) echo $email; ?>"/><br /><br />
<label id="employeecommentlabel" for="employeecomment">Employee Comment:</label><br />
<textarea name="employeecomment" id="employeecomment" value="<?php if (!empty($employeecomment)) echo $employeecomment; ?>"></textarea><br />
<label id="employeepiclabel"for="employeepic">Employee Picture:</label>
<input type="file" name="employeepic" id="employeepic" /><hr>
<input id="submit" type="submit" name="submit" value="Save"/>
</form>
</div content>
<footer class="fluid footer">This is the content for Layout Footer Tag "footer"</footer>
</div container clearfix>
</body>
Comment