php - pdo 查询更改表其中列名不存在?

标签 php mysql pdo

 <?php
   $month=$_SESSION['month'];
    $colname=$_SESSION['colname'];
require('connect.php');
global $pdo;
 $stmt=$pdo->prepare('SHOW COLUMNS FROM `selections` LIKE ":s%"');
$stmt=bindParam(':s',$colname);$stmt->execute();$row =$stmt->fetch()?true:false;
if($row==false){$sql=$pdo->prepare("ALTER TABLE  `$month` ADD  `$colname` VARCHAR( 120)NOT NULL DEFAULT 'absent'");
$sql->execute();}else{die("error".print_r($sql>errorinfo()));
}
 ?>

代码错误调用未定义的函数bindParam(); 绑定(bind)错误 这里 $month 是用户动态选择的月份,$colname 也是用户选择的

<?php
$month=$_SESSION['month'];
$colname=$_SESSION['colname'];
$tot='present';
require('connect.php');
  global $pdo;
$stmt=$pdo->prepare("UPDATE `$month` SET `$colname`=:a WHERE roll =:foo");
 $stmt=bindparam(':a',$tot);
  foreach( $value as $value)
           {
      $stmt>bindParam(':foo',$value);
       $stmt->execute();
          }
    if($stmt==false)
   {    
     die("error".print_r($stmt->errorinfo()));
      } ?>

最佳答案

错误1

你的意思可能是:

$stmt->bindParam(':s',$colname);

错误2

此外,您尚未通过 session_start() 启动 session

错误3

应该是:

die("error".print_r($sql->errorinfo(), true));

错误4

您可能想要:

$stmt->bindParam(':foo',$value);

而不是:

$stmt>bindParam(':foo',$value);

顺便说一句,您更喜欢用单行编写代码?

关于php - pdo 查询更改表其中列名不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20835492/

相关文章:

php - exec、system 和 shell_exec PHP 函数中的命令字符串不能超过 8175 个字符

python - RaspberryPi-MySQLdb

php - 在 HTML 中嵌入 PHP - 无法在表中填充和显示数据

php - MySQL如何从数据库中获取最新日期的所有行

php - 我应该检查 pdo php 中执行操作的返回值吗

php - MySQL Select 与动态 where 子句不同

php - 如果值在 mysql 中则返回 true

javascript - 如何重定向页面

mysql - 使用内连接SQL语句时未定义索引

PHP PDO 和存储函数