0

I have been at it for 2 days. I'm new to PHP. I have been trying to create a form that sends data to mysql. I have created a form and also the PHP code for this but it just doesn't update in mysql. All of this is code is in the file name add-courses.php . Help Please.

 <form method="post" action="add-courses.php">

                Course Title:<input type="text" name="courseTitle"/> <br/>

                Course Description:<input type="text" name="courseDesc"/> <br/>

                Course Duration:<input type="text" name="courseDuration"/> <br/>

                Course Start Date:<input type="date" name="courseStartDate"/> <br/>

                Course End Date:<input type="date" name="courseEndDate"/> <br/>

                Course Price:<input type="text" name="coursePrice"/> <br/>

                Course Trainer:<input type="text" name="courseTrainer"/> <br/>

                Course Status:<input type="text" name="courseStatus"/> <br/>

                <input type="button" value="Add Course" name="submit"/>

            </form>

            <?php 

            //Defining Database Constants
            define('DBHOST', 'localhost');
            define('DBUSER', 'root');
            define('DBPASS', '');
            define('DBNAME', 'demo');

            //Connecting to database 
            $connection = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

            //Form Variables
            if (isset($_POST['submit'])) {
                $courseTitle = $_POST['courseTitle'];
                $courseDesc = $_POST['courseDesc'];
                $courseDuration = $_POST['courseDuration'];
                $courseStartDate = $_POST['courseStartDate'];
                $courseEndDate = $_POST['courseEndDate'];
                $coursePrice = $_POST['coursePrice'];
                $courseTrainer = $_POST['courseTrainer'];
                $courseStatus = $_POST['courseStatus'];

            //Courses Sql Entry
                $sql = "INSERT INTO courses (courseTitle, courseTitle, courseDuration, courseStartDate, courseEndDate, coursePrice, courseTrainer, courseStatus) VALUES ('$courseTitle', '$courseDesc', '$courseDuration', $courseStartDate', '$courseEndDate', '$coursePrice', '$courseTrainer', '$courseStatus')";
            //Query Result to Msql
                $result = mysqli_query($connection,$sql);

            if (!$result) {
                die('Unable to add records');
                }

            else {
                echo "Records added sucessfully";
                }

            }

             ?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Arif Khan
  • 13
  • 4
  • 1
    Typo at `$conncetion` it would be `$connection` Vote to close it – Saty Jun 09 '16 at 07:46
  • @Saty If this is actually the case, what a waste of 2 days. – Parthapratim Neog Jun 09 '16 at 07:49
  • You should read up on sql injection. – jeroen Jun 09 '16 at 07:50
  • try to watch error logs next time :) it'll save you 2 days – Alex Jun 09 '16 at 07:51
  • Take a look at this question: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 How to prevent SQL-injection –  Jun 09 '16 at 07:52
  • It could be everything please give us more infos.. Is this "Unable to add records" printed? You are receiving some other error? Or your insert looks good but you don't see the data on the db? – Paolof76 Jun 09 '16 at 08:00
  • Actually I have used the correct spelling for conncection before also it just doesn't work and that typo is probably last night when. I tried to re-write the whole thing again. Do you see any other issue other than that typo which I am going to edit now. – Arif Khan Jun 09 '16 at 08:02
  • Use `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` to check error in page – Saty Jun 09 '16 at 08:04
  • @Paolof76. The data does not get submitted at all to the mysql. Neither do I get an error nor success message from either statement. When I submit the data just sits there in the form and nothing happens. – Arif Khan Jun 09 '16 at 08:04
  • @saty added and checked no errors. – Arif Khan Jun 09 '16 at 08:11
  • Just change ` – Saty Jun 09 '16 at 08:17
  • It is letting me submit but it gives unable to add records message which I have enetered – Arif Khan Jun 09 '16 at 10:08

1 Answers1

0

I figured this out. The problem lied in the database table column which stores date in YYYYMMDD format and using the standard HTML5 date input tag it sends it in DDMMYYYY format. I removed everything and kept only one field at a time that is how I came to this conclusion.

I am sorry I did not provide proper information earlier. Thanks for all your support.

Arif Khan
  • 13
  • 4