Hi All,
I'm very new to PHP and MYSQL and I'm having hard time trying to insert data from PHP form in MYSQL DB. I'm able to echo out the output on webpage but i'm not able to figure out how to insert the data in DB.
Please help me guide in right direction.
I have 5 tables as below:
table : user
user_id (AI)(PK)
fname
lname
age
table : referals
referals_id (AI)(PK)
referals
file_path
table : course
course_id (AI)(PK)
course
table : user_referals
user_id (FK from user table)(PK)
referals_id (FK from referals table)(PK)
table : user_course
user_id (FK from user table)(PK)
course_id (FK from course table)(PK)
Below is PHP Form code
<!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>Insert User Details</title>
<style type="text/css">
body {
background-color: #C9F;
}
</style>
</head>
<body>
<b>
<h1>Create New User Entry:</h1>
<form action="view1.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p> </p>
<table width="60" border="1" cellpadding="1" cellspacing="1">
<tr>
<td width="50"><b>First Name:</b></td>
</tr>
<tr>
<td><b>
<input name="fname" type="text" id="fname" size="30" />
</b></td>
</tr>
<tr>
<td width="50"><b>Last Name:</b></td>
</tr>
<tr>
<td><b>
<input name="lname" type="text" id="lname" size="30" />
</b></td>
</tr>
<tr>
<td><b>Age:</b></td>
</tr>
<tr>
<td><b>
<input type="text" name="age" id="age" size="30"/>
</b></td>
</tr>
</table>
<p>
REFERAL:
<label for="img"></label>
</p>
<table width="400" border="1" cellspacing="1">
<tr>
<th width="50"><b>NAME</b></th>
<th width="50">REFERAL LETTER UPLOAD</b></th>
<th width="300">PREVIEW</th>
</tr>
<tr>
<td><b>
<input name="referal[]" type="text" id="referal" size="30" />
</b></td>
<td><b>
<input type="file" name="img12" id="img12" />
</b></td>
<td> </td>
</tr>
<tr>
<td><b>
<input name="referal[]" type="text" id="referal" size="30" />
</b></td>
<td><b>
<input type="file" name="img18" id="img18" />
</b></td>
<td> </td>
</tr>
<tr>
<td><b>
<input name="referal[]" type="text" id="referal" size="30" />
</b></td>
<td><b>
<input type="file" name="img19" id="img19" />
</b></td>
<td> </td>
</tr>
<tr>
<td><b>
<input name="referal[]" type="text" id="referal" size="30" />
</b></td>
<td><b>
<input type="file" name="img20" id="img20" />
</b></td>
<td> </td>
</tr>
<tr>
<td><b>
<input name="referal[]" type="text" id="referal" size="30" />
</b></td>
<td><b>
<input type="file" name="img21" id="img21" />
</b></td>
<td> </td>
</tr>
</table>
<p><br />
Courses:<br />
</p>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td width="150"><label> <input type="checkbox" name="course[]" value="C" /> C</label></td>
<td width="150"><label> <input type="checkbox" name="course[]" value="C++" /> C++</label></td>
<td width="150"><label> <input type="checkbox" name="course[]" value="PHP" /> PHP</label></td>
<td width="150"><label> <input type="checkbox" name="course[]" value="PERL" /> PERL</label></td>
</tr>
<tr>
<td width="150"><label> <input type="checkbox" name="course[]" value="python" /> PYTHON</label></td>
<td width="150"><label> <input type="checkbox" name="course[]" value="mysql" /> MYSQL</label></td>
<td width="150"><label> <input type="checkbox" name="course[]" value="oracle" />
ORACLE</label></td>
<td width="150"><label> <input type="checkbox" name="course[]" value="DB2" /> DB2</label></td>
</tr>
</table>
<br />
<br />
<input name="submit" type="submit" id="send" value="submit">
</form>
</b>
</body>
</html>
Below is PHP output code
<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
?>
<?php echo "First Name:" . " " . ":" . " " . "<b>$fname</b>"; ?><br /><br />
<?php echo "Last Name:" . " " . ":" . " " . "<b>$lname</b>"; ?><br /><br />
<?php echo "Age:" . " " . ":" . " " . "<b>$age</b>"; ?><br /><br />
<?php
if(isset($_POST['referal']))
{
$referal = $_POST['referal'];
$n = count($referal);
$i = 0;
echo "User Referals are :\r\n".
"<ol>";
while ($i < $n)
{
if($referal[$i] == ""){
//do nothing
$i++;
}else{
echo "<li>{$referal[$i]}</li> \r\n";
$i++;
}
}
echo "</ol>";
}
?>
<br />
<?php
if(isset($_POST['course']))
{
$course = $_POST['course'];
$n = count($course);
$i = 0;
echo "User is proficent in below Course : \r\n" .
"<ol>";
while ($i < $n)
{
if($course[$i] == ""){
//do nothing
$i++;
}else
echo "<li>{$course[$i]}</li> \r\n";
$i++;
}
echo "</ol>";
}
?><br />
<!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>User Detail</title>
</head>
<body>
</body>
</html>