> Online tutorial : insert form data into database php
Showing posts with label insert form data into database php. Show all posts
Showing posts with label insert form data into database php. Show all posts

Opening a File in php


Opening a File in php

The fopen() function is used to open files in PHP.
Syntax:

fopen('file name','mode of operation')
  • The first parameter of this function contains the name of the file to be opened
  • the second parameter specifies in which mode the file should be opened:

  • <html>
    <body>
    <?php
    $file=fopen("welcome.txt","r");
    ?>
    </body>
    </html> 





update query in php

Update data from Php:

  In this tutorial we are show the table value can be update from old one to new one.In this example name in the field value henry will changed to the mathavan

Syntax:

update table_name set attri_name='value' where attr_value='value to be changed'

this syntax for update value in table


<?php
$conn=mysql_connect("localhost","root","");
$sel=mysql_select_db("proj",$conn);
$c = mysql_query("select * from data");
echo "<table border='1'>";
echo "<tr><td>name</td><td>Last name</td><td>Address</td><td>Email id</td><td>Phone</td></tr>";
while($fetch = mysql_fetch_array($c))
{
echo "<tr><td>$fetch[0]</td><td>$fetch[1]</td><td>$fetch[2]</td><td>$fetch[3]</td><td>$fetch[4]</td></tr>";
}
echo "</table>";
$q=mysql_query("update data set name='mathavan' where name='kenry'");
if($q)
{
echo"update";
}
$c = mysql_query("select *from data");
echo "after updating";
echo "<table border=
'1'>";
echo "<tr><td>name</td><td>Last name</td><td>Address</td><td>Email id</td><td>Phone</td></tr>";
while($row = mysql_fetch_array($c))
{
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td></tr>";
}
echo "</table>";
?>
Output




select query in php

select query in php
From the select query in php.we are going to select entire or extract data from the table .we are using mysql_query()to retrieve the data.here example for go to get data from database.

?php
$conn=mysql_connect("localhost","root","");
$sel=mysql_select_db("proj",$conn);
$c = mysql_query("select * from data");

echo "<table border='1'>";
echo "<tr><td>name</td><td>Last name</td><td>Address</td><td>Email id</td><td>Phone</td></tr>";
while($row = mysql_fetch_array($c))
{
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td></tr>";
}
echo "</table>";




?>

Output




Buy it 40% discount

insert form data into database php

Insert Data From a Form Into a Database
Now we will create an HTML form that can be used to add new records to the "data" table.
Here is the HTML form:

<html>
<body>
<form action="in.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>

                    When a user clicks the submit button in the HTML form in the example above, the form data is sent to "in.php". The "in.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables. Then, the mysql_query() function executes the INSERT INTO statement, and a new record will be added to the database table.

Below is the code in the "in.php" page:


<?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 "one record added";
mysql_close($con)
?>
Buy it 40% discount