PHP Tutorial

PHP Tutorial #8

Finally, we need to insert some data. This is done like this:-

 

<?php   

	$domain = "localhost";
	$user = "linux";
	$password = "linux";
	$new_db = "fone";
	$name="ethel";
	$num="01234 12344";

	$sql="INSERT INTO fone(name, number)
		VALUES (\"$name\", \"$num\")";

	$conn = mysql_connect($domain, $user, $password)
		OR DIE("Sorry - could not connect to MySQL");

	if($new_db)
	{
		$result=mysql_select_db($new_db, $conn);
		if(!$result)
		{
			DIE("Sorry - could not select database");
		}

		$result=mysql_query($sql,$conn);

		if(!$result)
		{
			DIE("Sorry - could not insert data");
		}
		
		if(result)
		{
			echo("Record added");
		}
	}

?>

<html>
	<head>
		<title>Inserting data</title>
	</head>
	<body>

	</body>
</html>

Again, all the work is done by an SQL statement:-

$sql="INSERT INTO fone(name, number)
	VALUES (\"$name\", \"$num\")";

This is fairly straight forward although the \" are required because we are using variables to contain the data. Note that we haven't entered a value for id as this is being generated automatically for us. Now we want to view the data we have just entered:-

<?php   

	$domain = "localhost";
	$user = "linux";
	$password = "linux";

	$new_db = "fone";

	$sql="SELECT id, name, number FROM fone";

	$conn = mysql_connect($domain, $user, $password)
		OR DIE("Sorry - could not connect to MySQL");

	if($new_db)
	{
		$result=mysql_select_db($new_db, $conn);
		if(!$result)
		{
			DIE("Sorry - could not select database");
		}

		$result=mysql_query($sql,$conn);

		if(!$result)
		{
			DIE("Sorry - could not read data");
		}
		
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{
				echo("ID: " .$row["id"]);
				echo(" - NAME: " .$row["name"]);
				echo(" - NUMBER: " .$row["number"]);
			}
		}
	}

?>

<html>
	<head>
		<title>Viewing data</title>
	</head>
	<body>

	</body>
</html>

This uses the SQL statement to view all the entries. Note that the field names are enclosed in quotes " and square brackets [ ]. For completeness, we will now delete an entry:-

<?php   

	$domain = "localhost";
	$user = "linux";
	$password = "linux";

	$new_db = "fone";

	$sql="DELETE FROM fone where id = 1";

	$conn = mysql_connect($domain, $user, $password)
		OR DIE("Sorry - could not connect to MySQL");

	if($new_db)
	{
		$result=mysql_select_db($new_db, $conn);
		if(!$result)
		{
			DIE("Sorry - could not select database");
		}

		$result=mysql_query($sql,$conn);

		if(!$result)
		{
			DIE("Sorry - could not delete data");
		}
		
		if(result)
		{
			echo("Record deleted");
		}
	}

?>

<html>
	<head>
		<title>Deleting data</title>
	</head>
	<body>

	</body>
</html>

Note this SQL statement:-

$sql="DELETE FROM fone where id = 1";

This will only delete the entry that has an id that equals 1. To delete everything in the table use:-

$sql="DELETE FROM fone";

That just about completes this tutorial. Next time I'll be explaining how to create a graphical user interface (text boxes, buttons etc) to give a much more polished look. In the meantime, play around with the files, changing data etc...

Back

Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at:
nickjc@nickjc.co.uk if you need help