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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>

Recommended Answers

All 2 Replies

Do you know how to insert data into a mysql database from the mysql command line? If not, learn that first. It will make the rest a lot easier.
Your PHP code shows no connection to a MYSQL database whatsoever. Study a primer about php and mysql first. Then come back and ask specific questions.

I know how to insert data in mysql from form if i dont use array. I dont know how to insert data from array.

I dont have exact code with me right now as it is saved in my office computer. Below is something which I'm able to recall as of now.

<?php require("connect.php"); ?>

<?php
		
	// Variables
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$age = $_POST['age'];


$query = "INSERT INTO user (fname, lname, age) VALUES ('fname', 'lname', 'age')";
mysql_query($query);
$id_user = mysql_insert_id();



	$referal = $_POST['referal'];
	$name = $_FILES["img"]["name"];
	$type = $_FILES["img"]["type"];
	$size = $_FILES["img"]["size"];
	$tmp_name = $_FILES["img"]["tmp_name"];
	$error = $_FILES["img"]["error"];
	$filename = $referal .".jpeg";
	$path = "upload/" . $filename;
	
	
	//echo $type . "<br />";
	echo $referal . "<br />";
	echo $path . "<br />";
	
	
	if($type == "image/jpeg") 
	{
		if($error > 0)
		{
		echo "Return Code:" . $error; 
		}
		else 
		{
		echo "You have successfully uploaded " . $name . " file" . "<br />"; 
		
			if(file_exists("upload/" . $name))
			{
			echo $name . "File already exists";
			}
			else 
			{
			move_uploaded_file($tmp_name, "upload/" . $filename);
			echo "File saved in upload/" . $filename . "<br />";
			
			$qry = "INSERT INTO referals VALUES ('', '$referal', '$path')";
			mysql_query($qry) or die('Error, query failed : ' . mysql_error());
			$id_referal = mysql_insert_id();
			
			}
		}
	}
	else
    {
	echo "Invalid file";
	}

$query = "INSERT INTO user_referals (user_id, referals_id) VALUES ('$id_user', '$id_referal')";
mysql_query($query);

?>

<?php mysql_close($conn); ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.