简体   繁体   English

如何使用php插入MySQL并回显它

[英]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" 主表:“帖子”

  1. post_data = it's an POST request from the user. post_data =这是来自用户的POST请求。
  2. username = from another main table: users 用户名=来自另一个主表:用户
  3. user_id = from another main table: users user_id =来自另一个主表:users
  4. post_id = Automatic . post_id =自动。

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错误。

插入<!--?php echo in <?php if</div--><div id="text_translate"><pre> <?php if($range == 'arizona'): ?> <?php echo $image->county; ?> <?php endif; ?></pre><p> 好的,这段代码有效,回显 output 是:“arizona”</p><p> 现在我想用回声替换第一行的“亚利桑那”。 我试过:</p><pre> <?php if($range == $image->county;): ?> <?php echo $image->county; ?> <?php endif; ?></pre><p> 但没有工作。 我怎样才能包括:</p><pre> $image->county;</pre><p> 在第一行?</p></div> - Insert <?php echo in <?php if

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Php mysql和mamp插入和回显复选框 - how to Insert and echo checkboxes with Php mysql and mamp 在MySQL数据库表中插入PHP Echo值 - Insert PHP Echo Value in MySQL Database Table 如果INSERT IGNORE回显“查询被忽略”? (mysql / php) - If INSERT IGNORE echo “Query was ignored”? (mysql/php) PHP / MySQL不会INSERT和回应它应该吗? - PHP/MySQL won't INSERT and echo as it should? 如何使用 PHP 这个 MySQL 命令回显 - How to echo with PHP this MySQL command 如何通过MySQL在PHP中回显表 - how to echo table in php by mysql 插入<!--?php echo in <?php if</div--><div id="text_translate"><pre> <?php if($range == 'arizona'): ?> <?php echo $image->county; ?> <?php endif; ?></pre><p> 好的,这段代码有效,回显 output 是:“arizona”</p><p> 现在我想用回声替换第一行的“亚利桑那”。 我试过:</p><pre> <?php if($range == $image->county;): ?> <?php echo $image->county; ?> <?php endif; ?></pre><p> 但没有工作。 我怎样才能包括:</p><pre> $image->county;</pre><p> 在第一行?</p></div> - Insert <?php echo in <?php if 如何从使用php echo显示的html表中的mysql中插入多行 - How can I insert multiple rows in mysql from an html table that used php echo for display 如何在$ output变量上插入php echo - How to insert php echo on $output variable 无法发布并插入数据库并回显结果AJAX / PHP / MYSQL - couldn't post and insert to database and echo result AJAX/PHP/MYSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM