Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Sunday, November 11, 2018

Multiple delete using checkbox PHP MySql

<?php
include("./connect.php");
$sql="select * from tblPerson where 1";
$res=mysqli_query($con,$sql);
if(isset($_POST['btnDel'])){
echo "delete";
$id=$_POST['chk'];
$idList=implode(",",$id);
echo $idList;
$sql="delete from tblPerson where pid in(".$idList.")";
$res=mysqli_query($con,$sql);
header("Location:./disp.php");
}
?>
<!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>Untitled Document</title>
</head>

<body>
<form method="post">
<table border="1" width="779" cellpadding="10" cellspacing="0">
<tr>
<th colspan="9" align="right"><a href="ins_reg.php">Add New</a></th>
</tr>
<tr>
<th>Name</th>
    <th>Gender</th>
    <th>Contact No.</th>
    <th>Hobbies</th>
    <th>Address</th>
    <th>Designation</th>
    <th>Edit</th>
    <th>Delete</th>
    <th>Multiple Delete</th>
</tr>
<?php
while($row=mysqli_fetch_array($res)){
?>
<tr align="center">
    <td><?php echo $row['pname']; ?></td>
        <td><?php echo $row['gender']; ?></td>
        <td><?php echo $row['contactNo']; ?></td>
        <td><?php echo $row['hobbies']; ?></td>
        <td><?php echo $row['address']; ?></td>
        <td><?php echo $row['designation']; ?></td>
        <td><a href="ins_reg.php?id=<?php echo $row[0]; ?>&act=edit">Edit</a></td>
        <td><a href="disp.php?id=<?php echo $row[0]; ?>&act=del">Delete</a></td>
        <td><input type="checkbox" name="chk[]" value="<?php echo $row[0]; ?>"/></td>
    </tr>
<?php
}
?>
<tr>
<td colspan="9" align="right"><input type="submit" name="btnDel" value="Delete Marked"/></td>
</tr>
</table>
</form>
<?php
if(isset($_GET['act'])){

if(strcmp($_GET['act'],"del")==0){

$sql="delete from tblPerson where pid=".$_GET['id'];
$res=mysqli_query($con,$sql);
header("Location:./disp.php");
}
}
?>
</body>
</html>

Monday, April 18, 2016

How to upload data in php using excel file and save in mysql database

<form enctype="multipart/form-data" method="post" role="form">
    <div class="form-group">
        <label for="exampleInputFile">File Upload</label>
        <input type="file" name="file" id="file" size="150">
        <p class="help-block">Only CSV File Import.</p>
    </div>
    <button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
<?php
if(isset($_POST["Import"]))
{
    //First we need to make a connection with the database
    $host='localhost'; // Host Name.
    $db_user= 'root'; //User Name
    $db_password= '';
    $db= 'database'; // Database Name.
    $conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
    mysql_select_db($db) or die (mysql_error());
    echo $filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
        //$sql_data = "SELECT * FROM prod_list_1 ";
$count = 0;                                         // add this line
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
    //print_r($emapData);
    //exit();
    $count++;                                      // add this line

    if($count>1){                                  // add this line
      $sql = "INSERT INTO `tablename` (`fieldname`, `fieldname`, `fieldname`) VALUES ('$emapData[0]', '$emapData[1]', '$emapData[2]', '$emapData[3]')";
      mysql_query($sql);
    }                                              // add this line
}
        fclose($file);
        echo 'CSV File has been successfully Imported';
     
    }
    else
        echo 'Invalid File:Please Upload CSV File';
}
?>