PHP我这个代码为什么提交什么数据都插入到数据的内容都一样呢?

WBOY
Release: 2016-06-06 20:21:48
Original
842 people have browsed it

不管提交什么数据都是插入图片里面的内容 也就是把name自身插入进去了 没有把name的值插进去 这是为什么? 谁能帮我试一下我的代码呢 谢谢
PHP我这个代码为什么提交什么数据都插入到数据的内容都一样呢?

这是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>
Copy after login
Copy after login

这是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>
Copy after login
Copy after login

回复内容:

不管提交什么数据都是插入图片里面的内容 也就是把name自身插入进去了 没有把name的值插进去 这是为什么? 谁能帮我试一下我的代码呢 谢谢
PHP我这个代码为什么提交什么数据都插入到数据的内容都一样呢?

这是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>
Copy after login
Copy after login

这是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>
Copy after login
Copy after login

既然你使用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>
Copy after login

额,其实我也是小白,'{$_POST['name']}' 这里面的name旁边的引号需不需要转义,因为外面已经有引号了,我没试过,会不会是这个问题

表单都没传值(value),提交之后打印出来看下数据是否正确

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template