主要PHP文件 -
<html>
<head>
<script type="text/javascript" src="MyJS.js"></script>
<body>
<?php
include ("connection.php");
$result = mysqli_query($con, "SELECT * FROM stu_rec");
while($stu = mysqli_fetch_array($result)){
echo "<h3>" . $stu["Name"] . "<h3>";
echo "<h3>" . $stu["FirstName"] . "<h3>";
echo "<h3>" . $stu["SID"] . "<h3>";
echo "<h3>" . $stu["Email"] . "<h3>";
}
?>
<form action="insert.php" method="post">
Name: <input type="text" name="Name">
First Name: <input type="text" name="FirstName">
SID: <input type="int" name="SID">
Email: <input type="text" name="Email">
<input type="submit">
</form>
<select>
<option id="mon" value="1"></option>
<option id="tue" value="2"></option>
<option id="thu" value="3"></option>
<option id="fri" value="4"></option>
</select>
<script>
remaining();
</script>
</body>
</head>
</html>
将信息插入数据库的文件...
<?php
include ("connection.php");
$sql="INSERT INTO table1 (Name, FirstName, SID, Email)
VALUES
('$_POST[Name]','$_POST[FirstName]','$_POST[SID]','$_POST[Email]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
header('Location: MyPHP.php');
mysqli_close($con);
?>
javascript文件 -
function remaining(){
var mon=8;
var tue=5;
var thu=8;
var fri=9;
document.getElementById("mon").innerHTML = 'Monday 15:00 to 17:00 ---- ' + mon + ' seats remaining';
document.getElementById("tue").innerHTML = 'Tuesday 14:00 to 16:00 ---- ' + tue + ' seats remaining';
document.getElementById("thu").innerHTML = 'Thursday 11:00 to 13:00 ---- ' + thu + ' seats remaining';
document.getElementById("fri").innerHTML = 'Friday 10:00 to 12:00 ---- ' + fri + ' seats remaining';
}
我有4个数据库表。从下拉菜单中选择星期一后,它将插入“table1”,从下拉菜单中选择星期二后,它将插入“table2”,依此类推。请告诉我如何做到这一点...我在这方面很新。请高兴我的愚蠢错误!
感谢。
答案 0 :(得分:0)
将菜单放在表单中,以便提交其值:
<form action="insert.php" method="post">
Name: <input type="text" name="Name">
First Name: <input type="text" name="FirstName">
SID: <input type="int" name="SID">
Email: <input type="text" name="Email">
<select name="day">
<option id="mon" value="1"></option>
<option id="tue" value="2"></option>
<option id="thu" value="3"></option>
<option id="fri" value="4"></option>
</select>
<input type="submit">
</form>
然后在PHP中,你可以这样做:
$table = "table" . $_POST['day'];
$sql="INSERT INTO $table (Name, FirstName, SID, Email)
VALUES
('$_POST[Name]','$_POST[FirstName]','$_POST[SID]','$_POST[Email]')";
答案 1 :(得分:0)
为选择添加名称:
<select name="my_select_name">
然后相应地更改插入查询的第一行:
$sql="INSERT INTO table".$_POST['my_select_name']." (Name, FirstName, SID, Email)
但是请注意,在将数据插入数据库之前,不要对从用户检索的数据进行任何过滤,这非常危险并且可以使用错误/ SQL注入