0

I have been working on adding data to my sql database i have tryed many different ways of doing this but am still gettting the same errors.

Notice: Undefined index: ModuleId in N:\ftp\compc\d11os\Project\addModule.php on line 63

Notice: Undefined index: Title in N:\ftp\compc\d11os\Project\addModule.php on line 64

Notice: Undefined index: CreditLevel in N:\ftp\compc\d11os\Project\addModule.php on line 65

Notice: Undefined index: CreditPoints in N:\ftp\compc\d11os\Project\addModule.php on line 66

Notice: Undefined index: Status in N:\ftp\compc\d11os\Project\addModule.php on line 67

what am i doing wring i know it probly somthing smalll but i cant see it and really need help?

here is the code i am using

 <?php
$con=mysqli_connect("localhost","ROOT","ROOT","ROOTdb");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$ModuleId = mysqli_real_escape_string($con, $_POST['ModuleId']);
$Title = mysqli_real_escape_string($con, $_POST['Title']);
$CreditLevel = mysqli_real_escape_string($con, $_POST['CreditLevel']);
$CreditPoints = mysqli_real_escape_string($con, $_POST['CreditPoints']);
$Status = mysqli_real_escape_string($con, $_POST['Status']);
$Award = mysqli_real_escape_string($con, $_POST['Award']);


$sql="INSERT INTO module(ModuleId, Title, CreditLevel, CreditPoints, Status, Award)
VALUES ('$ModuleId', '$Title', '$CreditLevel' ,'$CreditPoints', '$Status', '$Award')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

Html code im using

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">

        <img src="logo.jpg" alt="University of Ulster Logo" width="332" height="132">

    <h1 style="font-family:Bell MT;color:blue;font-size:28px;">Add a New Module.</h1>

</head>
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

 <table border="0">

  <tr><td>Module:</td><td>

 <input type="text" name="name" maxlength="60" required>

 </td></tr>

  <tr><td>Title:</td><td>

 <input type="text" name="telephoneNo" maxlength="60" required>

 </td></tr>

 <tr><td>CreditLevel:</td><td>

 <input type="text" name="email" maxlength="60" required>

 </td></tr>

 <tr><td>CreditPoints:</td><td>

 <input type="text" name="course" maxlength="60" required>

 </td></tr>

 <tr><td>Status:</td><td>

 <input type="text" name="Staus" maxlength="100" required>

 </td></tr>

  <tr><td>Award:</td><td>

 <input type="text" name="Award" maxlength="100" required>

 </td></tr>

 <td colspan="2" style="text-align:center">
<input type="submit" name='submit' value='Submit'>

 </form>

1 Answers1

0

The issue is appearing because the variables you are using are not defined in the $_POST array.

Try something like

if (isset($_POST['ModuleId']))
{
    $ModuleId = mysqli_real_escape_string($con, $_POST['ModuleId']);
} else {
    $ModuleId = -1; // Default value or Error flag
    echo "Module Id is not specified.";
}

and so on for your other variables.

It is probably a good idea to check that all the values are present, prior to insert. For example,

if ($ModuleId != -1) {
    $sql="INSERT INTO module(ModuleId, Title, CreditLevel, CreditPoints, Status, Award)
    VALUES ('$ModuleId', '$Title', '$CreditLevel' ,'$CreditPoints', '$Status', '$Award')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";
}

mysqli_close($con);

// EDIT

You issue seems to be stemming from not checking if the page has done a post back. When the page is first loaded, it will try to insert data - but there is no data to insert as you have not submitted the form.

Wrap your php code with a simple check like so

<?php
if (isset($_POST['submit']))
{
    $con=mysqli_connect("localhost","ROOT","ROOT","ROOTdb");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    // escape variables for security
    $ModuleId = mysqli_real_escape_string($con, $_POST['ModuleId']);
    $Title = mysqli_real_escape_string($con, $_POST['Title']);
    $CreditLevel = mysqli_real_escape_string($con, $_POST['CreditLevel']);
    $CreditPoints = mysqli_real_escape_string($con, $_POST['CreditPoints']);
    $Status = mysqli_real_escape_string($con, $_POST['Status']);
    $Award = mysqli_real_escape_string($con, $_POST['Award']);


    $sql="INSERT INTO module(ModuleId, Title, CreditLevel, CreditPoints, Status, Award)
        VALUES ('$ModuleId', '$Title', '$CreditLevel' ,'$CreditPoints', '$Status', '$Award')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";

    mysqli_close($con);
}
?>
Kami
  • 19,134
  • 4
  • 51
  • 63
  • the code i am using is adding a record to the sql table but it not adding the information i and typing it it is just adding in black Fields? – user3605679 May 06 '14 at 12:32
  • @user3605679 Update your question with the html, it is likely that the form is not submitting correctly, or using `GET` instead of `POST`. – Kami May 06 '14 at 12:34
  • @user3605679 i have added the html as requested – user3605679 May 06 '14 at 12:47
  • @user3605679 Thanks, I have updated the answer. You need a simple check for form submission. – Kami May 06 '14 at 12:51
  • i have done that but know im getting a error "object not found" – user3605679 May 06 '14 at 13:20
  • @user3605679 you are missing something, I have typed out the code as it should look. – Kami May 06 '14 at 13:45
  • i have check this and still getting the object error would o=it have anythign till do with the following
    – user3605679 May 06 '14 at 14:04
  • @user3605679 The object required will give you a line number, work with that. Also, that line should include an `echo` command - – Kami May 06 '14 at 14:09
  • i was missing a " ad the end of my post acusing the issue the form is now working and adding data to the database :) – user3605679 May 06 '14 at 14:30