Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: Mysql

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: Mysql
Showing posts with label Mysql. Show all posts
Showing posts with label Mysql. Show all posts

PHP Live Edit Table Example

PHP Live Edit Table Example

Today We See How To Make Live Editable Using PHP+JS+Mysql+Ajax

Let's Start.

For Updating Data Live in Our Database.

1. Let's First Going TO Start My Server.

2.Opening PHPMyadmin and Creating a Database Named "live"

and also a table name "live_data".


live.php (code )  //Our View Page
 
<?php
include 'connect.php';
//==========Checked Connection Establish or Not.==============
if(!$con1){

	echo "Failed TO Establish Database Connection";
	die();
	//===========Stop Execution if connection failed============

}
else{
	//===========Fetching Result from table live_data==================
	$qr=mysql_query("select * from live_data");

	//=========adding border so it look good===============
	echo "<table style='border-collapse:collapse;' border='1'>";

	//============adding heading for each column=========================
	echo "<tr><th>ID</th><th>NAME</th><th>DOB</th><th>Qualification</th></tr>";
	while ($row=mysql_fetch_assoc($qr)) {

		//============Printing All Data in Table Row========

		//============data is html5 attribute for adding exta information on html page===
		echo "<tr>";
		echo "<td class='id' data-id='".$row['id']."' >".$row['id']."</td>";
		echo "<td class='item' data-name='name'>".$row['name']."</td>";
		echo "<td class='item' data-name='dob'>".$row['dob']."</td>";
		echo "<td class='item' data-name='qualification'>".$row['qualification']."</td>";
		echo "</tr>";
	}
	echo "</tr>";
}
?>
<script  src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
	//===========Events Call When Our Page is Ready======
	$(document).ready(function() {
		

		//=============adding double click event on each table cell==========
		$(document).on("dblclick",".item",function(){

			//=======get The Value Of Current Selected TD using this keyword======
			var values=$(this).text();
			$(this).html("<input value='"+values+"' class='input'>");

		});

		$(document).on("blur",".input",function(){

			var vales=$(this).val();

sendToServer($(this).val(),$(this).parent("td").parent("tr").children(".id").data("id"),$(this).parent("td").data("name"));
									$(this).parent("td").text(vales);
			                      $(this).remove();
		})




function sendToServer(value,id,key){



			$.ajax({
				url: 'api.php',
				type: 'POST',
				data: {key:key,id:id,value:value},
			})
			.done(function(data) {
				alert(data);
			})
			.fail(function() {
				alert("Network error");
			});

			}

	})
</script> 

connect.php (Database Connection file)
 
<?php
//================+Connecting Database================
$con=mysql_connect("localhost","root","");

//=============Selecting Database======================
$con1=mysql_select_db("live",$con);

api.php (Our Api Which Update Our Date When We Post Through Ajax)
 
<?php
include 'connect.php';
if($_REQUEST['key']=="name"){
$qr=mysql_query("UPDATE live_data set name='".$_REQUEST['value']."' where id='".$_REQUEST['id']."'");
echo "Updated";
}
else if($_REQUEST['key']=="dob"){
$qr=mysql_query("UPDATE live_data set dob='".$_REQUEST['value']."' where id='".$_REQUEST['id']."'");
echo "Updated";	
}
else if($_REQUEST['key']=="qualification"){
	$qr=mysql_query("UPDATE live_data set qualification='".$_REQUEST['value']."' where id='".$_REQUEST['id']."'");
echo "Updated";
}
else{
	echo "Key Not Found";
}

Video Tutorial
0

PHP Mysql Tutorial



PHP MySql Tutorial





In this tutorial i will share how to use mysql and php.

Lets get start.

For Cpanel
Open Mysql Database And Create A New Database And Create a New Username and Password then set the username to Database And Give all privilages.
Now Open Your Phpmyadmin from cpanel then select database and create a new table which we use to store data.

In this tutorial i will create a table name login
And Columns are
id - int - 255 - Auto increment - primary key
name - text
password - text
email - text

For xampp:

Open http://localhost/phpmyadmin
And Create a new database and create the table same as which we created in for server.

Now Lets start How to insert,select,update,delete data from database.

Create Database Connection in PHP

Filename: connect.php

<?php
$server=”localhost”;
$username=”root”;
$password=”root”;
$database=”demo”;
$con=mysql_connect($server,$username,$password);

//this function is user to connect our server mysql it takes three parameters first is your server ,second is username and third parameter is password.

$con2=mysql_select_db($database,$con);

//this function is used to select our database. it takes two parameter first parameter is database name and second is the connection which we create to connect our server.
?>



Insert Operation

<?php
include ‘connect.php’;
//inclufe function is used to to include one file into other in php

$name=”sswebtricks”;
$email=”sswebtricks@gmail.com”;
$password=”pass@123”;

$qr=mysql_query(“INSERT INTO login (name,email,password) VALUES (‘$name’,’$email’,’$password’));

//mysql_query is function to write our query it takes one parameter which is our query it returns boolean type value true or false

If($qr)
{
echo “insert sucess”;
}
else
{
echo “failed”;
}
?>


Select Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“select * from login”);
$row=mysql_fetch_array($qr);

//mysql fetch array function is used to fetch fetch all the data from.database it takes one parameter which is our query and return array value .Here we only fetch single row .

echo $row[‘email’].”<br>”;
//print the email of name

//Now we fetch multiple rows
while($row2=mysql_fetch_array($qr))
{
  echo $row2[‘email’].”<br>”;
}

?>


Update Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;
$newemail=”sksanjeev362@gmail.com”;

$qr=mysql_query(“UPDATE login SET email='$newemail' WHERE name=’$name’");

// Above is updAte query

if(mysql_affected_rows>0)
{
echo “updated”;
}
else
{
echo “failed”;
}


//mysql affected rows check how many rows will affected with our query
?>

Delete Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“delete from login where name=’$name’”);

if($qr)
{
echo “ deleted”;
}
else
{
echo “failed”;
}
?>


8