I am very new to PHP;
ADD.PHP – I have a form that collects the following information 1. name 2. email 3. phone and picture
pictures are stored on a directory folder on my server and then the filename of that photo is stored on my sql table.
VIEW.PHP – all the data in mysql is being displayed in this page including the photo in tabular format including the id of every record. The id being display is a hyperlink in which when clicked you will be directed to a page wherein you can edit the record contents:
below is the code for my EDIT.PHP
<?php
// Connects to your Database
mysql_connect("localhost", "user1", "12345") or die(mysql_error()) ;
mysql_select_db("test") or die(mysql_error()) ;
// Check whether the value for jobnumber is transmitted
if (isset($_GET['id'])) {
// Put the value in a separate variable
$id = $_GET['id'];
// Query the database for the details of the chosen jobnumber
$result = mysql_query("select id, name, email,
phone, picture from employees where id = $id");
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = "Invalid query: " . mysql_error() . "\n";
$message .= "Whole query: " . $query;
die($message);
}
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(),etc.
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
echo $name. "\n";
echo $row['email'] . "\n";
echo $row['phone'] . "\n";
echo "<img width=500px height=500px src=pics/" . $row['picture'].">" . "\n";
// form tag
echo '<form action="add2.php" method="POST">';
//display name
echo 'Name: <input type="text" name="name" value="';
echo $row['name'];
echo '"><br>';
//display email
echo 'email: <input type="text" name="email" value="';
echo $row['email'];
echo '"><br>';
//display phone
echo 'Phone: <input type="text" name="phone" value="';
echo $row['phone'];
echo '"><br>';
//display photo
echo 'Photo: <input type="text" name="photo" value="';
echo $row['picture'];
echo '"><br>';
echo '<input type="submit" value="Add">';
echo '</form>';
}
} else {
die("No valid data specified!");
}
?>
using this code, the test fields went well but the input box for the photo is blank and when i click the button the photo field in my database will be blank unless i uploaded a new photo?
how can the user change the existing photo? or retain the old photo if not being changed?
Assuming your
employees.picturecolumn stores the path to the image.To upload a new photo, you’re going to need to change a couple of things…
Your form needs to use the correct encoding type, ie
You also need to provide a “file” input element to accept the new photo, eg
To see if a new photo has been supplied, simply check (after detecting a valid POST request)