选择选项时插入数据库

时间:2013-12-08 14:41:22

标签: php mysql ajax

这是我从主题“讲师”表中选择的选项数据库

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)'< / p>

就像我只在select for选项中调用一个值

非常感谢..

1 个答案:

答案 0 :(得分:0)

您的 INSERT 语句有3列,但您只插入一列的值,这是错误的来源。

即您没有传递credit_hourcapacity

的值