Inserting data and displaying data from mysql and php

create a connect.php file to save connection string

<?php


$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="swara"; // Database name

.
$connect = mysql_connect("$host", "$username", "$password");
if(!$connect)
{
die("cannot connect");
}
mysql_select_db("$db_name")or die("cannot select DB");


?>

Inserting data in mysql database using php

create insert.php
include connect.php in insert.php
pass the parameter to query .
see the sample code given below.



<?php

include"connect.php";
$txtuser=$_POST['user_name'];
$txtpass=$_POST['pass_user'];
   $sql = mysql_query("insert into login (id,user,pass)value('','$txtuser','$txtpass') ")or die(mysql_error());


?>


display data from mysql database using php


<?php
include"connect.php";



echo"<table>";

using table tag will give formating to table and data will be arrange systematically.

$show=mysql_query("select * from reciept") or die(mysql_error());


using mysql_query array is fetch and it is passed to while loop to display each and every record from mysql database

while($row=mysql_fetch_array($show))
{
echo "<tr bgcolor=#BDEDFF ><td>";
echo $row ['id'];
echo "</td><td>" ;
echo $row ['pro_name'];
echo "</td><td>" ;


echo "<a href=edit_form.php?id=".$row['id']."><strong>Edit--</strong></a>";
echo "<a href=delete.php?id=".$row['id']."><strong>/--Delete</strong></a>";
echo "</td></tr>";
}
echo"</table>";
?>

No comments:

Post a Comment