1

When I run this code on the browser, I get an error message saying that there is an undefined index. I've spent a couple hours trying to get it to work but I just don't know why I keep getting the same exact error.

<?php
if ($dbSuccess) {
$companyID = $_POST['ID'];

$preName = $_POST['preName'];
$companyName = $_POST['Name'];
$RegType = $_POST['RegType'];
$StreetA = $_POST['StreetA'];
$StreetB = $_POST['StreetB'];
$StreetC = $_POST['StreetC'];
$Town = $_POST['Town'];
$County = $_POST['County'];
$Postcode = $_POST['Postcode'];
$COUNTRY = $_POST['COUNTRY'];

}

$tCompany_SQLupdate = "UPDATE tCompany SET ";
$tCompany_SQLupdate .= "preName = ".$preName.", ";
$tCompany_SQLupdate .= "Name = ".$companyName.", ";
$tCompany_SQLupdate .= "RegType = ".$RegType.", ";
$tCompany_SQLupdate .= "StreetA = ".$StreetA.", ";
$tCompany_SQLupdate .= "StreetB = ".$StreetB.", ";
$tCompany_SQLupdate .= "StreetC = ".$StreetC.", ";
$tCompany_SQLupdate .= "Town = ".$Town.", ";
$tCompany_SQLupdate .= "County = ".$County.", ";
$tCompany_SQLupdate .= "Postcode = ".$Postcode.", ";
$tCompany_SQLupdate .= "COUNTRY = ".$COUNTRY.", ";
$tCompany_SQLupdate .= "WHERE ID = ".$companyID." ";

if (empty($companyName)) {
    echo '<span style="color: red;">Cannot make the company name empty.</span><br /><br />';
}   else {
    echo '<span style="text-decoration: underline;">
    SQL statement</span>
    <br />'.$tCompany_SQLupdate.'<br /><br />';

    if (mysql_query($tCompany_SQLupdate)) {
        echo 'used to Successfully update the company.<br /><br />';
    }   else {
        echo '<span style="color: red;">FAILED to update the company.</span><br /><br />';
    }
}

?>

Error Message:

Notice: Undefined index: ID in C:\xampp\htdocs\forms\companyUpdate.php on line 40

Notice: Undefined index: preName in C:\xampp\htdocs\forms\companyUpdate.php on line 42

Notice: Undefined index: Name in C:\xampp\htdocs\forms\companyUpdate.php on line 43

Notice: Undefined index: RegType in C:\xampp\htdocs\forms\companyUpdate.php on line 44

Notice: Undefined index: StreetA in C:\xampp\htdocs\forms\companyUpdate.php on line 45

Notice: Undefined index: StreetB in C:\xampp\htdocs\forms\companyUpdate.php on line 46

Notice: Undefined index: StreetC in C:\xampp\htdocs\forms\companyUpdate.php on line 47

Notice: Undefined index: Town in C:\xampp\htdocs\forms\companyUpdate.php on line 48

Notice: Undefined index: County in C:\xampp\htdocs\forms\companyUpdate.php on line 49

Notice: Undefined index: Postcode in C:\xampp\htdocs\forms\companyUpdate.php on line 50

Notice: Undefined index: COUNTRY in C:\xampp\htdocs\forms\companyUpdate.php on line 51

Form:

