Jump to content

[SOLVED] Undefined index issue


oraya

Recommended Posts

Can anyone see why i'm getting undefined index errors?  This is driving me loopy

 

<?php

include_once '../inc/config.php';

include_once 'hearse.php';

 

$id=mysql_real_escape_string($_POST['id']);

$cat=mysql_real_escape_string($_POST['cat']);

$title=mysql_real_escape_string($_POST['title']);

$info=mysql_real_escape_string($_POST['info']);

$price=mysql_real_escape_string($_POST['price']);

 

$query = "INSERT INTO eco_friendly VALUES ('$id', '$cat', '$title', '$info', '$price')";

mysql_query($query);

    header("Location: added.php");

 

mysql_close();

?>

 

I've gone over everything, all is correct and as it should be but it's just throwing a hissy fit.  Sigh this is driving me mad slowly.  If anyone can see anything that they think might be the reason, could you let me know.  I've checked form, and database, I haven't miss spelled any fields.  I just don't understand it.  I have other input forms that are identical and they work just fine.

 

Many thanks in advance,

Oraya

Link to comment
Share on other sites

Here it is:

 

Notice: Undefined index: id in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 5

 

Notice: Undefined index: cat in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 6

 

Notice: Undefined index: title in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 7

 

Notice: Undefined index: info in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 8

 

Notice: Undefined index: price in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 9

 

Warning: Cannot modify header information - headers already sent by (output started at c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php:5) in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 14

 

I've checked every thing and can't find any thing a miss.

Oraya

Link to comment
Share on other sites

Since they are all undefined, that would indicate that either the code is being executed unconditionally (not as the result of the form being submitted) or the form is not setting any $_POST variables.

 

Still waiting for your form code to be posted to provide the quickest solution. There are a number of possible reasons and seeing actual code is the quickest way to find a solution.

Link to comment
Share on other sites

<form action="post_add.php" method="post">

<input type="hidden" name="id" />

Category: <select name="cat">

<option value="">Select Category</option>

<option value=""></option>

<option value="Coffin">Coffin</option>

<option value="Casket">Casket</option>

<option value="Urn">Urn</option>

</select>

Title: <input type="text" size="60" name="title" />

Description: <textarea name="info"></textarea>

Price: <input type="text" size="10" maxlength="10" name="price" />

<input type="button" value="Cancel Add" onClick="history.go(-1)" /> <input type="submit" value="Add New Eco Product" />

</form>

 

Sorry yes I agree should have posted it with other.  Thank you so much for your help, I'm very greatful.  I truly is driving me mad. lol...

Oraya

Link to comment
Share on other sites

<?php
include_once '../inc/config.php';
include_once 'hearse.php';
// assuming all your fields are required and they cannot be empty
foreach($_POST as $key=>$val){
  $error = 0;
  if (!isset($_POST[$key])){
      $error ++;
  }
}
if($error<=0){
  $id =mysql_real_escape_string($_POST['id']);
  $cat=mysql_real_escape_string($_POST['cat']);
  $title=mysql_real_escape_string($_POST['title']);
  $info=mysql_real_escape_string($_POST['info']);
  $price=mysql_real_escape_string($_POST['price']);
  
  $query = "INSERT INTO eco_friendly VALUES ('$cat', '$title', '$info', '$price')";
  mysql_query($query);
  mysql_close();
  header("Location: added.php");
  exit;
  
}
else{
  echo  'please fill up the form completely';
}
?>

try not tested but should work  ;)

Link to comment
Share on other sites

I can't explain this, BUT I just re-wrote the post_add.php and the form, and it's now working.  I re-named old as BCKUP and compaired them to the re-writes, they are identical. But the new ones work.  How strange is that!  Thank you both for you patience and help, you may not know it, but you helped a lot.  Well you saved me from becoming bald anyway. lol.... 

 

Thank you soooooooo much for trying to help!

Best wishes,

Oraya

Link to comment
Share on other sites

Now I just re-wrote the original.  I didn't see your code until after I had re-written it.  I doesn't make any sense, but there you go.  I've gone over it line by line and they are both the same.  It's really odd.

 

Thank you for taking the time to write and post the code though.

Oraya

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.