Undefined index [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 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>

Related

PHP - Unidentified Index Variables [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 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.

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']))

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.

line 75 : if (is_array($errors)) [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 9 years ago.
Error!
The following error(s) occurred:
Notice: Undefined variable: errors in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\scripts\ladies_paradise\checkout.php on line 75
Please try again.
`
Untitled Document
<body>
<?php
$page_title='ORDER FORM';
if(isset($_POST['submitted'])){
$errors= array();
$price=($_POST['price']);;
$quantity=($_POST['quantity']);
$total = $quantity * $price;
$total = number_format ($total, 2);
// Validate the name and combat Magic Quotes, if necessary.
if (empty ($_POST['product'])) {
$errors[]='You forgot to enter product.';
}else{
$pr=trim($_POST['product']);
}
// Validate the price.
if (empty ($_POST['price'])) {
$errors[] ='You forgot to enter price.';
} else {
$p=trim($_POST['price']);
}
// Validate the quantity.
if (empty ($_POST['quantity'])) {
$errors[] ='You forgot to enter quantity.';
} else {
$q=trim($_POST['quantity']);
}
if(empty($errors)){
require_once('checkout_connect.php');
$query="INSERT INTO customers(product,price,quantity)VALUES('$pr','$p','$q')";
$result=#mysql_query($query);//Run the query.
if($result){
echo 'You are purchasing <b>', $c. '</b>.
Total price is <b>$', $total, '</b>.';
} else { // One from element was not filled out properly.
echo '<p><font color="orange">Please go back and fill out the form again.</font></p>';
}
exit();
}else{ //If it did not run OK.
echo '<h1 id="mainhead">System Error</h1>
<p class="error">You could not registered due to system error.We apologize for any inconvenience.</p>';//Public message.
echo '<p>'.mysql_error().'<br/><br/>'.$query.'</p>'; //Debugging message.
exit();
}
mysql_close(); //Close the database connection.
}else{ //Report the errors.
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
if (is_array($errors)) {
foreach ($errors as $msg){ //Print each error.
echo " - $msg<br />\n";
}
}
echo '</p><p>Please try again.</p><p><br /></p>';
}//End of if (empty($errors)) IF.
?>
<h2>ORDER FORM:</h2>
<form action="checkout.php" method="post">
<p>Product: <input type="number" name="code_item" size="15" maxlength="15" value"<?php if (isset($_POST['product'])) echo $_POST['product']; ?>" /></p>
<p>Price: <input type="number" name="price" size="15" maxlength="30" value"<?php if (isset($_POST['price'])) echo $_POST['price']; ?>" /></p>
<p>Quantity: <input type="number" name="quantity" size="30" maxlength="50" value"<?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>``
Define it at the top of the document.
<?php
$errors = false;
Alternatively, check if it is set using a ternary where assignment is necessary.
echo isset($errors) ? $errors: '';
Where '' can be replaced with whatever assignment correlates to the requirements.

php and get method error

I have this code :
<?php
if ($_GET["do"]=="success") {
echo "SUCCESS";
}
?>
<form action="file.php?do=success" method="post">
<input type="text"><input type="submit">
</form>
And I get this error:
Notice: Undefined index: do in C:\Program Files\EasyPHP-5.3.8.1\www\m\file.php on line 2
What do I need to do?
You need to check if the index exists before trying to access it:
<?php
if (isset($_GET["do"]) && $_GET["do"] == "success") {
echo "SUCCESS";
}
?>
<form action="file.php?do=success" method="post">
<input type="text"><input type="submit">
</form>
Try this:
<?php
if (!empty($_GET['do']) && $_GET["do"]=="success") {
echo "SUCCESS";
}
?>
<form action="file.php?do=success" method="get">
<input type="text"><input type="submit">
</form>

Categories