是的,我正在努力做到这一点,以便我可以更新博客文章,以便可以编辑特定的文章。类似的准备好的语句有效,但另一个无效。这是为准备好的语句设置数组的 set 方法:
public function set($key, $value){
$this->data[$key] = $value;
}
现在这里是无法工作的代码,如果帖子不会更新。
public function edit($pID){
$this->postObject = new Post();
$post = $this->postObject->getPost($pID);
$this->set('pID', $post['pID']);
$this->set('title', $post['title']);
$this->set('content', $post['content']);
$this->set('categoryID',$post['categoryID']);
$this->set('date', $post['date']);
$this->set('task', 'update');
//Im totally lost on how the task update thing works on addpost. Just going to call the update post class method.
$post = new Post();
$post->update($this->data);
}
public function update($data){
$sql='UPDATE posts SET title=?,content=?,categoryID=?,date=?
WHERE pID=?';
$this->db->execute($sql,$data);
$message = 'Post updated';
return $message;
}
现在这里是一个有效的类似语句的代码。
public function add(){
$this->postObject = new Post();
$data = array('title'=>$_POST['post_title'],'content'=>$_POST['post_content'],
'categoryID'=>$_POST['categoryID'],'date'=>$_POST['date']);
$result = $this->postObject->addPost($data);
$this->set('message', $result);
}
public function addPost($data){
$sql='INSERT INTO posts (title,content,categoryID,date) VALUES (?,?,?,?)';
$this->db->execute($sql,$data);
$message = 'Post added.';
return $message;
}
我试过计算进来的数组并且数组有行。我尝试使用 mysqli 函数进行调试,但事实上这是在预制框架上完成的,这使得 mysqli_functions 的使用更加复杂。为什么数组不像插入语句那样进入更新语句?
编辑关于 db->execute,那部分不是我的代码。我认为这可能是它的代码。
function Execute($sql,$inputarr=false)
{
if ($this->fnExecute) {
$fn = $this->fnExecute;
$ret = $fn($this,$sql,$inputarr);
if (isset($ret)) return $ret;
}
if ($inputarr) {
if (!is_array($inputarr)) $inputarr = array($inputarr);
$element0 = reset($inputarr);
# is_object check because oci8 descriptors can be passed in
$array_2d = $this->bulkBind && is_array($element0) && !is_object(reset($element0));
//remove extra memory copy of input -mikefedyk
unset($element0);
if (!is_array($sql) && !$this->_bindInputArray) {
$sqlarr = explode('?',$sql);
$nparams = sizeof($sqlarr)-1;
if (!$array_2d) $inputarr = array($inputarr);
foreach($inputarr as $arr) {
$sql = ''; $i = 0;
//Use each() instead of foreach to reduce memory usage -mikefedyk
while(list(, $v) = each($arr)) {
$sql .= $sqlarr[$i];
// from Ron Baldwin <ron.baldwin#sourceprose.com>
// Only quote string types
$typ = gettype($v);
if ($typ == 'string')
//New memory copy of input created here -mikefedyk
$sql .= $this->qstr($v);
else if ($typ == 'double')
$sql .= str_replace(',','.',$v); // locales fix so 1.1 does not get converted to 1,1
else if ($typ == 'boolean')
$sql .= $v ? $this->true : $this->false;
else if ($typ == 'object') {
if (method_exists($v, '__toString')) $sql .= $this->qstr($v->__toString());
else $sql .= $this->qstr((string) $v);
} else if ($v === null)
$sql .= 'NULL';
else
$sql .= $v;
$i += 1;
if ($i == $nparams) break;
} // while
if (isset($sqlarr[$i])) {
$sql .= $sqlarr[$i];
if ($i+1 != sizeof($sqlarr)) $this->outp_throw( "Input Array does not match ?: ".htmlspecialchars($sql),'Execute');
} else if ($i != sizeof($sqlarr))
$this->outp_throw( "Input array does not match ?: ".htmlspecialchars($sql),'Execute');
$ret = $this->_Execute($sql);
if (!$ret) return $ret;
}
} else {
if ($array_2d) {
if (is_string($sql))
$stmt = $this->Prepare($sql);
else
$stmt = $sql;
foreach($inputarr as $arr) {
$ret = $this->_Execute($stmt,$arr);
if (!$ret) return $ret;
}
} else {
$ret = $this->_Execute($sql,$inputarr);
}
}
} else {
$ret = $this->_Execute($sql,false);
}
return $ret;
}