我需要將所有表單詳細信息插入數據庫並將上傳的圖像保存在某個位置。我成功地將文件上傳到指定的位置。但表格細節沒有被插入到數據庫中。無法將表單數據插入到數據庫中
這是pages.php
<?php
session_start();
if(!empty($_SESSION['uname']) && !empty($_SESSION['pswd'])) {
include 'classes/insert.php';
$db_con->dbcon();
$db_con->insert_data();
$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['bg_img']['name']);
if(move_uploaded_file($_FILES['bg_img']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['bg_img']['name']).
" has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>New Page</title>
</head>
<body>
<p>Add pages</p>
<a href="logout.php">logout</a>
<br /><br />
<a href="dashboard.php">Dashboard</a>
<br /><br />
<form method="post" action="" enctype="multipart/form-data">
<label>Page name</label>
<input type="text" name="page_name" />
<br />
<br />
<label>Page title</label>
<input type="text" name="page_title" />
<br />
<br />
<label>Page bg img</label>
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="bg_img" />
<br />
<br />
<label>Page content</label>
<textarea name="page_content"></textarea>
<br />
<br />
<input type="submit" value="Submit" name="submit" />
</form>
</body>
</html>
<?php } else {
$home_loc = 'index.php';
header ('Location:' .$home_loc);}
?>
代碼這是insert.php
class db_con {
public function dbcon() {
$hostname = 'localhost';
$username = 'root';
$pswd = 'admin';
$dbname = 'web';
mysql_connect($hostname, $username, $pswd) or die('cudn\'t connect');
mysql_select_db($dbname) or die('cudn\'t select db');
}
function insert_data() {
$this->dbcon();
if (isset($_POST['page_name']) && isset($_POST['page_title']) && isset($_POST['bg_img']) && isset($_POST['page_content'])) {
$pg_name = mysql_real_escape_string($_POST['page_name']);
$pg_title = mysql_real_escape_string($_POST['page_title']);
$pg_img = mysql_real_escape_string($_POST['bg_img']);
$pg_content = mysql_real_escape_string($_POST['page_content']);
$insert_query = mysql_query("INSERT INTO pages (page_name, page_title, page_bg_img, page_content) VALUES ('" . $pg_name . "', '" . $pg_title . "', '" . $pg_img . "', '" . $pg_content . "')");
}
}
}
$db_con = new db_con();
不應該您對您的類instanciation括號? '$ insert = new insert();' – Fluffeh
產生的錯誤是什麼? – 2014-03-27 08:10:41
我給實例添加了括號,但它沒有做任何改變。沒有錯誤顯示。但是,如果我刪除'enctype =「multipart/form-data」'數據正在插入。 – Joker