gpt4 book ai didi

php - 选择选择选项时插入数据库

转载 作者:行者123 更新时间:2023-11-30 00:57:11 25 4
gpt4 key购买 nike

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

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

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

非常感谢..

最佳答案

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

也就是说,您没有传递 credit_hourcapacity 的值

关于php - 选择选择选项时插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com