【问题标题】:inserting data from php to database mysql table将数据从php插入数据库mysql表
【发布时间】:2015-10-22 22:54:43
【问题描述】:

我在将数据从 php 插入数据库时​​遇到问题。

文件是:Patient.php

表名是:patient_tbl

表格里面是:头像education

这里是插入代码:下面这段代码没有问题,'education'的值会被插入到表中

$sql = "INSERT INTO " . $this->_table;
$sql .= "education,";
$params = array(
  urlencode($patient->getEducation()),
);
return $this->exec($sql, $params);
}

但现在这是我的问题,“头像”不要插入到表格中

错误消息是:“无法准备查询。”

$sql = "INSERT INTO " . $this->_table;
$sql .= "education, avatar, ";
$params = array(
  urlencode($patient->getEducation()),
  urlencode($patient->getAvatar()),
);
return $this->exec($sql, $params);
}

我的问题是什么?我找不到解决办法。

非常感谢您的帮助。

谢谢

【问题讨论】:

  • 好吧,对于初学者来说,第二个查询中的尾随 , 会导致语法错误。
  • 打印出$sql 并将其包含在问题中。我的猜测是答案会很明显(比如列名周围没有括号)。
  • 好吧,这是错误消息:无法准备查询。
  • 你为什么使用 urlencode?​​span>
  • 您应该接受(一个)答案,而不是在标题中添加“[SOLVED]”。

标签: php mysql sql-insert


【解决方案1】:

基本语法sql查询'INSERT'是

INSERT [INTO] tbl_name [(col_name,...)]
        VALUES (expression,...),(...)

如果你使用一些参数,你需要添加括号:

$sql = "INSERT INTO " . $this->_table;
$sql .= "(education, avatar) VALUES (?, ?)";
$params = array(
  urlencode($patient->getEducation()),
  urlencode($patient->getAvatar()),
);
return $this->exec($sql, $params);
}

然后写,$this->exec 中的内容是什么?

【讨论】:

  • 是的,你是对的,这是一个小问号,只是这个((?))你是对的。非常感谢:)
猜你喜欢
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多