Results 1 to 9 of 9

Thread: Insert Data Into a Database Table

  1. #1
    Join Date
    Apr 2005
    Posts
    46,704

    Default Insert Data Into a Database Table

    The INSERT INTO statement is used to add new records to a database table.

    Code:
    <?php
    
    $link = mysql_connect("servername", "username", "password") or die("unable to connect to mysql");
    $db  = mysql_select_db("database_name ") or die("unable to select DB");
    
    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('', '', '')");
    
    mysql_close($link);
    
    ?>
    Last edited by minisoji; 11-29-2011 at 11:01 AM.

  2. #2
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    Insert Data From a Form Into a Database

    Code:
    <html>
    <body>
    
    <form action="insert.php" method="post">
    Firstname: <input type="text" name="firstname" />
    Lastname: <input type="text" name="lastname" />
    Age: <input type="text" name="age" />
    <input type="submit" />
    </form>
    
    </body>
    </html> 
    
    <?php
    
    $link = mysql_connect("servername", "username", "password") or die("unable to connect to mysql");
    $db  = mysql_select_db("database_name ") or die("unable to select DB");
    
    $sql="INSERT INTO Persons (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
    
    if (!mysql_query($sql,$link))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    
    mysql_close($link)
    ?>
    Last edited by minisoji; 12-02-2011 at 05:50 AM.

  3. #3
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    Select Data From a Database Table

    The following example selects all the data stored in the "Persons" table

    Code:
    <?php
    $link = mysql_connect("servername", "username", "password") or die("unable to connect to mysql");
    $db  = mysql_select_db("database_name ") or die("unable to select DB");
    
    $result = mysql_query("SELECT * FROM Persons");
    
    while($row = mysql_fetch_array($result))
      {
      echo $row['FirstName'] . " " . $row['LastName'];
      echo "<br />";
      }
    
    mysql_close($link);
    ?>

  4. #4
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    Display the above Result in an HTML Table

    Code:
    <?php
    $link = mysql_connect("servername", "username", "password") or die("unable to connect to mysql");
    $db  = mysql_select_db("database_name ") or die("unable to select DB");
    
    $result = mysql_query("SELECT * FROM Persons");
    
    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['FirstName'] . "</td>";
      echo "<td>" . $row['LastName'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($link);
    ?>

  5. #5
    Join Date
    Dec 2011
    Posts
    5

    Default

    Thanks! that was very useful

  6. #6
    Join Date
    Dec 2011
    Posts
    5

    Default

    pretty nice

  7. #7
    Join Date
    Jan 2012
    Posts
    197

    Default

    Helpful coding.Thanks for posting.

  8. #8
    Join Date
    Apr 2012
    Posts
    6

    Default

    very nice ty

  9. #9
    Join Date
    Nov 2013
    Posts
    6

    Default

    good one

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •