【发布时间】:2013-12-25 14:50:57
【问题描述】:
这是我从科目的“讲师”表中选择的选项数据库
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 服务器版本相对应的手册,以了解在第 1 行的“主题”、“信用小时”、“容量”附近使用正确的语法)值 (1)')
就像我在 select for 选项中只调用一个值
非常感谢..
【问题讨论】:
-
您需要进一步研究返回给您的错误。重新阅读错误,然后是 Shankar 的答案。
'subject', 'credit_hour', 'capacity') VALUES (1)'