PHP - Unidentified Index Variables [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
Notice: Undefined index: categoryID in C:\xampp\htdocs\mit\postCategory.php on line 14
I'm having this error in my PHP code below where I'm only getting the categoryID via session and storing it in a index/variable which is $categoryID and then I called this variable on line 20 where I have my $query. Did I miss something? Thank you for your help!
<?php
session_start();
include "dbconnect.php";
if (!isset($_SESSION['admin'])) {
header("Location:index.php");
}
if (isset($_GET['categoryID'])) {
$_SESSION['postCategory']['categoryID'] = $_GET['categoryID'];
}
if (isset($_POST['update'])) {
$categoryID = $_GET['categoryID'];
$postTitle = $_POST['postTitle'];
$postDesc = $_POST['postDesc'];
$postImage = $_FILES['image']['name'];
$target = "postImage/".basename($postImage);
$query = "UPDATE background SET postTitle = '$postTitle', postDesc = '$postDesc', postImage = '$postImage' WHERE post.categoryID ='$categoryID'";
$result = mysqli_query($con, $query);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
} else {
$msg = "Failed to upload image";
}
}
?>
<form method="post" action="postCategory.php" enctype="multipart/form-data">
<input type="text" name="postTitle" placeholder="Your Post Title">
<br>
<textarea name="postDesc" id="myTextarea">Your Post Description</textarea>
<br>
<input type="file" name="image">
<br>
<button type="submit" class="btn btn-primary" name="update">Post</button>
</form>

You need to include the $_GET variable in your form action:
<form method="post" action="postCategory.php?categoryID=1" enctype="multipart/form-data">
Or add it as a hidden text field and reference it as a $_POST variable.

Related

PHP - PDO app error [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I'm trying to extend my PDO knowledge and at the moment I'm working at php app. Actually is a simple CMS, and I got a problem on the admin page, when you try to update the existing pages I got an error about ID [$_POST].
The error looks like:
Notice: Undefined index: id in /Applications/MAMP/htdocs/cms/admin/edit.php on line 6 object(PDOStatement)#2 (1) { ["queryString"]=> string(138) " UPDATE pages SET label = :label, title = :title, slug = :slug, body = :body, updated = NOW(), WHERE id = :id " } Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/cms/admin/edit.php:6) in /Applications/MAMP/htdocs/cms/admin/edit.php on line 28
EDIT.php
<?php
require '../app/start.php';
if (!empty($_POST)) {
$id = $_POST['id'];
$label = $_POST['label'];
$title = $_POST['title'];
$slug = $_POST['slug'];
$body = $_POST['body'];
$updatePage = $db->prepare('
UPDATE pages
SET label = :label, title = :title, slug = :slug, body = :body, updated = NOW(),
WHERE id = :id
');
$updatePage->execute([
'id' => $id,
'label' => $label,
'title' => $title,
'slug' => $slug,
'body' => $body,
]);
var_dump($updatePage);
header('Location: ' . BASE_URL . '/admin/list.php');
exit();
}
if(!isset($_GET['id'])){
header('Location:' . BASE_URL . '/admin/list.php');
exit();
}
$page = $db->prepare('
SELECT *
FROM pages
WHERE id = :id
');
$page->execute(['id' => $_GET['id']]);
$page = $page->fetch(PDO::FETCH_ASSOC);
require VIEW_ROOT . '/admin/edit.php';
START.php
<?php
require ('functions.php');
ini_set('display_errors', 1);
define('APP_ROOT', __DIR__);
define('VIEW_ROOT', APP_ROOT . '/views');
define('BASE_URL', 'http://localhost/cms');
$db = new PDO('mysql:host=localhost;dbname=cms', 'root', 'root');
?>
EDIT.PHP (form page)
<?php require VIEW_ROOT . '/templates/header.php'; ?>
<h2>Add page</h2>
<form action="<?php echo BASE_URL; ?>/admin/edit.php" method="POST" autocomplete="off">
<label for="title">
Title
<input type="text" name="title" id="title" value="<?php echo e($page['title']); ?>">
</label>
<label for="label">
Label
<input type="text" name="label" id="label" value="<?php echo e($page['label']); ?>">
</label>
<label for="slug">
Slug
<input type="text" name="slug" id="slug" value="<?php echo e($page['slug']); ?>">
</label>
<label for="body">
Content
<textarea name="body" id="body" cols="30" rows="10"><?php echo e($page['body']); ?></textarea>
</label>
<input type="hidden" value="<?php echo e($page['id']);?>">
<input type="submit" value="Edit article">
</form>
check following code:
<input type="hidden" value="<?php echo e($page['id']);?>">
as you see this code does not have name , according to your samples it should have name="id"

Undefined index: firstname in C:\xampp\htdocs\form_require1.php on line 53/55 [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
Hello everyone!
can someone guide me for the following problem? I was trying to make a php form, but it shows the error ( Undefined index: firstname in C:\xampp\htdocs\form_require1.php on line 53/55) when I run it. please help me
enter code here <?php // define valiables and set to empty values $firstnameErr = $lastnameErr = ""; $firstname = $lastname = ""; if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($_POST["firstname"])) {
$firstnameErr = "Name is required";
} else {
$firstname = test_input($_POST["firstname"]); }
if (empty($_POST["lastname"])){
$lastnameErr = "Name is require"; } else { $lastname = test_input($_POST["lastname"]); } } function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
return $data; } ?> `enter code here` <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]) ?>" method="post"> Your firstname <input type="text" name="firstname" /> <span class="error">* <?php echo $firstnameErr;?></span> <br><br> Your Lastname <input type="text" name="lastname" /> <span class="error">* <?php echo $lastnameErr;?></span> <br><br> <input type="submit" value="Submit" /> </form> <?php // } else {
echo 'Your Details';
echo "<br>";
echo 'Fistname: ' . $_POST["firstname"];
echo "<br>";
echo 'Lastname: ' . $_POST["lastname"];
Basically the $_POST variables you want to use are not set. To be sure the form passes the right variables use var_dump($_POST); to see what variables it passes on.
For more information about your error see: php notice undefined variable and notice undefined index

