PHP Code to Insert Data in MySql

Right now, will make a table in a database and afterwards we will add information to it. We will likewise utilize a PHP function to transfer documents.

PHP Code to Insert Data in MySql

Initially, make a mapping in a database and make a table in it include the following fields in it.

This table holds the information for an item provider.

Presently, make an Html structure to get the information from the client. Make a record named “supplier.php” and include the accompanying code in it:

HTML code

<html>
<form action='supplierSubmit.php' method="post" enctype='multipart/form-data'>
	<center>
	<table>
		<caption>
			<font size='5'><b>Supplier Details</b></font>
		</caption>
		<tr>
			<td><b>Supplier Id:</b></td>
			<td><input type='text' name='sid' size=30></td>
		</tr>
		<tr>
			<td><b>Supplier Name:</b></td>
			<td><input type='text' name='sn' size=30></td>
		</tr>
		<tr>
			<td><b>Firmname:</b></td>
			<td><input type='text' name='fn' size=30></td>
		</tr>
		<tr>
			<td><b>Gender:</b></td>
			<td>
				<input type=radio name='gen' value="Male">Male&nbsp;&nbsp;
				<input type=radio name='gen' value='Female'>Female
			</td>
		</tr>
		<tr>
			<td><b>DOB:</b></td>
			<td><input type=date name='dob' size=30></td>
		</tr>
		<tr>
			<td><b>Amount Due:</b></td>
			<td><input type='text' name='amtd' size=30></td>
		</tr>
		<tr>
			<td><b>Amount Paid:</b></td>
			<td><input type='text' name='amtp' size=30></td>
		</tr>
		<tr>
			<td><b>City:</b></td>
			<td><input type='text' name='city' size=30></td>
		</tr>
		<tr>
			<td><b>Photo:</b></td><td>
			<input type='file' name='photo'></td>
		</tr>
		<tr>
			<td><input type=submit></td>
			<td><input type=reset></td>
		</tr>
	</table>
	</center>
</form>
</html>

How about we look at how the structure resembles:

PHP Code to Insert Data in MySql

Presently make a record named “supplierSubmit.php” and include the accompanying code in it.

PHP code with HTML

<html>
	<?php
		$dsn="mysql:host=localhost;dbname=hpindia";
		$cn=new PDO($dsn,'root','123');
		$sid=$_POST['sid'];
		$sn=$_POST['sn'];
		$fn=$_POST['fn'];
		$gender=$_POST['gen'];
		$dob=$_POST['dob'];
		$amtd=$_POST['amtd'];
		$amtp=$_POST['amtp'];
		$city=$_POST['city'];
		$pic=$sid.".jpg";
		//$photo=$_Files['photo']['name'];

		$q="insert into supplier values($sid,'$sn','$fn','$gender','$dob',$amtd,$amtp,'$city','$pic')";
		$smt=$cn->prepare($q);
		$result=$smt->execute();
		if($result){
			move_uploaded_file($_FILES['photo']['tmp_name'],"images/$pic");
			echo "<font color='green' size=5>Record Submitted</font>";
		}
		else{
			echo "<font color='red' size=5>Fail to Submit Record</font>";
		}

		$nq="select * from supplier";
		$res=$cn->query($nq);

		if($res){
	?>

	<center>
	<table border=1>
		<caption>
			<b><i>List of Suppliers</i></b>
		</caption>
	<tr>
		<th>Supplier Id</th>
		<th>Name</th>
		<th>Firmname</th>
		<th>Gender</th>
		<th>DOB</th>
		<th>Amount Due</th>
		<th>Amount Paid</th>
		<th>City</th>
		<th>Photo</th>
		<th>Update</th>
	</tr>
	<?php
			while($row=$res->fetch()){
				echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td><img src='images/$row[8]' width=50 height=50></td><td><a href='displayById?sid=$row[0]'>Edit</a></td></tr>";
			}
		}
		else {
			echo "<font color='red' size=5>Record Not Found";
		}
		?>
	</table>
	</center>
</html>

This function is utilized to transfer the records. Recollect database will just hold the name of the picture document and not the record to spare the picture a specific spot we need to make an envelope named “pictures” inside your PHP workspace.

<?php
move_uploaded_file($_FILES['photo']['tmp_name'],"images/$pic");
?>

It is a predefined function, it takes two parameters.

$_FILES[‘photo’][‘tmp_name’] → this snaps the photo and stores it in an impermanent organizer name tmp_name.

The second parameter has an organizer named pictures and name of the image in variable $pic, this exchanges the image from the transitory envelope to the pictures organizer with name as putting away in $pic variable.

As the client hit’s the submit button the subtleties ought to be added to the database and the picture must be added to pictures organizer that u made before.

Note: In request to send an image or record we generally keep the solicitation POST and enctype as multipart/structure information as we are sending picture just as the structure information with it.

Leave a Comment

error: Alert: Content is protected!!