[英]How to Insert to MySQL with php and echo it
I'm really new to PHP and need some help. 我真的是PHP新手,需要一些帮助。
I want to build a forum where you can upload posts and pictures. 我想建立一个论坛,您可以在其中上传帖子和图片。
Now, I need help with the connection between my code and my MySQL database. 现在,我需要有关代码和MySQL数据库之间连接的帮助。
I'm using MySQL with the folowing table: 我在下面的表中使用MySQL:
main table: "posts" 主表:“帖子”
I want to post the data that I get from the user and I don't really know what the problem is. 我想发布从用户那里获得的数据,但我真的不知道问题出在哪里。
This is the PHP code: 这是PHP代码:
<?php
session_start();
$MySQLdb = new PDO("mysql:host=127.0.0.1;dbname=forum", "root", "");
$MySQLdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['msg'])){
$post = $_POST['msg'];
$cursor = $MySQLdb->prepare("SELECT post_data FROM posts WHERE post=:posts_data");
$cursor->execute( array(":post_data"=>$post) );
}
$cursor = $MySQLdb->prepare("INSERT INTO posts (post) value (:post_data)");
$cursor->execute(array(":post_data"=>$post));?>
and this is the HTML code for "msg" (the post input): 这是“ msg”的HTML代码(帖子输入):
div class="input-group">
<input id="msg" type="text" method="POST" class="form-control" name="msg" placeholder="Write your message here...">
<span class="input-group-addon"><button id="send_post">Send</button></span>
</div>
This is the error that I get: 这是我得到的错误:
Notice: Undefined variable: post in C:\\xampp\\htdocs\\forum\\main.php on line 15 注意:未定义的变量:在第15行的C:\\ xampp \\ htdocs \\ forum \\ main.php中发布
Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'post' in 'field list' in C:\\xampp\\htdocs\\forum\\main.php:15 Stack trace: #0 C:\\xampp\\htdocs\\forum\\main.php(15): PDOStatement->execute(Array) #1 {main} thrown in C:\\xampp\\htdocs\\forum\\main.php on line 15 致命错误:未捕获的PDOException:SQLSTATE [42S22]:找不到列:1054 C:\\ xampp \\ htdocs \\ forum \\ main.php:15中的“字段列表”中的未知列“ post”堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ forum \\ main.php(15):PDOStatement-> execute(Array)#1 {main}在第15行的C:\\ xampp \\ htdocs \\ forum \\ main.php中抛出
Thanks a lot. 非常感谢。
Your html is all wrong. 您的html都是错误的。
<input id="msg" type="text" method="POST" class="form-control" name="msg" placeholder="Write your message here...">
The method is an attribute on form tag, not input. 该方法是表单标签上的属性,而不是输入。
<form action="/youraction" method="POST" />
<input id="msg" type="text" method="POST" class="form-control" name="msg" placeholder="Write your message here...">
</form>
PHP fails because there's no $_POST['msg']
set PHP失败,因为未设置$_POST['msg']
Perhaps method your html form is get or ... 也许您的HTML表单的方法是get或...
Change $_POST['msg'];
更改$_POST['msg'];
to $_REQUEST['msg'];
到$_REQUEST['msg'];
<?php
session_start();
$MySQLdb = new PDO("mysql:host=127.0.0.1;dbname=forum", "root", "");
$MySQLdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$post ="";
if(isset($_REQUEST['msg'])){
$post = $_REQUEST['msg'];
$cursor = $MySQLdb->prepare("SELECT post_data FROM posts WHERE post=:posts_data");
$cursor->execute( array(":post_data"=>$post) );
}
$cursor = $MySQLdb->prepare("INSERT INTO posts (post) value (:post_data)");
$cursor->execute(array(":post_data"=>$post));?>
Which of the lines is Line 15? 第15行是哪几行? is it the $cursor = $MySQLdb->prepare("INSERT INTO posts (post) value (:post_data)");
是$cursor = $MySQLdb->prepare("INSERT INTO posts (post) value (:post_data)");
Because if it so then you might want to find a way of SETTING you $post
variable. 因为如果是这样,那么您可能想找到一种设置$post
变量的方法。 Take a look at your code, you are setting $post
inside the if(){} statement
and so when it tries to execute the below lines: 看一下您的代码,您正在if(){} statement
中设置$post
,因此当它尝试执行以下行时:
$cursor = $MySQLdb->prepare("INSERT INTO posts (post) value (:post_data)");
$cursor->execute(array(":post_data"=>$post));?>
Your $post
variable is empty. 您的$post
变量为空。
"INSERT INTO posts (post) value (:post_data)" must be changed to "INSERT INTO posts (post_data) value (:post_data)" . 必须将“ INSERT INTO帖子(帖子)值(:post_data)”更改为“ INSERT INTO帖子(帖子)值(:post_data)” 。
Please be aware, that there might come new MySQL errors depending on your table definition and the defined constraints. 请注意,根据表定义和定义的约束,可能会出现新的MySQL错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.