Your True Business Partner.

Monday, 2 April 2018

PHP Insert Data Into MySQL

In This tutorial i tell you how to insert data into mysql server, please fellow the steps as shown in below video ⇊



Code for data insertion: 
<?php
error_reporting(0);
include_once 'dbcon.php';

if(isset($_POST['save_mul']))
{
{
$pid = $_POST['pid'];
$name = $_POST['name'];
$desc = $_POST['desc'];
$sku = $_POST['sku'];
$qty= $_POST['qty'];
$cp = $_POST['cp'];
$sp = $_POST['sp'];



$sql="INSERT INTO products(ProductID,Name,Description,SKU,Quantity,CostPrice,SPrice) VALUES('".$pid."','".$name."','".$desc."','".$sku."','".$qty."','".$sp."','".$cp."')";

$sql = $MySQLiconn->query($sql);
}

if($sql)
{
?>
        <script>
alert('<?php echo $total." Records was inserted !!!"; ?>');
window.location.href='home.php';
</script>
        <?php
}
else
{
?>
        <script>
alert('error while inserting , TRY AGAIN');
</script>
        <?php
}
}
?>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Record</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css" />
<script src="jquery.js" type="text/javascript"></script>
<script src="js-script.js" type="text/javascript"></script>
</head>

<body>

<div class="navbar navbar-default navbar-static-top" role="navigation">
    <div class="container">

        <div class="navbar-header">
            <a align="center"><B>Product Management System</B></a>
           
        </div>

    </div>
</div>
<div class="clearfix"></div>



<div class="clearfix"></div><br />

<div class="container">

    <form name="frmRegistration" method="post" action="">
   
<table class='table table-bordered'>
   
    <tr>

<th>Product ID</th>
<th>Name</th>
<th>Description</th>
<th>SKU</th>
<th>Quantity</th>
<th>Cost Price</th>
<th>Sell Price</th>

    </tr>

        <tr>
       
         <td><input type="text" name="pid" placeholder="Product ID" class='form-control' /></td>
        <td><input type="text" name="name" placeholder="Name" class='form-control' /></td>
  <td><input type="text" name="desc" placeholder="Description" class='form-control' /></td>
        <td><input type="text" name="sku" placeholder="SKU" class='form-control' /></td>
        <td><input type="text" name="qty" placeholder="Quantity" class='form-control' /></td>
        <td><input type="text" name="cp" placeholder="Cost Price" class='form-control' /></td>
        <td><input type="text" name="sp" placeholder="Sell Price" class='form-control' /></td>
</tr>
     
    <tr>
    <td colspan="4">
   
    <button type="submit" name="save_mul" class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i> &nbsp; Insert Records</button>

    <a href="home.php" class="btn btn-large btn-success"> <i class="glyphicon glyphicon-fast-backward"></i> &nbsp; Back to Home</a>
   
    </td>
    </tr>
    </table>
    </form>

</div>

</body>
</html>

Code For Database Connection:
<?php

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "product";

$dsn = 'mysql:host=localhost;dbname=product';

$MySQLiconn = new MySQLi($DB_host,$DB_user,$DB_pass,$DB_name);
   
     if($MySQLiconn->connect_errno)
     {
         die("ERROR : -> ".$MySQLiconn->connect_error);
     }


try {
        $con = new PDO($dsn,$DB_user,$DB_pass);
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        include('../errors/err.php');
        exit();
    }






?>
Its complete details about data insertion..if you feel any difficulty please comment below .

For More Detail Contact Us:

vsitexperts@gmail.com

No comments:

Post a Comment