查询无效:SQL语法中存在错误; (插入错误)

时间:2015-04-16 17:15:04

标签: php mysql

所以我是php&的新手MySQL的。每次填写我创建的表单时,我都会收到此错误。当我检查phpmyadmin以查看表单中的信息是否已添加到表中时,它无处可寻。我在这里找到了一个类似的问题,通过运行查询来关闭SQL的严格模式来解决他们的问题。我试图这样做,它在表中添加了一个条目,但条目中的所有值都是0.这里是我的php块:

<?php

    require('connect2.php');
    session_start();
    $username = $_SESSION['username'];


    //My queries will be here
    $bloodquery = mysql_query("SELECT * FROM `CodesBloodType` ORDER BY `BloodTypeText`");
    $donor_add = "INSERT INTO `Donor`(`DonorID`, `PersonID`, `DateRegistered`, `AgeRegistered`, `DonorPreRegistered`, `MedicalFacilityID`, `NationalLocalRegistry`, `NationalLocalRegistryID`, `Height`, `Weight`, `BloodTypeCode`, `OrganCriteriaID`, `LivingDonor`, `DirectedDonor`) VALUES ($donorid,$personid,$dateregistered,$ageregistered,$donorpreregistered,$medicalfacilityid,$nationallocalregistry,$nationallocalregistryid,$height,$weight,$bloodtypecode,$organcriteriaid,$livingdonor,$directeddonor)";




//Check to see if something is entered in my fields, if so then define variables


// Loop over field names, make sure each one exists and is not empty



$required = array('donorid', 'personid', 'dateregistered', 'ageregistered','medicalfacilityid','nationallocalregistry','nationallocalregistryid', 'height', 'weight','organcriteriaid');
$error = false;
$var = $_POST['submit'];
if($var){
foreach($required as $field) {
  if (empty($_POST[$field])) {
    $error = true;
    if ($error) {
    echo $field. ' is empty';
    ?> 
    <html><br></html>
    <?php
    }
}

  else{
    $error = false;

        }

}

 if(!$error){

    $donorid = $_POST['donorid'];
    $personid = $_POST['personid'];
    $dateregistered = $_POST['dateregistered'];
    $ageregistered = $_POST['ageregistered'];
    if(isset($_POST['donorpreregistered'])){
    $donorpreregistered = "1";}
    else{
        $donorpreregistered = "0";
    }
    $medicalfacilityid = $_POST['medicalfacilityid'];
    $nationallocalregistry = $_POST['nationallocalregistry'];
    $nationallocalregistryid = $_POST['nationallocalregistryid'];
    $height = $_POST['height'];
    $weight = $_POST['weight'];
    $bloodtypecode = $_POST['bloodtypec'];
    $organcriteriaid = $_POST['organcriteriaid'];
    if(isset($_POST['livingdonor']))
    {
        $livingdonor = "1";
    }
    else{
        $livingdonor = "0";
    }

    if(isset($_POST['directeddonor'])){
    $directeddonor = "1";}
    else{
        $directeddonor = "0";
    }




    $result = mysql_query($donor_add);
    if (!$result) {
    die('Invalid query: ' . mysql_error()); 
    //echo "Form Submitted Successfully";    

        }
    }

}   



?>
  

查询无效:您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以便在&#39; ,,,,,,,,,,,,)附近使用正确的语法。在第1行

1 个答案:

答案 0 :(得分:0)

您不需要对任何表名或列名使用单引号... mySQL可能会将它们识别为别名,并且无法识别任何执行查询的表。您的INSERT语法也是错误的,您需要将值连接到您的查询字符串,正确的sintax在PHP中连接一个值正在使用&#34;。&#34;例如......&#34; INSERT INTO myTable值(&#34;。$ myValue。&#34;)&#34;或者&#34; INSERT INTO myTable(column1,column2)值(&#34;。$ value1。&#34;,&#34;。$ value2。&#34;)&#34;