Can't login using a PHP code and SQL [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
So here is my form I use to get the email and password, don't mind all the tabs xD.
<form method="post" action="">
<ul>
<label for='usermail'>email </label>
<input type='text' name='email' placeholder="jouwnaam#mail.nl" required>
</ul>
<ul>
<label for="password">Wachtwoord</label>
<input type="password" name="wachtwoord" placeholder="wachtwoord" required>
</ul>
<ul>
<div class='login-btn btn btn-default btn-lg'><input name="submit" type="submit" value="Inloggen"></div>
</ul>
</form>
And this is my PHP code:
<?php
if (isset($_POST['inloggen']))
{
mysql_connect("localhost","root","usbw") or die('error');
mysql_select_db("sportschool") or die('error');
$k_email = $_POST['email'];
$wachtwoord = $_POST['wachtwoord'];
echo $k_email;
if (mysql_query("SELECT * FROM klant;") == false)
{
echo mysql_error();
}
else
{
$resultaat = mysql_query("SELECT wachtwoord FROM klanten WHERE email='".$k_code."';");
$data = mysql_fetch_assoc($resultaat);
echo "<br />";
echo "<br />";
$k_wachtwoord = $data["wachtwoord"];
echo $k_wachtwoord;
if ($wachtwoord==$k_wachtwoord)
{
echo "U are logged in";
session_start();
$_SESSION['ingelogd'] = true;
$_SESSION['klantemail'] = $k_email;
}
else
{
echo "Username or Password is not right try again.";
}
echo "<br />";
mysql_close();
}
}
?>
Every time I login with the correct email and password the page is refreshed but I don't get the echo for logged in or the error for not logging in.
I have this PHP code which checks if the user is logged in, and that one stays on not logged in:
<?php
if ((isset($_SESSION['ingelogd'])) && ($_SESSION['ingelogd'] == true))
{
echo $_SESSION['klantemail']." is ingelogd";
}
else
{
echo "Nog niet ingelogd.";
}
?>
I don't use PHP a lot, so there may be a lot of mistakes.
Replace this code
if (isset($_POST['inloggen']))
with
if (isset($_POST['submit']))
Your are not checking the correct value.
Try:
if (isset($_POST['submit']) && $_POST['submit'] == "Inloggen")
The key to the post variable is the name attribute of the submit button while its value would be inloggen.
So it should be:
if (isset($_POST['submit']))

