用 CodeIgniter 插入语句——太困惑了
我用 CodeIgniter 做得很好。我可以毫无问题地在我的 MySQL 数据库上执行 SELECT 语句。但是,现在我正在尝试执行 INSERT 语句。
请注意,我还没有尝试过 UPDATE 语句。
读完文档后,我很困惑。
这就是我所拥有的:
contacts.php:
function add() {
//echo "<pre>";print_r($_POST);
$this->load->model('Contacts_model');
$this->Contacts_model->insertContact($_POST);
}
contacts_model.php:
function insertContact($_POST) {
//echo "<pre>";print_r($_POST);
$title = $_POST['title']; // I can echo this here. It works
$f_name = $_POST['f_name']; // I can echo this here. It works
$sql = "INSERT INTO contacts (title,f_name) " .
"VALUES (" .
$this->db->escape($title) .
"," .
$this->db->escape($f_name) .
")";
$this->$db->query($sql);
}
我读过有关Active Record的内容,但如果这就是让我困惑的原因,那么我仍然没有意识到我做错了什么。所有的例子看起来都和我的一模一样。
帮助?
编辑
$sql = "INSERT INTO contacts (title,f_name) VALUES ('$this->db->escape($title)','$this->db->escape($f_name)'";
$this->$db->query($sql);
我也尝试过这样的。还有许多其他变体。这似乎不是我的语法...我想。
I'm doing well with CodeIgniter. I can do SELECT statements on my MySQL database with no problems at all. But, now I'm trying to do an INSERT statement.
Note that I have not tried an UPDATE statement yet.
After reading the docs, I'm so confused.
This is what I have:
contacts.php:
function add() {
//echo "<pre>";print_r($_POST);
$this->load->model('Contacts_model');
$this->Contacts_model->insertContact($_POST);
}
contacts_model.php:
function insertContact($_POST) {
//echo "<pre>";print_r($_POST);
$title = $_POST['title']; // I can echo this here. It works
$f_name = $_POST['f_name']; // I can echo this here. It works
$sql = "INSERT INTO contacts (title,f_name) " .
"VALUES (" .
$this->db->escape($title) .
"," .
$this->db->escape($f_name) .
")";
$this->$db->query($sql);
}
I've read about Active Record, but if that's what is messing me up, then I still don't realize what I'm doing wrong. All of the examples look exactly like mine.
Help?
EDIT
$sql = "INSERT INTO contacts (title,f_name) VALUES ('$this->db->escape($title)','$this->db->escape($f_name)'";
$this->$db->query($sql);
I've also tried it like this. And many other variants. It doesn't seem to be my syntax... I think.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的查询很好,查询没有被执行的唯一原因是您正在使用这个:
没有像 $db 这样的东西,只需使用这个:
我确信这是问题所在,但如果不是,请发帖它给出的错误。谢谢。
希望这有帮助。
Your query is fine, only reason that why query is not being executed is that you are using this:
there is nothing like $db, just use this:
I'm sure this is the problem, but if it is not then please kindly post the error what it is giving. Thanks.
Hope this helps.
您错过了引号字符:
顺便说一句,
$_POST
变量到底是怎么回事?它是超级全局变量之一。您不必在参数中传输它。您始终可以在脚本中的任何位置安全地调用它。另请注意,由于您使用 CodeIgniter,因此最好查看
Input
类库 并用它来满足您的所有输入需求。You missed the quote character:
BTW, What the hell with the
$_POST
variable? It's one of SuperGlobal variable. You don't have to transfer it in parameter. You can always safely call it anywhere in your script.Another note, since you use CodeIgniter, you better check out the
Input
class library and use it for all your input need.