php 使用PHP将表单数据插入sql server数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17723727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert form data into sql server database using PHP
提问by user2587986
I am a beginner of web design. I have a database set up and just want to insert form data into my database using PHP. I have code like these but when click submit button it goes to PHP code page. Need help. Thanks
我是网页设计的初学者。我设置了一个数据库,只想使用 PHP 将表单数据插入到我的数据库中。我有这样的代码,但是当单击提交按钮时,它会转到 PHP 代码页。需要帮忙。谢谢
<!DOCTYPE html>
<html>
<body>
<form name="input" action="test.php" method="post">
First name: <input type="text" name="FirstName" value="Mickey"><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['sumit'])){
$Fname=$_POST['FirstName'];
$Lname=$_POST['LastName'];
mysql_connect("server", "xxxxxx", "xxxxxx") or die(mysql_error());
mysql_select_db("test_name") or die(mysql_error());
mysql_query("INSERT INTO `FirstName`,'LastName' VALUES ('$Fname', '$Lname')");
Print "Your information has been successfully added to the database."; }
?>
<p>If you click the "Submit" button, the form-data will be sent to a my test table test_name.</p>
回答by Fallen
Change
action=test.php
toaction=''
if you want to submit the form to the same pageCorrect your sql query
mysql_query("INSERT INTO TABLE_NAME(FirstName,LastName) VALUES ('$Fname', '$Lname')")
;mysql_* functions are deprecated now. Skip mysql_* functions. Try mysqli_* or PDO
- instead of
if(isset($_POST['submit'])){
it's better to tryif($_SERVER['REQEST_METHOD']=="POST"){
to see if the form was submitted via POST method or not
如果要将表单提交到同一页面,请更改
action=test.php
为action=''
更正您的 sql 查询
mysql_query("INSERT INTO TABLE_NAME(FirstName,LastName) VALUES ('$Fname', '$Lname')")
;mysql_* 函数现在已弃用。跳过 mysql_* 函数。试试 mysqli_* 或 PDO
- 而不是
if(isset($_POST['submit'])){
最好尝试if($_SERVER['REQEST_METHOD']=="POST"){
查看表单是否通过 POST 方法提交
回答by Chinmay235
<input type="submit" value="Submit">
everything ok but you need to add one attribue look like
一切正常,但你需要添加一个属性看起来像
<input type="submit" value="Submit" name="sumit">
missing table name
缺少表名
mysql_query("INSERT INTO table_name (`FirstName`,'LastName') VALUES ('$Fname', '$Lname')");
回答by Chinmay235
changeaction="test.php"
to your localhost server address
更改action="test.php"
为您的本地主机服务器地址
回答by Chelal
error sumit should be submit
应该提交错误 sumit
Check to ensure names in this [''] brackets are same in the database and the form html form of the value name you used to POST or any form you used. Capitalization matters.
检查以确保此 [''] 括号中的名称在数据库中与用于 POST 的值名称的表单 html 表单或您使用的任何表单相同。资本化问题。
回答by mvw
Your insert seems to lack a table name, see http://www.w3schools.com/sql/sql_insert.asp
您的插入似乎缺少表名,请参阅http://www.w3schools.com/sql/sql_insert.asp
回答by Virus721
Where have you set : $_POST['sumit']
There is no input with such a name -> it is not set -> you never reach the code in "if".
Also forms do not have a name attribute.
您在哪里设置:$_POST['sumit']
没有输入具有这样的名称-> 未设置-> 您永远不会到达“if”中的代码。表单也没有 name 属性。
回答by Lorenzo Marcon
You wrote $_POST['sumit']
instead of $_POST['submit']
你写$_POST['sumit']
而不是$_POST['submit']
回答by Prasanth Bendra
"it goes to PHP code page" => Do you save files in server? if so check the below points
Change
if(isset($_POST['sumit'])){
toif(isset($_POST['submit'])){
Change
mysql_query("INSERT INTO
FirstName,'LastName' VALUES ('$Fname', '$Lname')");
tomysql_query("INSERT INTO tablename
FirstName,'LastName' VALUES ('$Fname', '$Lname')");
You are missing table name
“它转到 PHP 代码页” => 您是否将文件保存在服务器中?如果是这样,请检查以下几点
更改
if(isset($_POST['sumit'])){
为if(isset($_POST['submit'])){
更改
mysql_query("INSERT INTO
名字,'LastName' VALUES ('$Fname', '$Lname')");
到mysql_query("INSERT INTO tablename
名字,'LastName' VALUES ('$Fname', '$Lname')");
你缺少表名