Create table for store data . Read content which are entered in table. Update data which are already stored in table like change n...

Create , Read , Update , delete(CRUD) in PHP



Create table for store data .
Read content which are entered in table.
Update data which are already stored in table like change name , number, adress,etc...
Delete used for delete data or raw which are stored in table.

Following create a crud application.

1) Create database , table and select Db

<?php

//connectivity code
$con=mysql_connect("localhost","root","") or die(mysql_error());

//create database
mysql_query("create database test");

//select database
mysql_select_db("test");

//create table stuinfo
$que="CREATE TABLE stuinfo
(
stu_id INT AUTO_INCREMENT PRIMARY KEY ,

email VARCHAR( 50 ) NOT NULL ,

password VARCHAR( 50 ) NOT NULL ,

mobile BIGINT NOT NULL ,

address TEXT NOT NULL ,

gender ENUM( 'm', 'f' ) NOT NULL ,

hobbies VARCHAR( 100 ) NOT NULL ,

image VARCHAR( 200 ) NOT NULL ,

dob DATE NOT NULL ,

INDEX ( `gender` , `dob` ) ,

UNIQUE (`email` ,`mobile`)

);

?>

2) Retrieve value from html form

<?php

error_reporting(1);

//retrieve email id entered by user and store in $eid. and so on for other values

$eid=$_POST['eid'];
$p=$_POST['pass'];
$m=$_POST['mob'];
$add=$_POST['add'];
$gen=$_POST['g'];
$hobb=$_POST['chlist'];
$h=implode(",",$hobb);
$img=$_FILES["file"]["name"];
$yy=$_POST['yy'];
$mm=$_POST['mm'];
$dd=$_POST['dd'];
$dob=$yy."-".$mm."-".$dd;

//checked whether user clicked on INSERT button

if(isset($_POST['ins']))
{
$sql="INSERT INTO stuinfo values('','$eid','$p','$m','$add','$gen','$h','$img','$dob')";

mysql_query($sql);
move_uploaded_file($_FILES["file"]["tmp_name"], "userDetails/" . $_FILES["file"]["name"]);
echo "data saved";
}

//checked whether user clicked on display button

if(isset($_POST['disp']))
{
$res=mysql_query("SELECT * FROM stuinfo");
echo "<table border='1'>";
echo "<tr><th>SutId</th><th>Email</th><th>Password</th><th>Mobile</th><th>Address</th><th>Gender</th>    </tr>";

while(list($a,$b,$c,$d,$e,$f)=mysql_fetch_array($res))
{

echo "<tr>";   

echo "<td>".$a."</td>";

echo "<td>".$b."</td>";

echo "<td>".$c."</td>";

echo "<td>".$d."</td>";

echo "<td>".$e."</td>";

echo "<td>".$f."</td>";

echo "<td><a href='db.php?chk=$b'>Edit</a>   
  <a href='db.php?chkid=$a'>Delete</a>
      </td>";

echo "</tr>";   

}

echo "</table>";
}

// receive  value(value sent  using query string )

@$v=$_GET['chkid'];
@$email=$_GET['chk'];
if(isset($v))
{
mysql_query("delete from stuinfo where stu_id='$v'");
echo "records deleted";
}

if(isset($_POST['upd']))
{
$p=$_POST['p'];
$m=$_POST['mob'];
$add=$_POST['add'];
echo $email;

$upd="update stuinfo set pass='$p',mobile='$m',address='$add' where email='$email'";

mysql_query($upd);

echo "updated";

}

if(isset($_GET['chk']))
{
$sql="SELECT * FROM stuinfo where email='$email'";

$res=mysql_query($sql);

list($a,$b,$c,$d,$e,$f)=mysql_fetch_array($res);

echo "<table border='1'>";

echo "<form method='post'>";

echo "<tr>";

echo "<td>Password</td>";

echo "<td><input type='password' name='p' value='$c' /></td></tr>";

echo "<tr>";

echo "<td>Mobile</td>";

echo "<td><input type='text' name='mob' value='$d' /></td></tr>";

echo "<tr>";

echo "<td>Address</td>";

echo "<td><textarea name='add'>$e</textarea></td></tr>";

echo "<tr>";

echo "<td colspan='2' align='center'><input type='submit' value='Update infor' name='upd' /></td>
      </tr>";

echo "</form>";

echo "</table>";
}


?>


3) HTML form

<body>

<form  method="post" enctype="multipart/form-data">

<table width="419" border="1">
<tr>   
<td width="173">Enter your email </td>
<td width="230"><input type="email"  name="eid"/></td> 
</tr> 
<tr>
<td>Password</td> 
<td><input type="password"  name="pass"/></td>
</tr> 
<tr>   
<td>Mobile</td>
<td><input type="text"  name="mob"/></td>
  </tr>

<tr> 
<td>Address</td>
    <td><textarea name="add"></textarea></td>
</tr>
<tr> 
<td>Gender</td>
<td>
Male<input type="radio" value="m" name="g"/>
Female<input type="radio" value="f" name="g"/>
</td>
  </tr>
<tr>

<td>Hobbies</td>
<td>cricket<input type="checkbox" name="chlist[]" value="cricket"/>
    singing<input type="checkbox" name="chlist[]" value="singing"/>
                    reading<input type="checkbox" name="chlist[]" value="reading"/>
</td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="file" id="file" /> </td>
</tr>
<tr>
  <td>DOB</td>
<td>
<select name="yy">
<option value="" disabled="disabled" selected="selected">Year</option>
<?php
for($i=1900;$i<=2013;$i++)
{
echo "<option value='$i'>$i</option>";
  }
?>
</select>
<select name="mm">
<option value="" disabled="disabled" selected="selected">Month</option>
<?php
for($i=1;$i<=12;$i++)
{
echo "<option value='$i'>$i</option>";
  }
?>
</select>
<select name="dd">
<option value="" disabled="disabled" selected="selected">Date</option>
<?php
for($i=1;$i<=31;$i++)
{
echo "<option value='$i'>$i</option>";
  }
?>
</select></td>
</tr>
  <tr>
  <td colspan="2" align="center"> <input type="submit" name="ins" value="Insert"/>
<input type="submit" name="disp" value="show"/>
</tr>
</table>
</form>
</body>

</html>

0 coment�rios: