2015-04-16 44 views
-1

所以我是一個新手到php & mysql。每次填寫我創建的表單時,我都會收到此錯誤。當我檢查phpmyadmin以查看錶單中的信息是否已添加到表中時,無法找到它。我在這裏發現了一個類似的問題,通過運行一個查詢來關閉SQL的嚴格模式,解決了他們的問題。我試圖這樣做,並把它添加到表中的條目,但該條目中的所有值分別爲0.這是我的PHP代碼塊:無效的查詢:您的SQL語法有錯誤; (錯誤在插入)

<?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服務器版本對應的手冊,以便在第1行''處使用正確的語法''

+0

In未來總是發佈錯誤消息。並非我們所有人都是心靈讀者。 –

+0

你是否真的需要INSERT語句中的所有單引號? - 只是好奇... –

+0

會做。無效的查詢:您的SQL語法中有錯誤;請檢查第1行的',,,,,,,,,,,,'附近使用正確的語法對應於您的MySQL服務器版本的手冊 – Lauren

回答

0

您不需要使用單引號對於任何表或列名... mySQL可能會將它們識別爲別名,並且不會識別任何表來執行查詢。您的INSERT語法也是錯誤的,您需要將這些值連接到您的查詢字符串,正確的sintax來連接PHP中的值使用「。」。例如...「INSERT INTO myTable values(」。$ myValue。「)」或「INSERT INTO myTable(column1,column2)values(」。$ value1。「,」。$ value2。「)」