Undefined index [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I get Undefined index: image error in this code. Can I know the exact solution? I wanna know workflow from line abc to xyz that I commented in the code using //.Thanks for your help..
<?php session_start();
include("config.php");
if(isset($_SESSION['name']))
{
if(!$_SESSION['name']=='admin')
{
header("Location:login.php?id=You are not authorised to access this page unless you are administrator of this website");
}
}
?>
<?php
$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$err=$_FILES['image']['error'];
}
if($err==0)
{
move_uploaded_file($tmp, $name);
//xyz}
$category=$_POST['category'];
$title=$_POST['title'];
$image=$_FILES["image"]["name"];
$content=$_POST['content'];
}
<?php
$qry=mysql_query("INSERT INTO articles(title,image,content,category)VALUES('$title','$image','$content','$category')");
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
else
{
echo "Article Added Successfully";
}
?>
The form code is here:
<?php
include("config.php");
$sql=mysql_query("select * from category");
if(!$sql)
{
mysql_error();
}
?>
<form action="created_article.php" method="post">
Category:
<select name="category">
<?php
while($row=mysql_fetch_array($sql))
{
echo"<option value='".$row['category']."'>".$row['category']."</option>";
}
?>
</select>
Title:
<input type="text" name="title"/>
Upload Image:
<input type="file" name="image" id="image" />
Contents:
<textarea name="content" cols="100" rows="12" ></textarea>
<input type="submit" name="button" value="Submit" />
</form>
I need help with these code, I need to make project and I'm stuck here, so please kindly I request for your help,
<form action="created_article.php" method="post" enctype="multipart/form-data">
define enctype in your form tag otherwise its not work for image
While Dealing with input File always put form attribute enctype='multipart/form-data'
<?php
session_start ();
include ("config.php");
if (isset ( $_SESSION ['name'] )) {
if (! $_SESSION ['name'] == 'admin') {
header ( "Location:login.php?id=You are not authorised to access this page unless you are administrator of this website" );
}
}
if (!empty($_POST))
{
$name = $_FILES ['image'] ['name'];
$tmp = $_FILES ['image'] ['tmp_name'];
$err = $_FILES ['image'] ['error'];
if($err==0) {
move_uploaded_file($tmp, $name);
$category=$_POST['category'];
$title=$_POST['title'];
$image=$_FILES["image"]["name"];
$content=$_POST['content'];
}
$qry=mysql_query("INSERT INTO articles(title,image,content,category)VALUES('$title','$image','$content','$category')");
if(!$qry){
die("Query Failed: ". mysql_error());
} else {
echo "Article Added Successfully";
}
}
include("config.php");
$sql=mysql_query("select * from category");
if(!$sql){
mysql_error();
}
?>
The form code is here:
<form action="created_article.php" enctype='multipart/form-data' method="post">
Category: <select name="category">
<?php
while($row=mysql_fetch_array($sql))
{
echo"<option value='".$row['category']."'>".$row['category']."</option>";
}
?>
</select> Title: <input type="text" name="title" /> Upload Image: <input
type="file" name="image" id="image" /> Contents:
<textarea name="content" cols="100" rows="12"></textarea>
<input type="submit" name="button" value="Submit" />
</form>

PHP Undefined variable with POST [duplicate]

This question already has answers here:
Undefined index error PHP
(9 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I am getting this...
Notice: Undefined index: cost, sku, costMethod
I think it is the validation part that is breaking this. I have tested sending these variables without the validation and they are received fine. I believe it is when everything is valid and the header gives it the destination is where it is losing the variables.
Here is my Form code:
<!DOCTYPE HTML>
<html>
<head>
<title>Shipping Overrides Client</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#additive").click(function(){
$("#cost").attr("value","0.00");
});
});
</script>
</head>
<body>
<?php
// define variables and set to empty values
$message = "SKU is required!";
$skuErr = "";
$sku = "";
$costErr ="";
$cost = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$valid = true;
if (empty($_POST["sku"]))
{$skuErr = "SKU is required";
$valid = false;}
else
{$sku = $_POST["sku"];}
if (empty($_POST["cost"]))
{$costErr = "Cost is required";
$valid = false;
}
else
{$cost = $_POST["cost"];}
if(isset($_POST['costMethod'])){
$costMethod = $_POST["costMethod"];
}
if($valid){
header('Location: SubmitFeedSample.php?sku=$sku&cost=$cost&costMethod=$costMethod');
exit();
}
}
?>
<h1>Shipping Override Client</h1>
<form method="post" action="index.php" >
SKU: <input type="text" name="sku" value="<?php echo $sku ?>">* <?php echo $skuErr;?><br>
STD Shipping Cost: $ <input type="text" name="cost" id="cost" value="<?php echo $cost ?>">* <?php echo $costErr;?><br>
<input type="radio" name="costMethod" id="exclusive" value="Exclusive" checked>Override
<input type="radio" name="costMethod" id="additive" value="Additive" >Delete Existing Override <br>
<input type="submit" name= "submit" value="Submit">
</form>
</body>
</html>
Here is SubmitFeedSample.php:
<?php
$shippingCost = $_POST["cost"];
$sku = $_POST["sku"];
$costMeth = $_POST["costMethod"];
echo $shippingCost . "<br>";
echo $sku . "<br>";
echo $costMeth . "<br>";
var_dump($_POST);
?>
How can I get the variables to send when valid?
You are making a GET request here -
header('Location: SubmitFeedSample.php?sku=$sku&cost=$cost&costMethod=$costMethod');
Try changing this -
<?php
$shippingCost = $_GET["cost"];
$sku = $_GET["sku"];
$costMeth = $_GET["costMethod"];
echo $shippingCost . "<br>";
echo $sku . "<br>";
echo $costMeth . "<br>";
var_dump($_POST);
?>
You cannot redirect with header once headers have already been sent:
header('Location: SubmitFeedSample.php?sku=$sku&cost=$cost&costMethod=$costMethod');
That's why you are probably not getting the result there. Also that's not a proper way to fetch the post data. If you are passing arguments in URL like that you are using GET not POST.

Categories