Weborum Webmaster Forum > [Beginner] Send and Retrieve Data
Help - Search - Members - Calendar
Full Version: [Beginner] Send and Retrieve Data
Weborum Webmaster Forum > TUTORIAL ARCHIVE - tutorials & scripts to save you scouring the internet. Please feel free to add your own. > PHP Tutorials & scripts
Joe
The first thing you need to do is get a list of all the information that you want to be stored ... then create your database and add the SQL code.


Basically a MySQl database is like a table, you have each field storing a separate set of data. E.G;


CODE

ID   | artistname | genre
--------------------------------
1    | Van Halen  | Rock
2    | Van Halen  | Country
3    | Metallica  | Metal
4    | Metalica   | Metal
5    | Metalllica | Metal
6    | Morrisey   | Pop
7    | Metaloaf   | Rock



This is how your data will be stored. All in separate fields.


For example, the MySQL for this little test table would be;

SQL
CREATE TABLE test (
ID int(11) NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
email varchar(30) default NULL,
KEY ID (ID)
);



This will need to go into your SQL field in PHPMyAdmin on your Cpanel or whatever it is you use.


From there you'll need to send your data to the database, then the page you want all the information displayed will read all the information from the database and display it on the page accordingly.


To send data to the database you'll need a simple form;


inputdata.php
PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />

    <title>Send Data To The Database</title>
</head>
<body>

<form method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
<p><label for="name">Name: <br />
<input type="text" name="name" id="name" size="255" tabindex="1" maxlength="255" /></label></p>

<p><label for="email">Email: <br />
<input type="text" name="email" id="email" size="255" tabindex="2" maxlength="30" /></label></p>

<p><input type="submit" name="submit" tabindex="3" value="Submit" /> <input type="reset" name="reset" tabindex="4" value="Reset" /></p>
</form>

</body>
</html>



As you can see the action of this form is <?=$_SERVER['PHP_SELF'] ?>, which means it will run the script which appears after the form. The PHP code is below.


This is the PHP to send data into a database;


PHP

<?php
if(isset($_POST['submit'])) {

$dbHost = 'localhost';
$dbUser = 'username';
$dbPass = 'password';
$dbname = 'databasename';

function
safeaddslashes($string) {
   if (
get_magic_quotes_gpc()) {
       return
$string;
   } else {
       return
addslashes($string);
   }
}

$name = $_POST['name'];
$email = $_POST['email'];

$name = safeaddslashes($name);
$email = safeaddslashes($email);

$db = mysql_connect($dbHost,$dbUser,$dbPass);
$db_selected = mysql_select_db($dbname,$db);
if (!
$db_selected) { die('Error : ' . mysql_error()); }

$sql="INSERT INTO test (name, email) VALUES ('$name', '$email')";
$result = mysql_query($sql, $db);
if (!
$result) { die('Invalid query: ' . mysql_error()); }

if(
$result) {
   echo
'<p>Data successfully added!  Now view the <a href="fetchdata.php">data</a>.</p>';
   exit();
}
else {
   echo
'<p>Error adding data!</p>';
}

exit();
}
?>




And so it all goes together like;


PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />

    <title>Send Data To The Database</title>
</head>
<body>

<form method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
<p><label for="name">Name: <br />
<input type="text" name="name" id="name" size="255" tabindex="1" maxlength="255" /></label></p>

<p><label for="email">Email: <br />
<input type="text" name="email" id="email" size="255" tabindex="2" maxlength="30" /></label></p>

<p><input type="submit" name="submit" tabindex="3" value="Submit" /> <input type="reset" name="reset" tabindex="4" value="Reset" /></p>
</form>

<?php
if(isset($_POST['submit'])) {

$dbHost = 'localhost';
$dbUser = 'username';
$dbPass = 'password';
$dbname = 'databasename';

function
safeaddslashes($string) {
   if (
get_magic_quotes_gpc()) {
       return
$string;
   } else {
       return
addslashes($string);
   }
}

$name = $_POST['name'];
$email = $_POST['email'];

$name = safeaddslashes($name);
$email = safeaddslashes($email);

$db = mysql_connect($dbHost,$dbUser,$dbPass);
$db_selected = mysql_select_db($dbname,$db);
if (!
$db_selected) { die('Error : ' . mysql_error()); }

$sql="INSERT INTO test (name, email) VALUES ('$name', '$email')";
$result = mysql_query($sql, $db);
if (!
$result) { die('Invalid query: ' . mysql_error()); }

if(
$result) {
   echo
'<p>Data successfully added!  Now view the <a href="fetchdata.php">data</a>.</p>';
   exit();
}
else {
   echo
'<p>Error adding data!</p>';
}

exit();
}
?>

</body>
</html>



And then we need to grab the data from our test database; we use the following code;


fetchdata.php
PHP

<?php
$dbHost
= 'localhost';
$dbUser = 'username';
$dbPass = 'password';
$dbname = 'databasename';

$db = mysql_connect($dbHost,$dbUser,$dbPass);
$db_selected = mysql_select_db($dbname,$db);
if (!
$db_selected) { die('Error : ' . mysql_error()); }

$sql = "SELECT * FROM test";
$result = mysql_query($sql,$db);

while (
$article = mysql_fetch_object($result))
{
echo
'<p><strong>Name: </strong> '.$article->name.'</p>'."\n";
echo
'<p><strong>Email: </strong> '.$article->email.'</p>'."\n";
}

mysql_free_result($result);
?>



Put it all together and see what you get wink.gif
hahaman
I have the same question
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.