943,892 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6344
  • PHP RSS
Jun 13th, 2004
0

submiting info into a database for retrieval!

Expand Post »
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
PHP Syntax (Toggle Plain Text)
  1. 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!

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <HEAD>
  3. <title> form Handling with PHP </title>
  4. </head>
  5. <body bgcolor="#FFFFFF">
  6. <center>
  7. <form method=post action="submittedinfo.php"> <!-- Submit this info to the file called submittedinfo.php -->
  8. <input type="hidden" name="id" value="NULL">
  9. <table>
  10. <tr>
  11. <td colspan="2"><font SIZE="+0" face="verdana">
  12. Lets see if we cant get this form to work!
  13. </td>
  14. </tr>
  15. <tr>
  16. <td>
  17. </td>
  18. </tr>
  19. <tr>
  20. <td align="left"><font SIZE="+0" face="verdana">
  21. <b>Your name <br \>Your E-Mail Address</b>
  22. </td>
  23. <td>
  24. <input type="text" name="name" id="name"> <!-- creating the name/id that will be associated with $name -->
  25. <br />
  26. <input type="text" name="email" id="email"> <!-- creating the name/id that will be associated with $email -->
  27. </td>
  28. </tr>
  29. <tr>
  30. <td colspan="2"><center>
  31. <SELECT name="opinion" id="opinion"> <!-- creating the name/id that will be associated with $opinion -->
  32. <!-- The values set on these next lines will be used
  33. to access the databse for querying, so remember
  34. remember these values, 'is great' 'is OK'
  35. 'is horrible' -->
  36. <option value="is great">I like your site</option>
  37. <option value="is OK">Your Site is OK</option>
  38. <option value="is horrible">Your Site is horrible</option>
  39. </SELECT><p><input type="submit" value="Tell us!">
  40. </td>
  41. </tr>
  42. </table>
  43. </form>
  44. </body>
  45. </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.
Last edited by happygeek; Oct 28th, 2006 at 11:28 am. Reason: Formatting
Similar Threads
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 13th, 2004
0

Re: submiting info into a database for retrieval!

Heres a zip file with the code. (had to fix the code, previous version had sensative information in it )
Attached Files
File Type: zip database.zip (2.6 KB, 59 views)
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
May 12th, 2005
0

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();

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Angelik is offline Offline
2 posts
since May 2005
Dec 28th, 2006
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snipertomcat is offline Offline
2 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: user management software
Next Thread in PHP Forum Timeline: Array Sorting issue--php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC