代码----
public function create(){
global $database;
//this code works perfectly to insert the new user into my database.
$sql = "INSERT INTO users (";
$sql .= "username, password, first_name, last_name";
$sql .= ") VALUES ('";
$sql .= $database->escape_value($this->username) ."', '";
$sql .= $database->escape_value($this->password) ."', '";
$sql .= $database->escape_value($this->first_name) ."', '";
$sql .= $database->escape_value($this->last_name) ."')";
if($database->query($sql)){
$this->id = $database->insert_id();
return true;
}else{
return false;
}
public function create()
{
global $database;
//this code is to be universal for other database types but it is not working.
$attributes = $this->attributes();
$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
if ($database->query($sql)) {
$this->id = $database->insert_id();
return true;
} else {
return false;
}
}
问题-假设是泛化脚本,以便它可以在任何数据库结构中使用。这在我脑子里不管用。
我认为这可能是魔术引用的问题。请使用addslash php函数array_values($attributes)
这不是您的编程错误,这里的问题是MySQL没有将此操作解释为有效,因为它的SQL_MODE处于严格模式。您可以选择编辑您的MySQL设置或将以下代码添加到您的数据库类:
private function open_connection() {
$this->connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME);
if (!$this->connection) {
# code...
die("Connection to database failed" . mysqli_errno($this->connection));
}else{
/// this is the part you should take note of i changed the sql mode here using this code
mysqli_query($this->connection, "SET GLOBAL sql_mode = ''");
}
您的问题可能是您正在尝试插入的值中使用join("', '", array_values($attributes))
。但我猜,你的列类型并不都是字符串,所以你正在创建这样的东西:
INSERT INTO mytable(id,column1)
VALUES('id','mycolumn1')
问题可能是你的id列的类型是int
,这可能是为什么你有Incorrect integer value
。所以你应该像这样写:
INSERT INTO mytable(id,column1)
VALUES(1,'mycolumn1')
我遇到了同样的问题。我想张贴回复,如果有人经历同样的问题。我想你是在看lynda.com的教程。正如DCoder所提到的,id是自动增量值,并且没有办法知道值是什么,此外,连接函数使每个属性值-列值字符串,在您的情况下,您得到空""
字符串。但id字段是整型。您的第一个create函数(没有join的那个)可以正常工作,因为省略了id字段。现在要修复$attributes = $this->attributes();
行之后的第二个create函数,添加以下代码if($attributes["id"]==""){
array_shift($attributes);
}
在$attributes = $this->attributes();
行之后你的attributes保存了一个属性列表;我还假设您的代码与我遇到的教程相同。检查id是否为空。如果为空,则执行array_shift操作。这将删除第一个属性,即id,并且在此过程中,新属性数组将没有id,然后在应用连接时,它将与第一个create函数相同。这将解决这个问题。但是我还是想知道老师是如何顺利通过教程的。很棒的讲师和很棒的面向对象教程——我说的是lynda.com教程。你可以进一步阐述你的问题,以得到更多的回应或正确的答案。因为我运行同样的问题,这对我来说更容易。
要让MySql为AUTO_INCREMENT字段生成序列号,您有一些选项:
显式分配NULL;显式赋值0
public function create() {美元的全球数据库;//如果你的auto_increment not null文件需要清理,使用'0'。$this->id = 0;
$attributes = $this->sanitized_attributes();
$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
if($database->query($sql)) {
$this->id = $database->insert_id();
return true;
} else {
return false;
}
}
这就是我想解决这个问题的方法
public function create(){
global $database;
$attributes = $this->sanitized_attributes();
$keys = array_keys($attributes);
$values = array_values($attributes);
$id = array_shift($values);
$id = (int) $id;
$sql = "INSERT INTO ".static::$table_name." (";
$sql .= join(", ", $keys);
$sql .= ") VALUES ($id,'";
$sql .= join("', '", $values);
$sql .= "')";
if($database->query($sql)) {
$this->id = $database->insert_id();
return true;
} else {
return false;
}
}