I am currently creating my site and have come to a point where I am to add content to my database
my add.php code is this:
<?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');
}
?>
it loads ok and works file on the display but it does not add the info to my database called apps.
please can someone tell me why?
thank you.
actually your variables looks wrong.
try this
$image = $_POST['Image URL'];
$link = $_POST['Link'];
$price = $_POST['Price'];
EDIT. you should add value="something"
to your inputs
<input type="text" value="" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" value="" placeholder="Content" name="content"></textarea><br /><br />
EDIT:2
give a name to the submit button
<input type="submit" name="submit" value="Add Article" />
ECHO_ME-
This code works:
<?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');
}
?>
But it's when I go to add new fields that causes the problem. All the past little things that you have requested will make no difference to the current code I do not think.
Any more suggestions? Thanks.