<?php
if ($dbSuccess) {
$companyID = $_POST['companyID'];

$tCompany_SQLselect = "SELECT * ";
$tCompany_SQLselect .= "FROM ";
$tCompany_SQLselect .= "tCompany ";
$tCompany_SQLselect .= "WHERE ID = ".$companyID." ";

$tCompany_SQLselect_Query = mysql_query($tCompany_SQLselect);

while ($row = mysql_fetch_array($tCompany_SQLselect_Query, MYSQL_ASSOC)) {
    $current_preName = $row['preName'];
    $current_Name = $row['Name'];
    $current_RegType = $row['RegType'];
    $current_StreetA = $row['StreetA'];
    $current_StreetB = $row['StreetB'];
    $current_StreetC = $row['StreetC'];
    $current_Town = $row['Town'];
    $current_County = $row['County'];
    $current_Postcode = $row['Postcode'];
    $current_COUNTRY = $row['COUNTRY'];
}

echo '<h2 style="font-family: arial, helvetica, sans-serif;">
        Company EDIT form
        </h2>';

echo '<form name="postCompany" action="companyUpdate.php" method="post">';

echo '<input type="hidden" name="companyID" value="'.$companyID.'">';
echo '
    <table>
        <tr>
            <td>pre Name</td>
            <td><input type="text" name="" value="'.$current_preName.'"></td>
        </tr>
        <tr>
            <td>Name</td>
            <td><input type="text" name="" value="'.$current_Name.'"></td>
        </tr>
        <tr>
            <td>Reg Type</td>
            <td><input type="text" name="" value="'.$current_RegType.'"></td>
        </tr>
        <tr>
            <td>Street A</td>
            <td><input type="text" name="" value="'.$current_StreetA.'"></td>
        </tr>
        <tr>
            <td>Street B</td>
            <td><input type="text" name="" value="'.$current_StreetB.'"></td>
        </tr>
        <tr>
            <td>Street C</td>
            <td><input type="text" name="" value="'.$current_StreetC.'"></td>
        </tr>
        <tr>
            <td>Town</td>
            <td><input type="text" name="" value="'.$current_Town.'"></td>
        </tr>
        <tr>
            <td>County</td>
            <td><input type="text" name="" value="'.$current_County.'"></td>
        </tr>
        <tr>
            <td>Postcode</td>
            <td><input type="text" name="" value="'.$current_Postcode.'"></td>
        </tr>
        <tr>
            <td>COUNTRY</td>
            <td><input type="text" name="" value="'.$current_COUNTRY.'"></td>
        </tr>
        <tr>
            <td></td>
            <td align="right"><button type="submit">Save</button></td>
        </tr>
    </table>
';

echo "</form>";
}

?>

If you need any more details, please comment bellow.

dsas
  • 1,650
  • 18
  • 30
FocuZst
  • 45
  • 7
  • Try changing: `UPDATE tCompany SET ` to: `UPDATE \`tCompany\` SET ` – Nir Alfasi Jul 21 '15 at 22:44
  • Which one of the your code samples is companyUpdate.php? The line numbers don't match up. – dsas Jul 21 '15 at 22:46
  • On a tangent, your code is written using [mysql_fetch_array](http://www.php.net/mysql_fetch_array) which is deprecated. It is also very insecure, you should read about [SQL injection](http://stackoverflow.com/questions/60174/) – dsas Jul 21 '15 at 22:56

1 Answers1

0

Your HTML is wrong form fields need the name attribute setting, as you haven't set them they will be numerically indexed in $_POST rather than indexed by the name, use print_r($_POST) to see what I mean.

You need to update your HTML to set the name="" to name="COUNTRY" etc.

You might also have a problem that your column names in the database are all lower case (generally the case imo) and you're accessing them in different cases in your second code sample.

Try checking what $row contains inside your while loop by using print_r. You might find you need to change to:

$current_preName = $row['prename']; $current_Name = $row['name']; $current_RegType = $row['regtype']; $current_StreetA = $row['streeta']; $current_StreetB = $row['streetb']; $current_StreetC = $row['streetc']; $current_Town = $row['town']; $current_County = $row['county']; $current_Postcode = $row['postcode']; $current_COUNTRY = $row['country'];

dsas
  • 1,650
  • 18
  • 30
  • Thanks! I just had to update the name="". I have 1 error that says Undefined index: ID which is **$companyID = $_POST['ID'];** not sure what's wrong with that. – FocuZst Jul 21 '15 at 23:01
  • You have `name="companyID"` but are accessing it in the PHP code as `$_POST['ID']`. The PHP and HTML need to match. Think of it as the HTML saying to PHP "put the value of this input element inside the request index mentioned in the input element's name attribute" – dsas Jul 21 '15 at 23:11
  • @FocuZst Ask a new question for that, it has nothing to do with this one. – Jonast92 Jul 21 '15 at 23:32