通过PHP发送HTML形式的数据到SQL数据库(使用的mysqli)(Send html form

2019-07-21 07:04发布

我想输入到一个html表单中的数据发送到我的SQL数据库,即创建一个新的行归于一定值某些列。 我知道有类似的问题,我读了答案,但似乎没有任何工作。

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 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>

我的分贝“的帖子”表colums:

pid
title
contents
tags
category

pidauto_increment

我已经尝试发送值的所有colunes,包括pid ,并在“右”的顺序。

mysqli_connect部分不是问题,因为我从我的一个不同的PHP文件的作品复制它。

服务器的PHP-SQL兼容性不是问题,因为相关我成功有不同的PHP文件从DB(这是手动插入数据)中检索数据。

Answer 1:

改变这种

<form onSubmit="send_post.php" method="post">

<form action="send_post.php" method="post">


Answer 2:

$connect = mysqli_connect("my host","my user","my password","my db");

不要忘了更正所有拼写错误,可当你不知道什么错误麻烦。 (拼错密码作为密码)



文章来源: Send html form data to sql database via php (using mysqli)