PHP我这个代码为什么提交什么数据都插入到数据的内容都一样呢?
不管提交什么数据都是插入图片里面的内容 也就是把name自身插入进去了 没有把name的值插进去 这是为什么? 谁能帮我试一下我的代码呢 谢谢
这是1.php
<code><?php $db = new PDO('mysql:host=localhost;dbname=t1', 'root', '',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")); $rs = $db -> prepare("select * from `baidu`;"); $rs -> execute(); $list = $rs -> fetchAll(PDO::FETCH_ASSOC); ?> <div id="content"> <?php foreach($list as $v){ echo $v['type'].'<br />'; echo $v['name'].'<br>'; echo $v['num'].'<br>'; echo $v['site'].'<br>'; echo $v['content'].'<br>'; } ?> </div> <div id="div1"> <form action="2.php" method="post"> <div> <select name="select"> <option>请选择</option> <option>111</option> <option>222</option> <option>333</option> <option>444</option> </select> <input type="text" id="ipt1" name="name"> <input type="text" id="ipt2" name="num"> <input type="text" id="ipt3" name="site"> </div> <textarea id="txt1" name="content"></textarea><br> <button type="submit">发布</button> </form> </div> </code>
这是2.php
<code><?php $pdo=new PDO("mysql:host=localhost;dbname=t1","root",""); if($pdo->exec("INSERT INTO baidu( type, name, num, site, content ) VALUES ('{$_POST['select']}','{$_POST['name']}','{$_POST['num']}','{$_POST['site']}','{$_POST['content']}')"){ echo "<script>alert('发布成功');location='1.php';</script>"; } ?></code>
回复内容:
不管提交什么数据都是插入图片里面的内容 也就是把name自身插入进去了 没有把name的值插进去 这是为什么? 谁能帮我试一下我的代码呢 谢谢
这是1.php
<code><?php $db = new PDO('mysql:host=localhost;dbname=t1', 'root', '',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")); $rs = $db -> prepare("select * from `baidu`;"); $rs -> execute(); $list = $rs -> fetchAll(PDO::FETCH_ASSOC); ?> <div id="content"> <?php foreach($list as $v){ echo $v['type'].'<br />'; echo $v['name'].'<br>'; echo $v['num'].'<br>'; echo $v['site'].'<br>'; echo $v['content'].'<br>'; } ?> </div> <div id="div1"> <form action="2.php" method="post"> <div> <select name="select"> <option>请选择</option> <option>111</option> <option>222</option> <option>333</option> <option>444</option> </select> <input type="text" id="ipt1" name="name"> <input type="text" id="ipt2" name="num"> <input type="text" id="ipt3" name="site"> </div> <textarea id="txt1" name="content"></textarea><br> <button type="submit">发布</button> </form> </div> </code>
这是2.php
<code><?php $pdo=new PDO("mysql:host=localhost;dbname=t1","root",""); if($pdo->exec("INSERT INTO baidu( type, name, num, site, content ) VALUES ('{$_POST['select']}','{$_POST['name']}','{$_POST['num']}','{$_POST['site']}','{$_POST['content']}')"){ echo "<script>alert('发布成功');location='1.php';</script>"; } ?></code>
既然你使用PDO, 那2.php 的插入逻辑改下:
<code>$sql = "INSERT INTO baidu(type,name,num,site,content) VALUES (:type,:name,:num,:site,:content)"; $stmt = $pdo->prepare($sql); $stmt->execute(array(':type' => $_POST['type'], ':name' => ....)); </code>
额,其实我也是小白,'{$_POST['name']}' 这里面的name旁边的引号需不需要转义,因为外面已经有引号了,我没试过,会不会是这个问题
表单都没传值(value),提交之后打印出来看下数据是否正确

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
