Add Data From Html Form To A Sql Database Using Php
I am trying to add data into a database created using phpmyadmin by entering information into a html form but whenever I click on 'submit' it goes to the 'insert.php' form and the
Solution 1:
Hopefully this will help
Form.html
<form action="insert.php" method="post">
<inputtype="text" name="data">
<inputtype="submit">
</form>
insert.php
<?php//checking if data has been enteredif( isset( $_POST['data'] ) && !empty( $_POST['data'] ) )
{
$data = $_POST['data'];
} else {
header( 'location: form.html' );
exit();
}
//setting up mysql details$sql_server = 'localhost';
$sql_user = 'user';
$sql_pwd = 'password';
$sql_db = 'database';
//connecting to sql database$myslqi = new mysqli( $sql_server, $sql_user, $sql_pwd, $sql_db ) ordie( $mysqli->error );
//inserting details into table$insert = $mysqli->query( "INSERT INTO table ( `data` ) VALUE ( '$data' )" );
//closing mysqli connection$mysqli->close;
?>
Solution 2:
Ensure you have set the form action and method.
<formaction='insert.php'method='POST'>
//HTML inputs here
</form>
Solution 3:
Some things to check first:
Have insert.php echo all the data you are passing into it via get or post. I would wager what's happening is an error with the form. Failing that, run the insert inside php admin manually (there's an sql tab once you're in your database, run your query there to make sure you don't have an sql error). If that is also fine, then post your html and we can go from there.
Solution 4:
<form action="insert.php" method="post">
Name <inputtype="text" name="name">
Last name <inputtype="text" name="last">
<inputtype="submit" name="submit" value="Send">
</form>
and your file insert.php should look like the one in the link you have (w3schools). Of course, you need to change variables to name
and last
and working MySQL connection!
Post a Comment for "Add Data From Html Form To A Sql Database Using Php"