2013-12-08 83 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