-1

im having an column error, but i cant seem to find the problem. Im trying to add patient detials to a table called "Patient". And i get the following error:

Could not enter data: Column count doesn't match value count at row 1

Can anyone help as to why?

PHP Code:

 <?php
     if(isset($_POST['add'])) {
        $dbhost = 'localhost';
        $dbuser = 'carl';
        $dbpass = 'password';

        $conn = mysql_connect($dbhost, $dbuser, $dbpass);

        if(! $conn ) {
           die('Could not connect: ' . mysql_error());
        }


            $PatientName = $_POST['PatientName'];
            $IDNumber = $_POST['IDNumber'];
            $Gender = $_POST['Gender'];
            $Address = $_POST['Address'];
            $LandlineTel = $_POST['LandlineTel'];
            $MobileTel = $_POST['MobileTel'];
            $DOB = $_POST['DOB'];
            $Conditions = $_POST['Conditions'];
            $NextOfKin = $_POST['NextOfKin'];
            $ClinicNo = $_POST['ClinicNo'];
            $Appointments = $_POST['Appointments'];

        $sql = "INSERT INTO Patient ". "(PatientName,IDNumber, Gender, Address, LandlineTel, MobileTel, DOB, Conditions, NextOfKin, ClientNo, Appointments) ". "VALUES('$PatientName','$IDNumber', $Gender, $Address, $LandlineTel, $MobileTel, $DOB, $Conditions, $NextOfKin, $ClinicNo, $Appointments, NOW())";

        mysql_select_db('MedicalDB');
        $retval = mysql_query( $sql, $conn );

        if(! $retval ) {
           die('Could not enter data: ' . mysql_error());
        }

        echo "Entered data successfully\n";

        mysql_close($conn);
     }else {
        ?>

HTML Form Code:

<form method = "post" action = "<?php $_PHP_SELF ?>">
              <table width = "400" border = "0" cellspacing = "1" 
                 cellpadding = "2">

                 <tr>
                    <td width = "100">Employee Name</td>
                    <td><input name = "PatientName" type = "text" 
                       id = "PatientName"></td>
                 </tr>

                 <tr>
                    <td width = "100">ID Number</td>
                    <td><input name = "IDNumber" type = "text" 
                       id = "IDNumber"></td>
                 </tr>

                 <tr>
                    <td width = "100">Gender</td>
                    <td><input name = "Gender" type = "text" 
                       id = "Gender"></td>
                 </tr>
                  <tr>
                    <td width = "100">Address</td>
                    <td><input name = "Address" type = "text" 
                       id = "Address"></td>
                 </tr>
                  <tr>
                    <td width = "100">LandlineTel</td>
                    <td><input name = "LandlineTel" type = "text" 
                       id = "LandlineTel"></td>
                 </tr>
                  <tr>
                    <td width = "100">MobileTel</td>
                    <td><input name = "MobileTel" type = "text" 
                       id = "MobileTel"></td>
                 </tr>
                  <tr>
                    <td width = "100">DOB</td>
                    <td><input name = "DOB" type = "text" 
                       id = "DOB"></td>
                 </tr>
                  <tr>
                    <td width = "100">Conditions</td>
                    <td><input name = "Conditions" type = "text" 
                       id = "Conditions"></td>
                 </tr>
                  <tr>
                    <td width = "100">Next Of Kin</td>
                    <td><input name = "NextOfKin" type = "text" 
                       id = "NextOfKin"></td>
                 </tr>
                  <tr>
                    <td width = "100">Clinic Number</td>
                    <td><input name = "ClinicNo" type = "text" 
                       id = "ClinicNo"></td>
                 </tr>
                   <tr>
                    <td width = "100">Appointments</td>
                    <td><input name = "Appointments" type = "text" 
                       id = "Appointments"></td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td> </td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td>
                       <input name = "add" type = "submit" id = "add" 
                          value = "Add Employee">
                    </td>
                 </tr>

              </table>
           </form>
chris85
  • 23,846
  • 7
  • 34
  • 51
CarlM
  • 33
  • 8
  • `$Gender, $Address, $LandlineTel, $MobileTel, $DOB, $Conditions, $NextOfKin, $ClinicNo, $Appointments` should all be quoted in SQL. This is also open to SQL injections; look into `pdo` or `mysqli` and using prepared statements. – chris85 Mar 01 '16 at 21:35
  • quite a few obvious errors here – Funk Forty Niner Mar 01 '16 at 21:35
  • Duplicate is now applicable as per *"I removed the NOW() and it produced a sytax error:"* from the OP. – Funk Forty Niner Mar 01 '16 at 21:37

1 Answers1

0

Your query doesn't specify what column you're trying to insert the value of NOW() into. You have 11 columns and 12 values.

WheatBeak
  • 1,036
  • 6
  • 12
  • I removed the NOW() and it produced a sytax error: Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Mews, 39393939, 939393399, 99399, EBS,CRS,BBE, Carl Jones, 33452234, N' at line 1 – CarlM Mar 01 '16 at 21:31
  • put `die($sql);` below `$sql = ...` and check your query for problems. – WheatBeak Mar 01 '16 at 21:35
  • Also, you should be using mysqli instead of mysql http://php.net/manual/en/book.mysqli.php – WheatBeak Mar 01 '16 at 21:36
  • @WheatBeak suggesting the OP to use mysqli won't fix their syntax error. – Funk Forty Niner Mar 01 '16 at 21:38
  • @Fred -ii- , true but it doesn't hurt to suggest the proper way. I didn't even notice he wasn't quoting properly because the first few variables are quoted but not the rest. – WheatBeak Mar 01 '16 at 21:40
  • @WheatBeak `mysqli_` *with* a prepared statement, *yes* ;-) – Funk Forty Niner Mar 01 '16 at 21:42
  • Also, technically it's not a duplicate of that question because the OP's original problem was a column/value mismatch not a syntax error. :-P – WheatBeak Mar 01 '16 at 21:43