Add record to my_database/my_table : mysql_query « MySQL Database « PHP






Add record to my_database/my_table

 
<html>
<body>
<?php
    $self =  $_SERVER['PHP_SELF'];
    $id =    $_POST['id'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
?>

<form action="<?php echo( $self ); ?>" method="post">
ID: <input type="text" name="id" size="3">
First Name: <input type="text" name="fname" size="8">
Last Name: <input type="text" name="lname" size="8"><br>
<input type="submit" value="Submit">
</form>

<?php

if( $id and $fname and $lname){
 $conn=@mysql_connect( "localhost", "userName", "password" ) or die( "Err:Conn" );

 $rs = @mysql_select_db( "my_database", $conn) or die( "Err:Db" );

 $sql = "insert into my_table ( id, first_name, last_name ) values ( $id, \"$fname\", \"$lname\" )";

 $rs = mysql_query( $sql, $conn );

 if( $rs ){
   echo( "Record added:$id $fname $lname" );
 }
}
 
?>

</body></html>
  
  








Related examples in the same category

1.Adding a Row to a Table
2.Deleting Data
3.Get data from mysql
4.mysql_query.php
5.function mysql_query() queries the database.
6.Storing Information in a Database
7.Using PHP variables wherever you want inside SQL queries
8.Sign the guestbook