![]() |
| ||
| submiting info into a database for retrieval! 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: [php]<!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> [/php] 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> 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. [php]<? $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>"; ?> [/php] 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. |
| ||
| Re: submiting info into a database for retrieval! 1 Attachment(s) Heres a zip file with the code. (had to fix the code, previous version had sensative information in it ;)) |
| ||
| Re: submiting info into a database for retrieval! 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(); } |
| ||
| Re: submiting info into a database for retrieval! 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. |
| All times are GMT -4. The time now is 8:20 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC