我面臨兩個問題,一個是我無法使用PDO將數據上傳到數據庫,即使代碼是正確的(據我所知)另一個是我收到幾個像這樣的警告,當頁面重新加載時,警告說警告:session_start():無法發送會話緩存限制器 - 頭已發送(輸出開始於C:\ xampp \ htdocs \ Ecommerce \ header。在第3行的C:\ xampp \ htdocs \ Ecommerce \ Admin Panel.php中,當我點擊註銷按鈕時,警告說警告:無法修改標題信息 - 已經發送的標題(輸出從C開始:\ xampp \ htdocs \ Ecommerce \ header.php:111)在第37行的C:\ xampp \ htdocs \ Ecommerce \ Admin Panel.php中。以下是我的代碼,如果我做錯了任何事,請幫助我。使用PDO和標題警告將數據上傳到數據庫的問題
這是我的header.php代碼。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ECOM Site</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link href="Styling.css" rel="stylesheet">
<link href="Sliders.css" rel="stylesheet">
<script type="application/x-javascript">
function showDiv() {
document.getElementById('sign-in').style.display = "block";
}
</script>
</head>
<body>
<div class="header1">
<div class="container">
<div class="header-top">
<div class="row">
<div class="col-sm-4">
<form class="navbar-form navbar-left">
<div class="input-group">
<input type="text" class="form-control search" placeholder="Search">
<span class="input-group-btn">
<!-- <button class="btn btn-default submit" type="button" ><i class="glyphicon glyphicon-search" style = "color : #e98fa0;"></i></button>-->
<a class="btn btn-default submit" role="button" ><i class="glyphicon glyphicon-search" style = "color : #e98fa0;"></i></a>
</span>
</div>
</form>
</div>
<div class="col-sm-4 logo text-center ">
<a href="index.php">LOGO</a>
</div>
<div class="col-sm-4 text-center">
<div class="login-bars">
<a class="btn btn-default log-bar" href="#" role="button" >Sign up</a>
<button type="submit" class="btn btn-default log-button" onclick = "showDiv()">Login</button>
<a class="btn btn-default log-bar" href="Productpage.php" role="button">Cart</a>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="header-botom">
<div class="content white">
<nav class="navbar navbar-default nav-menu" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">LOGO</a>
</div>
<div class="clearfix"></div>
<!--/.navbar-header-->
<div class="collapse navbar-collapse collapse-pdng " id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav nav-font">
<li><a href="#">Home</a></li>
<li><a href="#">Ear Rings</a></li>
<li><a href="#">Jewelry </a></li>
<li><a href="#">Accessories</a></li>
</ul>
<div class="clearfix"></div>
</div>
<!--/.navbar-collapse-->
<div class="clearfix"></div>
</nav>
<div class="clearfix"></div>
</div>
</div>
</div>
<div id ="sign-in" class=" container-fluid login " style="display: none;">
<div class="container">
<h3> Login </h3>
<br>
<form>
<div class="row">
<div class="col-md-3">
<div class="form-group ">
<!--<label for="username" class="head">Email or Phone</label>-->
<input type="text" class="form-control" id="username" Placeholder="email or Phone">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<!--<label for="password" class="head">Password</label>-->
<input type="password" class="form-control" id="Password"placeholder = "Password">
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
<a href="#" >Forgot Password</a>
</div>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-default pull-left">Submit</button>
<br>
</div>
</div>
</form>
</div>
</div>
<br>
</div>
這是我的聯繫Panel.php代碼
<?php
include('header.php');
session_start();
require ('includes/connect.php');
if (isset($_SESSION['logged_in'])) {
require ('includes/Product.php');
$product = new Product;
if(isset($_POST['productname'] , $_POST['oldprice'] , $_POST['newprice'] , $_POST['briefdescription'] , $_POST['productdescription'] , $_POST['productspecifications'])){
$productname = $_POST['productname'];
$oldprice = $_POST['oldprice'];
$newprice = $_POST['newprice'];
$briefdescription = $_POST['briefdescription'];
$productdescription = $_POST['productdescription'];
$productspecifications = $_POST['productspecifications'];
if(empty($productname) or empty ($oldprice) or empty ($newprice) or empty ($briefdescription) or empty ($productdescription) or empty ($productspecifications)){
$error = "All fields are required";
}
else{
$query = $pdo -> prepare("INSERT INTO products (product_name ,old_price , new_price , brief_description , product_description , product_specifications) VALUES (?,?,?,?,?,?)");
$query -> bindValue(1, $productname);
$query -> bindValue(2, $oldprice);
$query -> bindValue(3, $newprice);
$query -> bindValue(4, $briefdescription);
$query -> bindValue(5, $productdescription);
$query -> bindValue(6, $productspecifications);
$query ->execute();
header('location:index.php');
}
}
//Logout
function logout(){
session_destroy();
header('location:Admin Panel.php');
}
if(isset($_POST['logout'])) {
logout();
}
?>
<div class="container">
<div class="col-sm-6">
<form action = "Admin Panel.php" method="POST">
<button type="submit" class="btn btn-primary " name = "logout" >Logout</button>
</form>
<?php if(isset($error)){ ?>
<small style = "color : #aa0000"; ><?php echo $error ?></small>
<br><br>
<?php } ?>
<form>
<div class="form-group ">
<label for="productname" class="upload">Product name</label>
<input type="text" class="form-control" name="productname" id="productname">
</div>
<div class="form-group ">
<label for="oldprice" class="upload">Old price</label>
<input type="text" class="form-control" name="oldprice" id="oldprice">
</div>
<div class="form-group ">
<label for="newprice" class="upload">New price</label>
<input type="text" class="form-control" name="newprice" id="newprice">
</div>
<div class="form-group ">
<label for="briefdescription" class="upload">Brief description</label>
<textarea class="form-control" id="briefdescription" name=" briefdescription" rows="7"></textarea>
</div>
<div class="form-group ">
<label for="productdescription" class="upload">Description</label>
<textarea class="form-control" id="productdescription" name="productdescription" rows="7"></textarea>
</div>
<div class="form-group ">
<label for="productspecifications" class="upload">Specifications</label>
<textarea class="form-control" id="productspecifications" name="productspecifications" rows="7"></textarea>
</div>
<button type="submit" class="btn btn-default " >Submit</button>
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
<?php
}
else {
include ('includes/connect.php');
include ('includes/product.php');
if(isset($_POST['username'] , $_POST['password']))
{
$username = $_POST['username'];
$password = md5($_POST['password']);
if(empty($username) or empty($password))
{
$error = "Please fill all the fields";
}
else
{
$query = $pdo->prepare("SELECT * FROM product_login WHERE username = ? AND password = ? ");
$query->bindValue (1, $username);
$query->bindValue (2, $password);
$query->execute();
$num=$query->rowCount();
if($num==1) {
$_SESSION['logged_in']= true;
header('location:Admin Panel.php');
exit();
}
else{
$error = "Please enter correct Username and Password";
}
}
}
?>
<div class="container">
<div class="row">
<div class="col-md-8">
<h3>Admin Login</h3>
<br>
<?php if(isset($error)){ ?>
<small style = "color : #aa0000"; ><?php echo $error ?></small>
<br><br>
<?php } ?>
<form action ="Admin Panel.php" method ="POST" class = "form-inline">
<div class="form-group">
<input class="form-control" name="username" type="text" id="Username" placeholder ="Username"/>
</div>
<div class="form-group text-left">
<input class="form-control" name = "password" type="password" id="password" Placeholder ="Password"/>
</div>
<button type="submit" class="btn btn-primary " >Submit</button>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
<?php
}
?>
這是我Product.php包括文件。
<?php
class Product {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM products");
$query -> execute();
return $query->fetchAll();
}
}
?>
這是我的數據庫結構Database Structure
三江源
可能的[如何解決]重複的頭文件已發送「錯誤在PHP中](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – axiac
*「我面臨兩個問題」* - 發佈兩個不同的問題,並嘗試爲每個問題提供[mcve]。 – axiac