Hi Everybody,
I am very new in php coding. I tried to apply the knowledge I got and created a
form that would allow visitors to fill in their data and
displayed it and when there is mistake they could go back to the form and make
correction with all the filled data still displayed. This worked well. However, my
problem has to do with uploadig picture and include on the displayed page.It only shows X while other information displayed. Meanwhile a random number is inserted into the photo field in the database any time this transaction is carried out.
Another problem is that the "id" number increases whenever the displayed page
is refreshed and this is reflected in the database. The implication of this is that a
visitor can make multiple entries into the database instead of one. This is not what I intend to achieve with the form. I hope somebody can help me solve this problem. Great thanks in anticipation.
The codes are below:
index4.php(form)
<?php
session_start();
if(!isset($_SESSION['Fname'])) {
echo "
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method=\"post\" action=\"http://localhost/army/preview4.php\">
First Name:<input type=\"text\" size=\"12\" maxlength=\"12\" name=\"Fname\"><br />
Last Name:<input type=\"text\" size=\"12\" maxlength=\"36\" name=\"Lname\"><br />
Gender:<br />
Male:<input type=\"radio\" value=\"Male\" name=\"gender\"><br />
Female:<input type=\"radio\" value=\"Female\" name=\"gender\"><br />
Select a Level of Education:<br />
<select name=\"education\">
<option value=\"Jr.High\">Jr.High</option>
<option value=\"HighSchool\">HighSchool</option>
<option value=\"College\">College</option></select><br />
Select your favorite time of day:<br />
<select name=\"TofD\" size=\"3\">
<option value=\"Morning\">Morning</option>
<option value=\"Day\">Day</option>
<option value=\"Night\">Night</option></select><br />
<input type=\"submit\" value=\"submit\" name=\"submit\">
</form>
</body></html>";
} else {
$Fname = $_SESSION['Fname'];
$Lname = $_SESSION['Lname'];
$gender = $_SESSION['gender'];
$quote = $_SESSION['quote'];
$education = $_SESSION['education'];
$TofD = $_SESSION['TofD'];
echo "
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method=\"post\" action=\"http://localhost/army/preview4.php\">
First Name:<input type=\"text\" size=\"12\" maxlength=\"12\" name=\"Fname\" value=\"$Fname\"><br />
Last Name:<input type=\"text\" size=\"12\" maxlength=\"36\" name=\"Lname\" value=\"$Lname\"><br />
Gender:<br /> ";
if($gender=="Male") {
echo "Male:<input type=\"radio\" value=\"Male\" name=\"gender\" checked><br /> ";
} else {
echo "Male:<input type=\"radio\" value=\"Male\" name=\"gender\"><br />";
}
if($gender=="Female") {
echo "Female:<input type=\"radio\" value=\"Female\" name=\"gender\" checked><br />";
} else {
echo "Female:<input type=\"radio\" value=\"Female\" name=\"gender\"><br />";
}
echo "
Select a Level of Education:<br />
<select name=\"education\">";
if($education=="Jr.High") {
echo "<option selected value=\"Jr.High\">Jr.High</option>";
} else {
echo "<option value=\"Jr.High\">Jr.High</option>";
}
if($education=="HighSchool") {
echo "<option selected value=\"HighSchool\">HighSchool</option>";
} else {
echo " <option value=\"HighSchool\">HighSchool</option>";
}
if($education=="College") {
echo "<option selected value=\"College\">College</option></select><br />";
} else {
echo "<option value=\"College\">College</option></select><br />";
}
echo "</select><p>
Select your favorite time of day:<br />
<select name=\"TofD\" size=\"3\"> ";
if($TofD=="Morning") {
echo "<option selected value=\"Morning\">Morning</option> ";
} else {
echo "<option value=\"Morning\">Morning</option> ";
}
if($TofD=="Day") {
echo "<option selected value=\"Day\">Day</option>";
} else {
echo "<option value=\"Day\">Day</option>";
}
if($TofD=="Night") {
echo "<option selected value=\"Night\">Night</option>";
} else {
echo "<option value=\"Night\">Night</option>";
}
echo "
</select><br />
<input type=\"submit\" value=\"submit\" name=\"submit\">
</form>
</body></html>";
}
?>
preview4.php
<?php
session_start();
if(!isset($_POST['insert'])) {
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$gender = $_POST["gender"];
$quote = $_POST["quote"];
$education = $_POST["education"];
$TofD = $_POST["TofD"];
//set the session values
$_SESSION['Fname'] = $Fname;
$_SESSION['Lname'] = $Lname;
$_SESSION['gender'] = $gender;
$_SESSION['education'] = $education;
$_SESSION['TofD'] = $TofD;
echo "Hello, $Fname $Lname<br />
You are $gender.<br />";
echo "<p><i>$quote</i><br />
You're favorite time is $TofD, and you passed $education!<br /><br />
If this information is incorrect, click the back button to make changes.
<form action=\"http://localhost/army/index4.php\" method=\"post\">
<input type=\"submit\" name=\"back\" value=\"Back\">
</form>
<form enctype= \"multipart/form-data\" action=\"preview4.php\" method=\"POST\">
Photo: <input type=\"file\" name=\"photo\"><br>
</form>
<form action=\"http://localhost/army/preview4.php\" method=\"post\">
<input type=\"submit\" name=\"insert\" value=\"Submit To Database\">
</form>";
} else {
//This is the directory where images will be saved
$ran = rand () ;
$ran2 = $ran.".";
$target = "images/";
$target = $target .$ran2. basename( $_FILES['photo']['name']);
$ok=1;
$Fname = $_SESSION['Fname'];
$Lname = $_SESSION['Lname'];
$gender = $_SESSION['gender'];
$quote = $_SESSION['quote'];
$education = $_SESSION['education'];
$TofD = $_SESSION['TofD'];
$photo=$ran2.($_FILES['photo']['name']);
include('connect.php');
mysql_query("INSERT INTO `favor`
(`Fname`, `Lname`, `gender`,`education`, `TofD`,`photo`) VALUES('$Fname', '$Lname', '$gender',
'$education', '$TofD','$photo') ")
or die(mysql_error());
$id= mysql_insert_id();
$query = "SELECT * FROM favor WHERE id=$id";
$result = mysql_query($query)or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<p>FORM NUMBER :62RRI/ <b>".$id."</b><br><br>";
echo "<img src=\"http://localhost/army/images/$photo\"> <br>";
echo "<p>Fame: <b>".$row['Fname']."</b><br>";
echo "<p>Lname: <b>".$row['Lname']."</b><br>";
echo "<p>gender: <b>".$row['gender']."</b><p>";
}
}
?>