Intro

Before we start need to create a databse to work in (i will assume you are using phpadmin to do this) just go to (databases)... and in the blank textbar create a new database, and call it phpforms

then open up your command prompt and enter your mysql shell
(for me it was C:\mysql\bin\mysql -u michael -p)

and enter these lines

mysql> CREATE TABLE information (
> id INT NOT NULL AUTO_INCREMENT,
> name VARCHAR (50),
> email VARCHAR (50),
> opinion VARCHAR (30),
> PRIMARY KEY (id)
);


Getting started

OK now we're ready to work. First you need a way to access your database so you can see what people think of your site/forums or whatever you plan on using this for.

We use this script:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title> How to Grab Data from a MySQL database!</title>
</head>
<body>
<table border="1" cellspacing="2" cellpadding="1" height="800"><caption>How people felt about my site!</caption>
<tr>
<td valign="top" bgcolor="#CCCCCC">
<?php 
 
 
// prints out at top of page, so that poeple know what they are looking at//
echo "poeple that found site to be horrible:<hr /><br /><br />";
 
 
/* declar some relevant variables */
 
$Host = "locationofmysql"; //location of mySQL on server
$User = "yourlogin"; //my username
$Pass = "yourpass"; //my password
$Name = "phpforms"; //name of the database to be used
$Table = "nameoftable"; //name of the table within the database
 
		mysql_connect ($Host, $User, $Pass, $Name, $Table) or die ("unable to connect to database");
		@mysql_select_db("$Name") or die ("unable to select DB");
		$sqlquery = "SELECT * FROM $Table WHERE opinion = 'is horrible'";
		$result = mysql_query($sqlquery);
		$number = mysql_num_rows($result);
 
 
$i = 0;
 
	 if ($number < 1) 
	 {
	 echo "<center><p>No results to display!</p></center>";
	 }
	 else 
	 {
	 while ($number > $i) 
		 {
		 $thename = mysql_result ($result, $i, "name");
		 $theemail = mysql_result ($result, $i, "email");
		 echo "<b>Name:</b> $thename<br /><b>E-Mail:</b>$theemail <hr /></p>";
			 $i++;
		 }
	 }
?>
</td>
<td valign="top">
<?php 
 
 
// prints out at top of page, so that poeple know what they are looking at//
echo "People that found your site ok:<hr /><br /><br />";
 
 
/* declar some relevant variables */
 
$Host = ""; //location of mySQL on server
$User = ""; //my username
$Pass = ""; //my password
$Name = "phpforms"; //name of the database to be used
$Table = "information"; //name of the table within the database
 
		mysql_connect ($Host, $User, $Pass, $Name, $Table) or die ("unable to connect to database");
		@mysql_select_db("$Name") or die ("unable to select DB");
		$sqlquery = "SELECT * FROM $Table WHERE opinion = 'is OK'";
		$result = mysql_query($sqlquery);
		$number = mysql_num_rows($result);
 
 
$i = 0;
 
	 if ($number < 1) 
	 {
	 echo "<center><p>No results to display!</p></center>";
	 }
	 else 
	 {
	 while ($number > $i) 
		 {
		 $thename = mysql_result ($result, $i, "name");
		 $theemail = mysql_result ($result, $i, "email");
		 echo "<b>Name:</b> $thename<br /><b>E-Mail:</b>$theemail <hr /></p>";
			 $i++;
		 }
	 }
?>
</td>
<td valign="top" bgcolor="#CCCCCC">
<?php 
 
 
// prints out at top of page, so that poeple know what they are looking at//
echo "People that liked your site:<hr /><br /><br />";
 
 
/* declar some relevant variables */
 
$Host = ""; //location of mySQL on server
$User = ""; //my username
$Pass = ""; //my password
$Name = "phpforms"; //name of the database to be used
$Table = "information"; //name of the table within the database
 
		mysql_connect ($Host, $User, $Pass, $Name, $Table) or die ("unable to connect to database");
		@mysql_select_db("$Name") or die ("unable to select DB");
		$sqlquery = "SELECT * FROM $Table WHERE opinion = 'is great'";
		$result = mysql_query($sqlquery);
		$number = mysql_num_rows($result);
 
 
$i = 0;
 
	 if ($number < 1) 
	 {
	 echo "<center><p>No results to display!</p></center>";
	 }
	 else 
	 {
	 while ($number > $i) 
		 {
		 $thename = mysql_result ($result, $i, "name");
		 $theemail = mysql_result ($result, $i, "email");
		 echo "<b>Name:</b> $thename<br /><b>E-Mail:</b>$theemail <hr /></p>";
			 $i++;
		 }
	 }
?>
</td>
</tr>
</table>
 
 
</body>
</html>

This script will connect to the phpforms databse and look for any entries and check to see if they match with the terms listed in

SELECT * FROM $Table WHERE opinion = 'is great'

Now we need a form that will submit info into another form which will then submit it's info into the database!

