0

I am trying since last hour to update my database using php but unable to update.

I get the error

Notice: Undefined index: intakeid in C:\wamp\www\Multi_Edit\edit_save.php on line 3.

<?php
include('dbcon.php');
$intakeid=$_POST['intakeid'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$password=$_POST['password'];
$confirmpassword=$_POST['confirmpassword'];
$homeaddress=$_POST['homeaddress'];
$email=$_POST['email'];

$N = count($intakeid);
for($i=0; $i < $N; $i++)
{
    $result = mysql_query("UPDATE registration SET firstname='$firstname[$i]',
                                                   lastname='$lastname[$i]',
                                                   password='$password[$i]',
                                                   confirmpassword='$confirmpassword[$i]',
                                                   homeaddress='$homeaddress[$i]',
                                                   email='$email[$i]'
                                             WHERE intakeid='$intakeid[$i]`enter code here`'")or die(mysql_error());
}
error_reporting(E_ALL);
ini_set("display_errors", 5);
?>
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • show view, it seems like there is no POST data here – Alex Apr 20 '14 at 04:54
  • You have a host of issues. To start off, you need to check if POST values exist using `isset()` – karthikr Apr 20 '14 at 04:55
  • first submit the form then you can get these POST values – ɹɐqʞɐ zoɹǝɟ Apr 20 '14 at 05:02
  • SQL Injection, not checking if fields are present and valid, using deprecated library, multiple queries to the same end where you could resume it using `IN` instead of a loop. Start by showing us your form that is used with the above code along with the result of `print_r($_POST);` before `$intakeid=$_POST['intakeid'];`. – Prix Apr 20 '14 at 05:02
  • check is there value stored in `$_POST['intakeid'];` using `echo $_POST['intakeid'];`. if no then check form where value submited – Gopal Joshi Apr 20 '14 at 05:05
  • @Sameer except your case is not different than what OP have and before it `echo` it would give the NOTICE, the thing is since his form does not have that field it will be undefined so he needs to make sure that field is set before using it be it on an `echo` or assigning it to a variable. – Prix Apr 20 '14 at 05:09
  • @user3533392 [Click here and update your question with the new information as code is very hard to read on comments.](http://stackoverflow.com/posts/23178569/edit) also that is only 1 line of your form, post your entire form from `` to `` – Prix Apr 20 '14 at 05:14
  • @Prix then its mistake in form where parameter is set. – Gopal Joshi Apr 20 '14 at 05:15
  • @Sameer that is what we already told him. – Prix Apr 20 '14 at 05:16
  • Your code is vulnerable to SQL injections. You should read on [how to prevent them in PHP](http://stackoverflow.com/q/60174/53114). – Gumbo Apr 20 '14 at 06:34

2 Answers2

1

Its always best method to check $_POST['value'] variable has value set or not using isset() function , otherwise you will get NOTICES as you have it already. check this

Community
  • 1
  • 1
Wit Wikky
  • 1,542
  • 1
  • 14
  • 28
0

It happens simply there is no variable called "intakeid" in $_POST array. If you still need to get it (whereas it is set) you can do something like follows.

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

This will put $intakeid to null if the post variable not have the value and if it has it will put the posted value

Ruwantha
  • 2,603
  • 5
  • 30
  • 44
  • `isset` will already return `false` for variables with `null` values. So `$_POST['intakeid'] != NULL` is redundant. – Gumbo Apr 20 '14 at 06:36