0

I have an error in sending data using textbox by POST to data base ERROR IS"NOTICE:UNDEFINED INDEX ON LINE"

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
Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50
Mehwish
  • 11
  • 1

3 Answers3

1

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
  • 54,544
  • 15
  • 116
  • 120
0

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
  • 909
  • 1
  • 11
  • 22
-1

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
  • 10,884
  • 18
  • 76
  • 129
  • It is not required that array indexes be surrounded by quotes. It is very good practice to though. – Yacoby Nov 30 '09 at 10:21
  • 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 Nov 30 '09 at 10:23
  • 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 Nov 30 '09 at 11:31