我的代码有一点时间,我无法找到变量dbentry到数据库的任何想法。
受保护的函数moveFile($ file) {
$filename = isset($this->newName) ? $this->newName : $file['name'];
$success = move_uploaded_file($file['tmp_name'], $this->destination . $filename);
if ($success) {
$result = $file['name'] . ' was uploaded successfully';
if (!is_null($this->newName)) {
$result .= ', and was renamed ' . $this->newName;
$dbentry = $this->newName;
echo "$dbentry";
$mysqli = new mysqli("localhost", "DBNAME", "PASSWORD", "USERNAME");
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
// attempt insert query execution
$sql = "INSERT INTO files (file_name) VALUES ('$dbentry')";
if($mysqli->query($sql) === true){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not execute " . $mysqli->error;
}
// Close connection
$mysqli->close();
//
}
$result .= '.';
$this->messages[] = $result;
} else {
$this->messages[] = 'Could not upload ' . $file['name'];
}
}
答案 0 :(得分:0)
根据documentation创建了连接:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
你确定你的参数顺序正确吗?
然后检查连接状态的官方方法是:
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
答案 1 :(得分:0)
这是新代码,但仍然得到的结果只是回显它出现的文件名,它只是在数据库条目部分停止。
protected function moveFile($file)
{
$filename = isset($this->newName) ? $this->newName : $file['name'];
$success = move_uploaded_file($file['tmp_name'], $this->destination . $filename);
if ($success) {
$result = $file['name'] . ' was uploaded successfully';
$dbentry = $this->newName;
echo "$dbentry";
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database name";
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
// attempt insert query execution
$sql = "INSERT INTO files (file_name) VALUES ('$dbentry')";
if($mysqli->query($sql) === true){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not execute " . $mysqli->error;
}
// Close connection
$mysqli->close();
//
if (!is_null($this->newName)) {
$result .= ', and was renamed ' . $this->newName;
}
$result .= '.';
$this->messages[] = $result;
} else {
$this->messages[] = 'Could not upload ' . $file['name'];
}
}