Try this:

mysql> CREATE DATABASE addressbook;
Query OK, 1 row affected (0.00 sec)

mysql> USE addressbook;
Database changed

mysql> CREATE TABLE userData(id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 firstName VARCHAR(50) NOT NULL,
 lastName VARCHAR(50) NOT NULL,
 telephone INT(12) NOT NULL,
 email VARCHAR(100) NOT NULL);
Query OK, 0 rows affected (0.11 sec)

Now when you have the databse created and a table to store data, you
need to write a PHP page that do the trick.
Here is a simpe sample. Read all comments and look at the source code...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
<head>
        <meta http-equiv="content-type" content="application/xhtml+xml;
charset=utf-8" />
        <title>Addressbook</title>
        <meta name="generator" content="BBEdit 10.1" />
</head>
<body>
<?php
if(!isset($_POST['updateDB'])){
?>

<form action="addressbook.php" method="post">
<fieldset>
   <legend>Contact info:</legend>
   <p>
   <label for="firstName">First Name:</label><br />
   <input type="text" id="firstName" name="firstName" />
   </p>
   <p>
   <label for="lastName">Last Name:</label><br />
   <input type="text" id="lastName" name="lastName" />
   </p>
   <p>
   <label for="telephone">Telephone:</label><br />
   <input type="text" id="telephone" name="telephone" />
   </p>
   <p>
   <label for="emailAddress">Email Address:</label><br />
   <input type="text" id="emailAddress" name="emailAddress" />
   </p>
   <p>
   <input type="submit" name="updateDB" value="Update DB" />
   </p>
</fieldset>

</form>

<?php
} else {
        // Formfields
        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $telephone = $_POST['telephone'];
        $email = $_POST['emailAddress'];
        
        // Delete all kind of tags that may be found in form fields
        $firstName = strip_tags($firstName);
        $lastName = strip_tags($lastName);
        $telephone = strip_tags($telephone);
        $email = strip_tags($email);
        
        // Just a simple way to a little more secure query
        $firstName = mysql_real_escape_string($firstName);
        $lastName = mysql_real_escape_string($lastName);
        $telephone = mysql_real_escape_string($telephone);
        $email = mysql_real_escape_string($email);

        // Connect to the Database
        $connectDB = mysql_connect("localhost","root","br0ok2meg") or
die(mysql_error());
        // Select Database
        $selectDB = mysql_select_db("addressbook") or die(mysql_error());
        
        // Now create the query that insert data into table userData
        $sql = "INSERT INTO
userData(firstName,lastName,telephone,email)VALUES(\"$firstName\",\"$lastName\",\"$telephone\",\"$email\")";
        
        // Run the query that actually insert data into the table userData
        mysql_query($sql,$connectDB) or die(mysql_error());
        echo "<p>The Contact information is registred</p>";
        }
?>

<p><a href="addressbook.php?view">View Contact Info</a></p>

<?php
if(isset($_GET['view'])){
        // Connect to the Database
        $connectDB = mysql_connect("localhost","root","br0ok2meg") or
die(mysql_error());
        // Select Database
        $selectDB = mysql_select_db("addressbook") or die(mysql_error());
        
        // Ask for information in table userData
        $sql = "SELECT * FROM userData";
        $result = mysql_query($sql,$connectDB) or die(mysql_error());

        // Handle received data
        while($row = mysql_fetch_array($result)){
                $firstName = $row['firstName'];
                $lastName = $row['lastName'];
                $telephone = $row['telephone'];
                $email = $row['email'];
        
        
        // Print the result to screen
        echo "<p><strong>First Name:</strong> $firstName<br />";
        echo "<strong>Last Name:</strong> $lastName<br />";
        echo "<strong>Telephone:</strong> $telephone<br />";
        echo "<strong>Email:</strong> $email<br />";
        echo "</p>";
        }
}

?>
</body>
</html>

Hope this can be in help for you.

Karl
-- 
Hjemmeside: http://www.karl-arne.name/



Den 08:45 31. mars 2012 skrev saeed ahmed <mycomputerbo...@gmail.com> følgende:
> i have made a php script with a tutorial help....i dont know where is a
> error.please have a look
>
>
> <?php
> //connect and select a database
> mysql_connect("localhost","root"," " );
> mysql_select_db("addressbook");
> //Run a query
> $result=mysql_query("select("SELECT*FROM COLLEAGUE");
> ?>
> <!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";>
> <head>
> <title>Address Book</title>
> <meta http-equiv="content-type" content="text/html;charset=utf-8"
> </head>
> <body>
> <h1>Address Book<h1>
> <table Border="1" cellpadding="2"cellspacing="3"
> summary="table holda colleague contacts information">
> <tr>
> <th> ID</th>
> <th>First Name</th>
> <th>Last Name</th>
> <th>Telephone</th>
> <th>Email Address</th>
> </tr>
> <?php
> //LOOP through all table rows
> while ($row=mysql_fetch_array($result)) {
> echo"<tr>";
> echo"<td>".$row['id'] . "</td>;
> echo "<td>" . $row['firstName'] . "</td>";
> echo "<td>" . $row['lastName'] . "</td>";
> echo "<td>" . $row['telephone'] . "</td>";
> echo "<td>" . Srow['email'] . "</td";
> echo"</tr>";
> }
> //free result memory and close database connection
> mysql_free_result($result);
> mysql_close();
> ?>
> </table>
> </body>
> </html>
> thanks

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to