【发布时间】:2014-06-10 01:40:09
【问题描述】:
这应该很容易...但我想不通。如果下面的执行语句在插入查询的末尾起作用.....
$query->execute(array($this->visible_password,
$this->hashed_password,
$this->temp_hashed_password,
$this->first_name,
$this->last_name,
$this->position,
$this->location,
$this->city,
$this->email,
$this->country,
$this->institution,
$this->interests,
$this->profile_comment));
和
$result = join(", ", array_values($place_holder));
echo $result;
给予
$this->visible_password, $this->hashed_password, $this->temp_hashed_password, $this->email, $this->first_name, $this->last_name, $this->position, $this->location , $this->city, $this->country, $this->institution, $this->interests, $this->profile_comment
那为什么这不起作用.....
$query->execute(array($result))
占位符上的 var_dump 给出...
array(13) {
[0]=> string(26) "$this->visible_password"
[1]=> string(25) "$this->hashed_password"
[2]=> string(30) "$this->temp_hashed_password"
[3]=> string(15) "$this->email"
[4]=> string(20) "$this->first_name"
[5]=> string(19) "$this->last_name"
[6]=> string(18) "$this->position"
[7]=> string(18) "$this->location"
[8]=> string(14) "$this->city"
[9]=> string(17) "$this->country"
[10]=> string(21) "$this->institution"
[11]=> string(19) "$this->interests"
[12]=> string(25) "$this->profile_comment" }
$placeholder 是一个数组...接受类的属性($attributes)
$place_holder = array();
foreach ($attributes as $key => $value) {
$place_holder[] = "$this->" . $key;
}
我一直在尝试在我的用户类抽象中创建用户方法,以便我可以在我的所有类中使用它。我一直在将我的网站从 mysqli(噩梦)转换为 PDO。这里是方法....
public function pdo_create_test() {
$attributes = $this->attributes();
$att = array_keys($attributes);
$question_marks = array();
foreach ($attributes as $key => $value) {
$question_marks[] = "?";
}
$place_holder = array();
foreach ($attributes as $key => $value) {
$place_holder[] = "$this->" . $key;
}
$result = join(", ", array_values($place_holder));
$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES (";
$sql .= join(", ", array_values($question_marks));
$sql .= ")";
$query = $handler->prepare($sql);
$query->execute(array($this->visible_password,
$this->hashed_password,
$this->temp_hashed_password,
$this->first_name,
$this->last_name,
$this->position,
$this->location,
$this->city,
$this->email,
$this->country,
$this->institution,
$this->interests,
$this->profile_comment));
}
这是一个更新......(我已经改变了占位符以获得比尔所说的值) 我还呼应了 sql 和占位符的结果,以查看其内容。
public function pdo_create_test() {
global $handler;
$attributes = $this->attributes();
$att = array_keys($attributes);
$question_marks = array();
foreach ($attributes as $key => $value) {
$question_marks[] = "?";
}
$place_holder = array();
foreach ($attributes as $key => $value) {
$place_holder[] = $this->$key;
}
$result = join(", ", array_values($place_holder));
$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES (";
$sql .= join(", ", array_values($question_marks));
$sql .= ")";
echo $sql;
echo "<br/>";
echo "<br/>";
echo $result;
$query = $handler->prepare($sql);
$query->execute(array($result));
}
为
$user = new User;
$user->visible_password = "Sam";
$user->hashed_password = "Walsh";
$user->pdo_create_test();
输出是
插入用户(visible_password、hashed_password、temp_hashed_password、email、first_name、last_name、位置、位置、城市、国家、机构、兴趣、profile_comment)值(?、?、?、?、?、?、?、? , ?, ?, ?, ?, ?)
山姆,沃尔什,,,,,,,,,,,,,,,,
但没有进入数据库....不明白为什么...其他数据库字段设置为 NULL 所以这不是问题....
【问题讨论】:
-
$place_holder包含什么?发布var_dump($place_holder);的输出。 -
您的
$place_holder值是字符串而不是变量,因此您的join()也返回一个字符串,而不是变量。你如何定义$place_holder?