2016-03-01 90 views
-1

即时通讯有列错误,但我似乎无法找到问题。我试图增加患者detials到一个名为“患者”的表。我得到以下错误:使用PHP表单向数据库添加数据错误

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

任何人都可以帮助至于为什么?

PHP代码:

<?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 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> 
+0

'$性别,$地址,$ LandlineTel,$ MobileTel,$ DOB,$ Conditions,$ NextOfKin,$ ClinicNo,$ Appointments'都应在SQL中引用。这也对SQL注入开放;查看'pdo'或'mysqli'并使用预准备语句。 – chris85

+0

这里有很多明显的错误 –

+0

重复现在适用于*“我删除了NOW()并且它产生了一个sytax错误:*来自OP。 –

回答

0

您的查询没有指定你要插入的NOW()值成什么样列。你有11列和12个值。

+0

我删除了NOW(),它产生了一个sytax错误: 无法输入数据:您的SQL语法有错误;请检查对应于您的MariaDB服务器版本的手册,以找到在第1行的'Mews,39393939,939393399,99399,EBS,CRS,BBE,Carl Jones,33452234,N'附近使用的手册 – CarlM

+0

put'die($ sql) ;''下面'$ sql = ...'并检查你的查询是否有问题。 – WheatBeak

+0

此外,你应该使用mysqli而不是mysql http://php.net/manual/en/book.mysqli.php – WheatBeak

相关问题