我目前正在创建我的网站,并且已经到了我要向我的数据库添加内容的地步
我的add.php代码是这样的:
<?php
session_start();
include_once('../include/connection.php');
if (isset($_SESSION['logged_in'])){
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
$image = $_POST['Image URL'];
$link = $_POST['Link'];
$price = $_POST['Price'];
if (empty($title) or empty($content)) {
$error = 'All Fields Are Required!';
}else{
$query = $pdo->prepare('INSERT INTO apps (app_title, app_content, app_img, app_link, app_price) VALUES(?, ?, ?, ?, ?)');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, $image);
$query->bindValue(4, $link);
$query->bindValue(5, $price);
$query->execute();
header('location: index.php');
}
}
?>
<html>
<head>
<title>testing</title>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>
<br />
<h4>Add Article</h4>
<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>
<form action="add.php" method="post" autocomplete="off">
<input type="text" value="" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" value="" placeholder="Content" name="content"></textarea><br /><br />
<input type="text" value="" name="Image URL" placeholder="Image URL" /><br /><br />
<input type="text" value="" name="Link" placeholder="Link" /><br /><br />
<input type="text" value="" name="Price" placeholder="Price" /><br /><br />
<input type="submit" name="submit" value="Add Article" />
</form>
</div>
</body>
</html>
<?php
}else{
header('location: index.php');
}
?>
它加载确定并在显示器上工作文件,但它不会将信息添加到我的数据库名为apps。
请有人能告诉我为什么吗?
谢谢你。答案 0 :(得分:1)
实际上你的变量看起来不对。
试试这个
$image = $_POST['Image URL'];
$link = $_POST['Link'];
$price = $_POST['Price'];
EDIT。您应该在输入中添加value="something"
<input type="text" value="" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" value="" placeholder="Content" name="content"></textarea><br /><br />
编辑:2
为提交按钮命名
<input type="submit" name="submit" value="Add Article" />
答案 1 :(得分:0)
ECHO_ME -
此代码有效:
<?php
session_start();
include_once('../include/connection.php');
if (isset($_SESSION['logged_in'])){
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All Fields Are Required!';
}else{
$query = $pdo->prepare('INSERT INTO apps (app_title, app_content, app_timestamp) VALUES(?, ?, ?)');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, time());
$query->execute();
header('location: index.php');
}
}
?>
<html>
<head>
<title>testing</title>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>
<br />
<h4>Add Article</h4>
<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>
<form action="add.php" method="post" autocomplete="off">
<input type="text" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
<input type="submit" value="Add Article" />
</form>
</div>
</body>
</html>
<?php
}else{
header('location: index.php');
}
?>
但是当我去添加导致问题的新字段时。你所要求的所有过去的小事都不会影响我不认为的当前代码。
还有其他建议吗?谢谢。