Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Tuesday, November 13, 2018

PHP programs to CRUD in php

<?php
include("./connect.php");
if(isset($_POST['btnReg'])){
$name=$_POST['txtnm'];
$gen=$_POST['rdbgen'];
$cno=$_POST['txtcno'];
$addr=$_POST['txtaddr'];
$h=$_POST['chk'];
$desg=$_POST['selDesg'];
$hobby=implode(",",$h);
$sql="insert into tblPerson(pname,gender,contactNo,hobbies,address,designation) values('".$name."','".$gen."','".$cno."','".$hobby."','".$addr."','".$desg."')";
$res=mysqli_query($con,$sql);
}
if(isset($_POST['btnDisp'])){
header("Location:./disp.php");
}
if(isset($_GET['act'])){

$sql="select * from tblPerson where pid =".$_GET['id'];
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_array($res);
$hobby=explode(",",$row['hobbies']);
$desg=$row['designation'];
}
if(isset($_POST['btnUpd'])){
echo 'upd';
$name=$_POST['txtnm'];
$gen=$_POST['rdbgen'];
$cno=$_POST['txtcno'];
$addr=$_POST['txtaddr'];
$h=$_POST['chk'];
$desg=$_POST['selDesg'];
$hobby=implode(",",$h);
$sql="update tblPerson set pname='".$name."',gender='".$gen."',contactNo='".$cno."',hobbies='".$hobby."',address='".$addr."',designation='".$desg."' where pid=".$_GET['id'];
$res=mysqli_query($con,$sql);
}
?>
<!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 width="779" border="1" cellspacing="0" cellpadding="10">
  <tr>
    <th scope="row">Name :</th>
    <td><input type="text" name="txtnm" id="txtnm" value="<?php (isset($row))? print($row[1]):print("") ?>" /></td>
  </tr>
  <tr>
    <th scope="row">Gender :</th>
    <td>
    <?php
if(isset($row)){
if(strcmp($row['gender'],"Male")==0){
    ?>
            <input type="radio" name="rdbgen"  value="Male" checked="checked" />
    Male
           <input type="radio" name="rdbgen"  value="Female" />
                Female
    <?php
}
else{
    ?>
<input type="radio" name="rdbgen"  value="Male" />
      Male
    <input type="radio" name="rdbgen"  value="Female" checked="checked"/>
    Female
<?php
}
}
else{
?>
    <input type="radio" name="rdbgen"  value="Male" />
      Male
    <input type="radio" name="rdbgen"  value="Female"/>
    Female
    <?php
}
    ?>
 
    </td>
  </tr>
  <tr>
    <th scope="row">Contact No. :</th>
    <td><input type="text" name="txtcno" id="txtcno" value="<?php (isset($row))? print($row['contactNo']):print("") ?>" /></td>
  </tr>
  <tr>
    <th scope="row">Address :</th>
    <td><textarea name="txtaddr" id="txtaddr" cols="45" rows="5"><?php (isset($row))? print($row['address']):print("") ?></textarea></td>
  </tr>
  <tr>
    <th scope="row">Hobbies :</th>
    <td>
    <?php
$hb=array("Dancing","Sports","Singing");
if(isset($row)){
$j=0;
foreach($hb as $val){
if(strcmp($hobby[$j],$val)==0){
echo "<input type='checkbox' name='chk[]' value='".$val."' checked='checked' />".$val."<br/>";
$j++;
}
else{
echo "<input type='checkbox' name='chk[]' value='".$val."' />".$val."<br/>";
}
}
}
else{
foreach($hb as $val){
echo "<input type='checkbox' name='chk[]' value='".$val."' />".$val."<br/>";
}
}
?>
      </td>
  </tr>
  <tr>
    <th scope="row">Designation :</th>
    <td><select name="selDesg">
    <?php
$ch=array('Programmer','Project Manager','Analyst','Clerk');
foreach($ch as $c){
if(isset($row) && strcmp($c,$desg)==0){
?>
    <option value="<?php echo $c; ?>" selected="selected"><?php echo $c; ?></option>
   <?php
}
else{
?>
<option value="<?php echo $c; ?>"><?php echo $c; ?></option>
<?php
}
}
?>
    </select></td>
  </tr>
  <tr>
    <th colspan="2" scope="row"><input type="submit" name="<?php (isset($row))? print("btnUpd"):print("btnReg")?>" value="<?php (isset($row))? print("Update"):print("Register Me") ?>" />
    <input type="submit" name="btnDisp" value="Display" />
    </th>
  </tr>
</table>
</form>
</body>
</html> 

Monday, November 12, 2018

Function to validate email in PHP

<?php
function validate($mail){
if(strpos($mail,"@")>0 && strpos($mail,".")>0)
{
$atPos= strrpos($mail,"@");
$dotPos=strrpos($mail,".");
$len=strlen($mail);
if($dotPos>$atPos && strrpos($mail,".")<= $len-2){
return true;
}
else
return false;
}
else
return false;
}
?>

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>

Saturday, November 10, 2018

Program to count number of emails in a string in 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 width="779" border="1" cellspacing="0" cellpadding="10">
  <tr>
    <th scope="row">Enter String :</th>
    <td><textarea name="txtstr" id="txtstr"></textarea></td>
  </tr>
  <tr>
    <th colspan="2" scope="row"><input type="submit" name="btnsubmit" id="btnsubmit" value="Submit" /></th>
  </tr>
</table>
</form>
</body>
</html>
<?php
include("email.php");
$email=array();
if(isset($_POST['btnsubmit'])){
$mail=$_POST['txtstr'];
$a=explode("\n",$mail);
foreach($a as $val){
$arr=explode(" ",$val);
foreach($arr as $m){
$f=validate($m);
if($f)
$email[]=$m;
}
}
echo '<br/>There are '.count($email).' emails in this string.';
}
?>

Friday, November 9, 2018

Bad words Detection in Input 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 width="779" border="1" cellspacing="0" cellpadding="10">
  <tr>
    <th scope="row">Enter String :</th>
    <td><textarea name="txtstr" id="txtstr"></textarea></td>
  </tr>
  <tr>
    <th colspan="2" scope="row"><input type="submit" name="btnsubmit" id="btnsubmit" value="Submit" /></th>
  </tr>
</table>
</form>
</body>
</html>
<?php
$wrds=array('oh','allas','wah','omg');
if(isset($_POST['btnsubmit'])){
$str=$_POST['txtstr'];
$a=explode("\n",$str);
foreach($a as $val){
$arr=explode(" ",$val);
foreach($arr as $m){
foreach($wrds as $w){
if(strcmp($w,$m)==0){ echo 'contains bad words.'; exit;
}
}
}
}
}
?>

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';
}
?>