gpt4 book ai didi

php - 无法将表单数据插入数据库

转载 作者:行者123 更新时间:2023-11-30 00:28:38 25 4
gpt4 key购买 nike

我需要将所有表单详细信息插入数据库并将上传的图像保存在某个位置。我成功将文件上传到指定位置。但表单详细信息未插入数据库。

这是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();

最佳答案

嗯,有很多事情需要考虑。

  • 您正在将 Javascript 与 PHP 混合。

    改变这个

        var $hostname = 'myhostname';

      $hostname = 'myhostname';

与其他人一样。

  • 根据您的情况,您不应该开设很多类(class)。

使用这个:

  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']);
  • 您应该切换到 PDOMYSQLI

编辑:

试试这个

    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();

编辑2:

要获取上传的文件,请使用 $_FILES 而不是 $_POST:

    $pg_img       = $_FILES['bg_img'];

也改变这个

  and $_FILES['bg_img']['name'] != ''

而不是

  && isset($_POST['bg_img'])

关于php - 无法将表单数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22681530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com