2013-12-08 85 views
0

這是從「講師」表我的選擇項數據庫從受試者插入到數據庫中,而選擇選擇選項

No. subject credit_hour capacity 
1 (111) AAA  3   20 
2 (222) BBB  4   10 
3 (333) CCC  3   30 

,這是我的選擇,其顯示使用AJAX是testing1.php

<?php 
$conn = mysql_connect('localhost','root','password'); 
mysql_select_db('lecturer'); 

if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
?> 

<html> 
<head> 
<script> 
function showUser(str) 
{ 
if (str==="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState===4 && xmlhttp.status===200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","testing2.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<body> 
    <form action="testing4.php" method="post"> 

<select name="sub" onchange="showUser(this.value)"> 
    <option value="">Select a subject:</option> 
<?php $result= mysql_query('SELECT * FROM subjects'); ?> 
    <?php while($row= mysql_fetch_array($result)) { 
     $list=array($row['subject'],$row['credit_hour'],$row['capacity']); 
     ?> 

    <option value=<?php echo $row['No']?> > 
      <?php echo htmlspecialchars($row['subject']); ?> 

      <?php echo"credit hour"; 
      echo htmlspecialchars($row['credit_hour']); ?> 

     <?php echo"capacity"; 
      echo htmlspecialchars($row['capacity']); ?> 
     </option> 
    <?php } ?> 
    </select> 
     <input type="submit"> 
</form> 
<br> 
<div id="txtHint"><b>subject info will be listed here.</b></div> 

</body> 
</html> 
選項

這是一個被顯示成其在選項選擇testing2.php

<?php 
$q = intval($_GET['q']); 

$con = mysqli_connect('localhost','root','password','lecturer'); 
if (!$con) 
    { 
    die('Could not connect: ' . mysqli_error($con)); 
    } 



mysqli_select_db($con,"lecturer"); 
$sql="SELECT * FROM subjects WHERE No = '".$q."'"; 


$result = mysqli_query($con,$sql); 


echo "<table border='1'> 
<tr> 
<th>Subject</th> 
<th>Credit_hour</th> 
<th>Capacity<th> 

</tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['subject'] . "</td>"; 
    echo "<td>" . $row['credit_hour'] . "</td>"; 
    echo "<td>" . $row['capacity'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 



mysqli_close($con); 
?> 

這是提交按鈕(testing4.php),我想插入我的其他數據庫 這是USER_SUBJECT

<?php 
    if(isset($_POST['sub'])) { 
     // Fetch and clean the <select> value. 
     // The (int) makes sure the value is really a integer. 
     $sub = $_POST['sub']; 

     // Create the INSERT query. 
     $sql = "INSERT INTO user_subject ('subject', 'credit_hour', 'capacity') VALUES ({$sub})"; 

     // Connect to a database and execute the query. 
     $dbLink = mysql_connect('localhost', 'root', 'password') or die(mysql_error()); 
        mysql_select_db('lecturer', $dbLink) or die(mysql_errno()); 

     $result = mysql_query($sql); 

     // Check the results and print the appropriate message. 
     if($result) { 
      echo "Record successfully inserted!"; 
     } 
     else { 
      echo "Record not inserted! (". mysql_error() .")"; 
     } 
    } 
    ?> 

的事情是,我希望當我從選項中的數據,然後點擊提交按鈕它會自動插入數據庫到user_subject但我一直得到這個錯誤 ,我不知道如何解決它

記錄沒有插入! (您的SQL語法有錯誤;請查看與您的MySQL服務器版本相對應的手冊,以在'subject','credit_hour','capacity')第1行的VALUES(1)'附近使用正確的語法)

就像我只是在選擇調用一個值選項

非常感謝你..

+1

你需要學習的錯誤退還給你多一點。重新閱讀錯誤,然後Shankar的答案。 **''subject','credit_hour','capacity')VALUES(1)** – James

回答

0

INSERT聲明有3列,但你只是一列,並且將值是錯誤的根源。

也就是說你不及格值credit_hourcapacity