Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Insert and Get data

Insert data into table

<?php
$con=mysqli_connect("localhost","root", "", "data1");
if(isset($_POST["log"]))
{
$sr=$_POST["sr"];
$user=$_POST["user"];
$pass=$_POST["pass"];
$insertquery="insert into admin(SR,username, password) values('$sr', '$user','$pass')";
$result=mysqli_query($con,$insertquery);
if($result)
{
echo "data inserted";
}
else
{
echo "data not inserted";
}
}

?>
<html>
<body>
<form method="POST">
<input type="text" name="sr" required><br>
<input type="text" name="user" required><br>
<input type="password" name="pass" required><br>
<input type="submit" name="log" value="login">
</form>
</body>
</html>
Get data from Database

<?php
$con=mysqli_connect("localhost","root", "", "data1");

$query="select *from admin";


if($result=mysqli_query($con,$query))
{
echo "query executed".'<br>';
while($query_execute=mysqli_fetch_assoc($result))
{
echo $query_execute['username'].'<br>';
echo $query_execute['password'].'<br>';
}
}
else
{
echo "query not executed";
}

?>
Deleting a Database

• If a database is no longer required then it can be deleted


forever. You can use pass an SQL command
to mysql_query to delete a database.
Example of delete database

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'DROP DATABASE test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not delete database db_test: ' . mysql_error());
}
echo "Database deleted successfully\n";
mysql_close($conn);
?>
update table
<html
<body>
<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("not opened");
}
echo "Connection open"."</br>";
$db = mysql_select_db("studinfo",$con);
if(!$db)
{
die("Database not found".mysql_error());
}
echo "Database is selected"."</br>";
$query = "update admin set id = 09 where id = 102";
$crtb = mysql_query($query,$con);
if(!$crtb)
{
die(" table not updated. .!".mysql_error());
}
echo "table updated.. !"."</br>";
?>
</body>
</html>
Select data and show in table format
<body>
<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("not opened");
}
echo "Connection open"."</br>";
$db = mysql_select_db("studinfo",$con);
if(!$db)
{
die("Database not found".mysql_error());
}
echo "Database is selected"."</br>";
$query = "select * from admin";
$sldt = mysql_query($query,$con);
if(!$sldt)
{
die("data not selected".mysql_error());
}
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>password</th>
</tr>";
while($row = mysql_fetch_array($sldt))
{
Upload file to a folder
• <?php
• if(isset($_FILES['img'])){
• echo "<pre>";
• print_r($_FILES);
• echo "<pre>";
• $file_name=$_FILES['img']['name'];
• $file_size=$_FILES['img']['size'];
• $file_tmp=$_FILES['img']['tmp_name'];
• $file_type=$_FILES['img']['type'];
• if(move_uploaded_file($file_tmp,"upload/".$file_name)){
• echo"successfully uploaded";
• }
• else{
• echo" not uploaded";
• }
• }

• ?><html>
• <body>
• <form action="" method="post" enctype="multipart/form-data">
• <input type="file" name="img">
• <input type="submit" name="s1">
• </body>

You might also like