0

Please help I'm new in coding php mysql This code is giving me errors like:

Notice: Undefined index: name in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\update1_ac.php on line 15

Notice: Undefined index: surname in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\update1_ac.php on line 15

Notice: Undefined index: contact_number in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\update1_ac.php on line 15

Notice: Undefined index: email in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\update1_ac.php on line 15

Notice: Undefined index: position in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\update1_ac.php on line 15

Notice: Undefined index: user_name in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\update1_ac.php on line 15

Notice: Undefined index: password in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\update1_ac.php on line 15 ERROR can not update data

This is the code

<?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password `enter code here`
    $db_name="administrator"; // Database name 
    $tbl_name="players"; // Table name 

// Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    if (isset($_POST['submit'])){
// update data in mysql database 
    $sql="UPDATE $tbl_name ports SET name='".$_POST['name']."', Surname='".$_POST['surname']."', contact='".$_POST['contact_number']."', email='".$_POST['email']."', position='".$_POST['position']."', user_name='".$_POST['user_name']."', password='".$_POST['password']."' WHERE id='".$_POST['player_id']."'";

    $result=mysql_query($sql);

// if successfully updated. 
    if(!empty($result)){                     //The error is here
    echo "Successful";
    echo "<BR>";
    echo "<a href='list2_player.php'>View result</a>";
        }
    }
    else {
    echo "ERROR can not update data";
        }
    ?>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126

3 Answers3

2

Although we answered this a moment ago, you shouldn't be using variables in your SQL when you are trying to run an update and specifying column names.

Your SQL statement should read like this:

$sql="UPDATE $tbl_name ports SET name='".$_POST['name']."', // etc etc
                                 ^ Column names don't need $

The problem is that your code is basically looking for the variable called $name which you haven't specified in your code rather than simply using the column name.

Community
  • 1
  • 1
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • thank you, now I'm getting undefined index: name, surname, contact_number...password – user2859119 Oct 10 '13 at 11:12
  • @user2859119 Looks like your form isn't being submitted properly, or you are running the code before you submit the form. – Fluffeh Oct 10 '13 at 11:14
  • thank you! I've edited the code now the error is undefined index please check the code above – user2859119 Oct 10 '13 at 11:20
  • You should concat all variables the **proper way**, `"UPDATE " . $tbl_name . " ports SET name='" . $_POST..... . "'`. **Undefined index** means, there is no index `'name'` in the array `$_POST`. **Capture these** with `array_key_exists('name', $_POST);` or `isset($_POST['name']);` – Daniel W. Oct 10 '13 at 11:29
0

Here is your error

$sql="UPDATE $tbl_name ports SET $name='".$_POST['name']."', $Surname='".$_POST['surname']."', $contact='".$_POST['contact_number']."', $email='".$_POST['email']."', $position='".$_POST['position']."', $user_name='".$_POST['user_name']."', $password='".$_POST['password']."' WHERE id='".$_POST['player_id']."'";

This wont work, as php thinks that in a "" (Double quote) string, $[variablename] means a variable. So it thinks you are trying to substitute the '$name' with the value of the variable called name, which obviously doesnt exist.

MySQL doesnt need the $ sign to come before its variable name. Use this code instead

$sql="UPDATE $tbl_name ports SET name='".$_POST['name']."', Surname='".$_POST['surname']."', contact='".$_POST['contact_number']."', email='".$_POST['email']."', position='".$_POST['position']."', user_name='".$_POST['user_name']."', password='".$_POST['password']."' WHERE id='".$_POST['player_id']."'";

Also, the 'Surname' is capitalized, everything else is not. You may want to double check that, as its usually not recommended to capitalize column and variable names's first letter.

Ps: Since you are new to PHP and MySQL, i should warn you that your code is vulnerable to a attack called the 'SQL Injection attack'. Its a trick for a hacker to execute their own SQL code on your server, you can read more about that here.

I prefer to use PDO(PHP data object), it is easier to use it and it also allows me to use something called 'prepared statements', which are safe from SQL injection attacks. You can read more about PDO here. Or you can read a simple tutorial about it on nettuts+

Aayush Agrawal
  • 1,354
  • 1
  • 12
  • 24
0

I think you are calling the update sql before form submit . First you need check before Update

if(isset($_POST['submit']))
{
// update data in mysql database 
    $sql="UPDATE $tbl_name ports SET name='".$_POST['name']."', Surname='".$_POST['surname']."', contact='".$_POST['contact_number']."', email='".$_POST['email']."', position='".$_POST['position']."', user_name='".$_POST['user_name']."', password='".$_POST['password']."' WHERE id='".$_POST['player_id']."'";

    $result=mysql_query($sql);

// if successfully updated. 
    if(!empty($result)){                     //The error is here
    echo "Successful";
    echo "<BR>";
    echo "<a href='list2_player.php'>View result</a>";
        }

    else {
    echo "ERROR can not update data";
        }
}
vijaykumar
  • 4,658
  • 6
  • 37
  • 54