Friday, 25 May 2018

insert multiple fields using foreach loop

create  a  table     students:

SQL>
     create table  students
 (id   int primary key auto_increment,
   name    varchar(200),
  age  varchar(200)
);


<?php

if (isset($_POST['submit'])) {
$i = 0;
    foreach ($_POST as $val) {
         $name = $_POST['name'][$i];
    $age = $_POST['age'][$i];

$con=mysqli_connect("localhost","root","","abhi");

$sql="INSERT INTO students (name,age) VALUES ('$name','$age')";


        $result=mysqli_query($con,$sql);

$i++;


if($result)
{
echo "record inserted";
}
    }
}

?>


<h1>Add user</h1>
 <form method="post" action="test.php">

 <table>
    <thead>
        <th>Name</th>
        <th>Age</th>
    </thead>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>
</table>

 <input type="submit" name="submit" value="Submit" />
 </form>

Monday, 21 May 2018

php pagination script with mysqli

<?php

    $con=mysqli_connect("localhost","root","","payroll");
 
    $per_page = 2; //per page record



 
$pages_query = mysqli_query($con, 'SELECT COUNT(id) FROM employee');

$row = mysqli_fetch_array($pages_query);

$pages = ceil($row[0] / $per_page);


 

//echo $pages;
 
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; //page number throung url

    $start = ($page - 1) * $per_page;

  $sql="SELECT *  FROM employee LIMIT $start, $per_page";
 
  $query = mysqli_query($con,$sql);

while($row=mysqli_fetch_array($query))
{

echo "id is" .$row['id'];
echo $row['name'];
echo $row['phone'];
echo $row['salary'];

echo '<br>';
}

$prev = $page - 1;
    $next = $page + 1;
 
    if(!($page<=1))
{
        echo "<a href='pag.php?page=$prev'>Prev</a> ";
    }

    if($pages>=1 && $page<=$pages){
 
        for($x=1;$x<=$pages;$x++){
            echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
     
        }
 
    }
 
    if(!($page>=$pages))
{
        echo "<a href='pag.php?page=$next'>Next</a>";
    }
 
 
?>

Monday, 14 May 2018

how to display multiple checkbox value from database in php

Step 1:  

SQL>
create table  userrecord (
id int primary key ,
name   varchar(200)
);


Step 2: write code for checkbox.php file:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ankit";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn)
{
    die("Connection failed: " . mysqli_connect_error());
}

$sql="select * from userrecord";

$result=mysqli_query($conn,$sql);
while ($row=mysqli_fetch_array($result))
{

echo '<form action="select.php" method="post">

<input type="checkbox" name="check_list[]" value="'.$row['id'].'"><label> "'.$row['name'].'"</label><br/>';



}
echo '<input type="submit" name="submit" value="Submit"/>
</form>';

?>


Step 3: write code for   select.php  file:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ankit";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

if(isset($_POST['submit'])){//to run PHP script on submit


if(!empty($_POST['check_list'])){




$data=implode(',',$_POST['check_list']);

echo $sql="select  *  from  userrecord  where  id  IN($data)";

$result=mysqli_query($conn,$sql);

while($row=mysqli_fetch_array($result))
{
echo "id=".$row['id'];
echo "name=".$row['name'];

}



// Loop to store and display values of individual checked checkbox.

}


}
?>

Browsers

Powered by Blogger.

Feature Post

About Me

My photo
B.E.(COMPUTER ENGINEER) PHP Web developer & Provide Training for Web Development , Digital Marketing ,Android Mobile Apps Development

Post Slider (Homepage)

Blog Archive

Malwares

Transfer Files

Media Players

Download Manager

Window

Torrents Soft

Developer Tools

Botton Ad

Ads

Newsletter

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top