我需要将所有表单详细信息插入到数据库中,并将上传的图像保存在某个位置。我成功将文件上传到指定位置。但是表单细节没有插入到数据库中。
这是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();
答案 0 :(得分:0)
尝试检查您是否已成功连接到数据库,如果没有,请将初始化从$db_con = new db_con;
更改为$db_con = new db_con();
EDITED: 我认为你还应该改变你的流程。分离表单,插入过程和数据库连接。我建议你创建3个单独的文件: 1.表单页面(HTML) 2.流程页面(PHP文件,您将链接表单。或者流程将在何处发生) 3.数据库连接文件(PHP文件。将数据库连接置于此处以及其他与数据库相关的功能)。
这是我的例子:
表单页面:
<!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="process.php" 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>
正如您所见,我将表单的操作指向process.php。
这是我的示例process.php:
<?php
include 'classes/db.php';
if(isset($_POST['submit'])){
// get your post data here
$post_data = array('your post data');
// instantiate db
$db = new Db();
// create db connection
$db_connect = $db->connect();
// check if successfully connected to db
// then call the insertion process
$db->insert_data('table_name', $post_data);
}
然后在你的db.php文件中:
<?php
class Db {
function connect(){
$hostname = 'localhost';
$username = 'root';
$pwd = 'admin';
$dbname = 'yourdbname';
mysql_connect($hostname, $username, $pswd) or die('couldn\'t connect');
mysql_select_db($dbname) or die('couldn\'t select db');
}
function insert_data($table_name, $data) {
mysql_query("INSERT INTO ".$table_name." (page_name, page_title, page_bg_img, page_content) VALUES ($data);
}
}
?>
只是不要按照我的插入过程因为我做的很简短。它只是我想要展示的流程。
希望这有帮助!
答案 1 :(得分:0)
您正在将Javascript与PHP混合。
更改此
var $hostname = 'myhostname';
到
$hostname = 'myhostname';
与其他人一样。
使用它:
class db_con{
public function dbcon(){
.........
}
function insert_data() {
...........
}
}
insert_data()
函数这样称呼:
$insert = new insert;
$insert->insert_data();
你应该逃避你的变量,如:
$pg_title = mysql_real_escape_string($_POST['page_title']);
您应切换为PDO
或MYSQLI
。
编辑:
试试这个
session_start();
if(!empty($_SESSION['uname']) && !empty($_SESSION['pswd'])){
include 'classes/insert.php';
$db_con = new db_con();
$db_con->dbcon();
$db_con->insert_data();
EDIT2:
要获取上传的文件,请使用$_FILES
代替$_POST
:
$pg_img = $_FILES['bg_img'];
也改变这个
and $_FILES['bg_img']['name'] != ''
而不是
&& isset($_POST['bg_img'])