使用PHP将数据从html表单添加到sql数据库 [英] Add data from html form to a sql database using PHP
问题描述
我试图将数据添加到使用phpmyadmin创建的数据库中,方法是将信息输入到html表单中,但每次点击提交时都会转到insert.php表单,而我得到的错误是'没有收到数据'这是我的insert.php代码。我在这个网站上直接输入了相同的代码。 http://www.w3schools.com/php/php_mysql_insert.asp (the将数据从表单插入到数据库部分。我使用XAMPP作为我的服务器。有什么建议? 解决方案
希望这会帮助
Form.html
< form action =insert .phpmethod =post>
< input type =textname =data>
< input type =submit>
< form>
insert.php
<$ p $ ($ _POST ['data'])&&!empty($ _POST ['data'])
//检查数据是否已输入
'data']))
{
$ data = $ _POST ['data'];
} else {
header('location:form.html');
exit();
}
//设置mysql的详细信息
$ sql_server ='localhost';
$ sql_user ='user';
$ sql_pwd ='密码';
$ sql_db ='database';
//连接到sql数据库
$ myslqi = new mysqli($ sql_server,$ sql_user,$ sql_pwd,$ sql_db)或死($ mysqli->错误);
//在表格中插入详细信息
$ insert = $ mysqli-> query(INSERT INTO table(`data`)VALUE('$ data'));
//关闭mysqli连接
$ mysqli-> close;
?>
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 error i get is 'no data received' This is my insert.php code. I have literally type out the same code in this website. "http://www.w3schools.com/php/php_mysql_insert.asp" (the Insert Data From a Form Into a Database section". I am using XAMPP as my server. Any suggestions?
Hopefully this will help
Form.html
<form action="insert.php" method="post">
<input type="text" name="data">
<input type="submit">
</form>
insert.php
<?php
//checking if data has been entered
if( 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 ) or die( $mysqli->error );
//inserting details into table
$insert = $mysqli->query( "INSERT INTO table ( `data` ) VALUE ( '$data' )" );
//closing mysqli connection
$mysqli->close;
?>
这篇关于使用PHP将数据从html表单添加到sql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!