Share on Google+Share on Google+

Php Sql Query Insert

Advertisement
This example illustrates how to execute insert query with values in php application.

Php Sql Query Insert

 This example illustrates how to execute insert query with values in php application.

In this example we create two mysql query for insert statement with separate values in the database table. The table before insertion and after insertion is as follows and the complete source code given below with output.

 

Table: users before insertion

Table: users after insertion

 

Source Code of insertquery.php 

<?php
  $con = mysql_connect("localhost","root","root");
  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("test", $con);

  mysql_query("INSERT INTO users (username, password, email) VALUES ('Gaurvi', 
                    'gaurvi', 'gaurvi@email.com')"
);

  mysql_query("INSERT INTO users (username, password, email) VALUES ('Hardeep', 
                    'hardeep', 'hardeep@email.com')"
);

  $result = mysql_query("SELECT * FROM users ORDER BY username desc");
  echo "<table border='1'>
    <tr>
      <th>Name</th>
      <th>password</th>
      <th>Email</th>
    </tr>";
    while ($row = mysql_fetch_array($result)) {
      echo "<tr>";
      echo "<td>" . $row['username'] "</td>";
      echo "<td>" . $row['password'] "</td>";
      echo "<td>" . $row['email'] "</td>";
      echo "</tr>";
    }
  echo "</table>";

  mysql_close($con);
?>

Download Source Code

 

Output:

Advertisements

Advertisement

Posted on: December 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.