tags:

views:

70

answers:

4

i have an eroor in sending data using textbox by POST to data base ERROR IS"NOTICE:UNDEFINED INDEX ON LINE" plz help me any one how can i solve this problem THIS IS MY CODE

<?php
error_reporting(0);
$con = mysql_connect("localhost","root","");

mysql_select_db("hello", $con);

$result = mysql_query("SELECT Max(id) as id from `aoa` WHERE 1");

$row = mysql_fetch_array($result);
  {

    echo $row['id'];
  }

 if($v=="")
{
 $v=1000;

}


$v=$row['id'];

$v=$v+1;
$id=$v;

$sql="INSERT INTO aoa(id,name,clss)
VALUES
('$id','$_POST[name]','$_POST[clss]')";
var_dump($sql);
if (!mysql_query($sql,$con))
  {
   die('Data InsertionError: ' . mysql_error());
  }
echo "Successfully Registered";

mysql_close($con);
?>

(AND THIS IS MY ERROR FILE)

Notice: C:\wamp\www\Register.php line 31 - Undefined index:  clss
Notice: C:\wamp\www\Register.php line 31 - Undefined index:  name
Notice: C:\wamp\www\Register.php line 17 - Undefined variable: v
A: 

Could you copy and paste your code exactly onto a site like pastebin.ca? Obviously removing any hardcoded database passwords.. It's difficult to read through it on this site, and I can't tell what the line numbers in your error file correspond to with respect to your code.

Gausie
Why would someone down vote this? It should be a comment to the original question though.
Pieter888
Duly noted. In my defence, I had originally sat down to write an answer. After half writing one, and reading through the question again, I realised the source code would help me along :P
Gausie
-1 - this should be a comment, and is bad advice. Don't use pastebin.ca or similar sites for questions or answers - it makes the question history useless once pastebin purges the code after 30 days or whatever the limit is, or if pastebin.ca falls off the planet. If you find the code hard to read, copy and paste it somewhere else.
Dominic Rodger
A: 

You just haven't declared your variables clss, name and v yet... At the top of your script add something like

$_POST['clss'] = null;
$_POST['name'] = null;
$v = null;

Besides you have some errors in your code. $_POST values must be provided with single or double quotes like $_POST['clss'] or $_POST["name"]. Otherwise your script will not function the way you intend it.

That should do it.

Ben Fransen
It is not required that array indexes be surrounded by quotes. It is very good practice to though.
Yacoby
since thats neater imo it's better to do so, besides, when you're using a decent editor your code gets easier to read too ;)
Ben Fransen
since he gets clss and name in POST and inserts them into DB it probably isn't a good idea to set them to null at the top of his script, don't you think ? totally agree on the quotes around indexes though
jab11
A: 

This is not an error, it is a notice. You receive them because the error_reporting php.ini directive is set so. You can eliminate it by declaring the variables before use.

Joó Ádám
+1  A: 

The issue with v being undefined is on this line

if($v==""){
    $v=1000;
}

When $v isn't set, it is not "". You could use for example

if( !isset($v) ){
    $v=1000;
}

Also, check your post flags using var_dump()

var_dump($_POST);

That way you can see if you have 'clss' and 'name' set, which I suspect they aren't.

Your code at present is also vulnerable to a sql injection, you should use mysql_real_escape_string on all variables before using them in sql.
Even better use something like PDO

Yacoby