6

I have a problem with my PHP code saying that "Notice: Undefined index" I am sure its very simple, since I am a beginner i am not getting well what is wrong exactly so please help me.

Here's the code

<?php require_once('../Connections/itemconn.php'); ?> <?php $id=$_GET['id']; $query=mysql_query("select * from manuf where id='$id' ")or die(mysql_error()); $row=mysql_fetch_array($query); ?> <form action="updateprice.php" method="post" enctype="multipart/form-data"> <table align="center"> <tr> <td> <label><strong>Item Name</strong></label></td> <td> <label> <?php echo $row['itemname']; ?></label><input type="hidden" name="id" value="<?php echo $id; ?> " /> <br /></td> </tr> <tr> <td><label><strong>Unit price </strong></label></td> <td> <input type="text" name="pass" value="<?php echo $row['unitprice']; ?> " /><br /></td> </tr> <tr> <td> <input type="reset" name="Reset" value="CANCEL" /> <br></td> <td> <input type="submit" name="Submit2" value="Update" /> </td> </tr> </table> </form> </body> </html> 
1
  • You are using $_GET['id'] but there is no ID in your URL. Are you sure you're not mixing up $_POST and $_GET ? Commented Oct 31, 2014 at 8:42

3 Answers 3

16

You are not getting value of $id=$_GET['id'];

And you are using it (before it gets initialised).

Use php's in built isset() function to check whether the variable is defied or not.

So, please update the line to:

$id = isset($_GET['id']) ? $_GET['id'] : ''; 
Sign up to request clarification or add additional context in comments.

1 Comment

for me removing form action did the thing, its changed the link before query ran so i removed the action from form.
5

if you are getting id from url try

$id = (isset($_GET['id']) ? $_GET['id'] : ''); 

if getting from form you need to use POST method cause your form has method="post"

 $id = (isset($_POST['id']) ? $_POST['id'] : ''); 

For php notices use isset() or empty() to check values exist or not or initialize variable first with blank or a value

$id= ''; 

2 Comments

yeah updated with isset() also
No, remove the empty from your answer (see link at bottom of my answer as to why you shouldn't use empty)
3

Simply add this

$id = ''; if( isset( $_GET['id'])) { $id = $_GET['id']; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.