<html>
<HEAD>
<title> form Handling with PHP </title>
</head>
<body bgcolor="#FFFFFF">
<center>
<form method=post action="submittedinfo.php"> <!-- Submit this info to the file called submittedinfo.php -->
<input type="hidden" name="id" value="NULL">
<table>
	<tr>
	 <td colspan="2"><font SIZE="+0" face="verdana">
	 Lets see if we cant get this form to work!
	 </td>
	</tr>
	<tr>
	 <td>
	 </td>
	</tr>
	<tr>
	 <td align="left"><font SIZE="+0" face="verdana">
	 <b>Your name <br \>Your E-Mail Address</b>
	 </td>
	 <td>
	 <input type="text" name="name" id="name"> <!-- creating the name/id that will be associated with $name -->
	 <br />
	 <input type="text" name="email" id="email"> <!-- creating the name/id that will be associated with $email -->
	 </td>
	</tr>
	<tr>
	 <td colspan="2"><center>
	 <SELECT name="opinion" id="opinion"> <!-- creating the name/id that will be associated with $opinion -->
	 <!-- The values set on these next lines will be used
		 to access the databse for querying, so remember 
		 remember these values, 'is great' 'is OK' 
		 'is horrible' -->
	 <option value="is great">I like your site</option>
	 <option value="is OK">Your Site is OK</option>
	 <option value="is horrible">Your Site is horrible</option>
	 </SELECT><p><input type="submit" value="Tell us!">
	 </td>
	</tr>
</table>
</form>
</body>
</html>

All this is, is a basic HTML form that will submit info in a file called submitted.php, so now let's create that file.

<?
$DBhost = "locationofmysql";//location of mySQL on server/site
$DBuser = "username";//User name for logging onto mySQL
$DBpass = "yourpass";//Password for logging onto mySQL
$DBName = "phpforms";//Name of the databse for logging into
$Table = "information";//Name of the Table to be used steps to create are included
$name = "$_POST[name]";//Name that the person gave on the form
$email = "$_POST[email]";//Email that person gave on the form
$opinion = "$_POST[opinion]";//Their opinion
 
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); //connecting to the database using the variable set
@mysql_select_db("$DBName") or die("Unable to select database $DBName");		 //at connection to the databse select DBNAME (phpforms) or tell that it couldnt connect
$sqlquery = "INSERT INTO $Table VALUES('$id','$name','$email','$opinion')";	 //Telling the mySQL to insert the values from the form into the databse coresponding with the form
$results = mysql_query($sqlquery); //Query the results
mysql_close();
echo " 
	<html>
	<head>
	 <title> PHP and MySQL </title>
	</head>
	<body>
	 <center>
	 <table border='0' width='500'>
		<tr>";
echo "	 <td>
		 <font face='verdana' size='+0'> 
		 <center>
		 <p>You Just Entered This Information Into the Database</p>
		 </center>";
 
//display the information that user submitted in the previous form
echo "	 <blockquote>
		 <center>
			<p>
			Name : $name </p> <p>E-Mail : $email </p> <p>Opinion : $opinion
			</p>
		 </center>
		 </blockquote>
		 </td>
		</tr>
	 </table> 
		 </center>
	 </body>
	 </html>";
?>

This form will take the data from the previous form and add it to the database, and now with the databaseconnect file (listed first) you should be able to see what the users entered about your site!

I have included a zip file with all of the scripts that I used, and they have better formatting than the ones listed above.

Heres a zip file with the code. (had to fix the code, previous version had sensative information in it ;))

Hey there!

Your tutorial has really helped me so far... but i am sort of wondering something: can you do more than one 'INSERT' at the same time to the same table and same database? If so, how do you do the connection? Once at the beginning or everytime? Do you have to do a new query everytime too?

Here is my code.. i have a few if-statements depending on which and if a product has been chosen. I want to inserta new row for every if-statement. But i can't get it to work..

Would appreciate any help i can get..

Cynthia
(cynno410@student.liu.se)

<?php
$DBhost = "*****";
$DBuser = "*****";
$DBpass = "******";
$NameofDB = "******";
$Table = "bestallning_info";


$product = $_POST["chosen_product"];  //"chosen_produkt" is a //checkbox.. there is 5 of them on my site that can all be chosen at the same time, of course.
$temp = $_POST["amout"];
$input1 = ltrim($temp);


$temp2 = $_POST["amount2"];
$input2 = ltrim($temp2);


$temp3 = $_POST["amount3"];
$input3 = ltrim($temp3);


$temp4 = $_POST["amount4"];
$input4 = ltrim($temp4);


$temp5 = $_POST["amount5"];
$input5 = ltrim($temp5);


$order_id = $t;
//$produkt_id;



if ($product == "gron_randig"){
mysql_connect($DBhost, $DBuser, $DBpass) or die("Unable to connect to database");
@mysql_select_db($NameofDB) or die("Unable to select $NameofDB");


$t ='1';
$produkt_id1 ='100301';
$sqlquery = "INSERT INTO $Table VALUES ('$t','$input1','$produkt_id1')";
$results = mysql_query($sqlquery);
mysql_close();


}



if ($product == "rod_randig"){


mysql_connect($DBhost, $DBuser, $DBpass) or die("Unable to connect to database");
@mysql_select_db($NameofDB) or die("Unable to select $NameofDB");


$t ='1';
$produkt_id2 ='100302';
$sqlquery2 = "INSERT INTO $Table VALUES ('$t','$input2','$produkt_id2')";
$results2 = mysql_query($sqlquery2);
mysql_close();


}

I havn't evaluated your code in depth, but your code doesn't look half bad. Maybe double check your pre-existing table [bestallning_info] and make sure the SQL code was done correctly.

Also, maybe post the exact problem your having..

example: cant connect to db
error on line whatever

something specific would help us help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.