php 通过php将html表单数据发送到sql数据库(使用mysqli)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15331477/
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
Send html form data to sql database via php (using mysqli)
提问by Alex
I want to send the data inputted into an html form to my sql database, i.e., create a new row attributing certain values to certain columns. I know there are similar questions, I read the answers but nothing seems to work.
我想将输入到 html 表单中的数据发送到我的 sql 数据库,即创建一个新行,将某些值归因于某些列。我知道有类似的问题,我阅读了答案,但似乎没有任何效果。
send_post.php
send_post.php
<?php
//Connecting to sql db.
$connect = mysqli_connect("my host","my user","my passwrod","my db");
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO posts (category, title, contents, tags)
VALUES ('$_POST[post_category]', '$_POST[post_title]', '$_POST[post_contents]', '$_POST[post_tags]')";
?>
post.html#form
post.html#form
<form onSubmit="send_post.php" method="post">
<h3>Category:</h3>
<input type="text" name="post_category">
<h3>Post title:</h3>
<input type="text" name="post_title">
<h3>Post tags (a,b,c...):</h3>
<input type="text" name="post_tags">
<h3>Post (use html):</h3>
<textarea rows="20" cols="50" name="post_contents"></textarea>
<input type="submit">
</form>
my db "posts" table colums:
我的数据库“帖子”表列:
pid
title
contents
tags
category
pidhas auto_incrementon
pid具有auto_increment上
I have already tried sending values to all colunes, including pid, and in the "right" order.
我已经尝试将值发送到所有 colunes,包括pid, 并以“正确”的顺序。
The mysqli_connectpart isn't the issue since I copied it from a different .php file of mine that works.
这mysqli_connect部分不是问题,因为我从我的一个不同的 .php 文件中复制了它。
Server php-sql compatibility isn't the issue either, since I successfully had a different .php file retrieve data from the db (data which was manually inserted).
服务器 php-sql 兼容性也不是问题,因为我成功地有一个不同的 .php 文件从数据库中检索数据(手动插入的数据)。
回答by Yogesh Suthar
change this
改变这个
<form onSubmit="send_post.php" method="post">
to
到
<form action="send_post.php" method="post">
回答by Kosmic
$connect = mysqli_connect("my host","my user","my password","my db");
Don't forget to correct all spelling mistakes, can be a hassle when you don't know what is going wrong. (misspelt password as password)
不要忘记纠正所有拼写错误,当您不知道出了什么问题时可能会很麻烦。(错误的密码作为密码)

