0

The following code is functional and I am able to add a row to the table in my database. However, I get a notice that on the page when I run it that says

Notice: Undefined index: submit in C:\xampp\htdocs\PHP_RESUME_AD\php\ED_A.php on line 49

and am unsure why.

<html id="education" lang = "en-US">
    <head>
     <link rel="stylesheet" type="text/css" href="../css/index.css">
 <meta charset = "UTF-8">
 <title>Education</title>
 <style type = "text/css">
  table, th, td {border: 1px solid black};
 </style>
 </head>
    <body>
        <form method="post">
            Enter your Class<input type="text" name="Class"><br>
            Enter your Discipline<input type="text" name="Discipline"><br>
            Enter your Description<input type="text" name="Description"><br>
            Enter your Term<input type="text" name="Term"><br>


            <input type="submit" name="submit" value="Save">
        </form>

        <style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}

li {
display: inline;
}
</style>
<body>

 <ul
  <li><a href="../html/welcome.html">Back to Home page</a></li>
  <li><a href="../php/ED_D.php">Education </a></li>
  <li><a href="../php/SK_D.php">Skills</a></li>
  <li><a href="../php/SK_A.php">Skills Add</a></li>
  <li><a href="../php/HB_D.php">Hobbies</a></li>
  <li><a href="../php/HB_A.php">Hobbies Add</a></li>
  <li><a href="../php/PWE_D.php">Work Experience</a></li>
  <li><a href="../php/PWE_A.php">Work Experience Add  </a></li>
  <li><a href="../php/email.php">Contact Me!</a></li>
</ul>
    </body>
</html>

<?php
   if(!empty($_POST["submit"]=="Save"))
   {
       $Class = $_POST["Class"];
       $Discipline = $_POST["Discipline"];
       $Description = $_POST["Description"];
       $Term = $_POST["Term"];

       mysql_connect("localhost", "root", "HAPPY3");
       mysql_select_db("cs266ad_db1");

       $s="insert into education(Class,Discipline,Description,Term)   values('".$Class."','".$Discipline."','".$Description."','".$Term."')";

       mysql_query($s);
   }
   ?>
tadman
  • 208,517
  • 23
  • 234
  • 262
Dillon Burke
  • 49
  • 1
  • 8
  • 1
    The `$_POST["submit"]` isn't populated on the first load. This is open to SQL injections as well. Use `isset` or `!empty`. – chris85 Apr 25 '16 at 21:22
  • 1
    Because `$_POST` is empty when you first open the page. It's only populated on post requests. And all `mysql_` functions have been removed in PHP7, you should upgrade to PDO or MySQLi. – Arjan Apr 25 '16 at 21:23
  • Side note: do your processing before sending output (before your html). – Wesley Murch Apr 25 '16 at 21:23
  • @chris85 tried using isset or !empty, neither worked – Dillon Burke Apr 25 '16 at 21:30
  • You still got the error/notice...or? Update the question with your usage. – chris85 Apr 25 '16 at 21:31
  • @chris85 i get the same notice – Dillon Burke Apr 25 '16 at 21:31
  • Show your usage because that shouldn't throw a notice if used properly. Use the `edit` below the `tags`. – chris85 Apr 25 '16 at 21:33
  • sorry just edited it, added the !empty – Dillon Burke Apr 25 '16 at 21:38
  • `if(!empty($_POST["submit"]) && $_POST["submit"]=="Save"))` or just us the `!empty()` check; `if(!empty($_POST["submit"])){`. – chris85 Apr 25 '16 at 21:39
  • **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and you have severe [SQL injection bugs](http://bobby-tables.com/) here. – tadman Apr 25 '16 at 21:39
  • @chris85 the first line of code worked! thank you alot, care to explain though why it did? – Dillon Burke Apr 25 '16 at 21:44
  • The `!empty` was checking the return of `$_POST["submit"]=="Save"`. So `$_POST["submit"]` was still being evaluated and since it was undefined/unset it threw the notice. – chris85 Apr 25 '16 at 23:57

0 Answers0