PHP MySQL Insert Into






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

Insert Data Into a Database Table

 

INSERT INTO table_name
VALUES (value1, value2,....)

atau

INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)

Program25-1.php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO person (FirstName, LastName, Age) 
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO person (FirstName, LastName, Age) 
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?>


Insert Data From a Form Into a Database

 

Program25-2.php
<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>

insert.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_db", $con);
$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
mysql_close($con)
?>

0 komentar:

Post a Comment

Copyright © 2014 Dunia Naeta All Right Reserved