我有一个包含下拉列表和一些textfiedl的网页,我想要做的是从下拉列表中查看讲师姓名,如下图所示
并存储数据库表中其余表单的数据,其中讲义名称与表名相同。 我的php代码如下
<?php
define('DB_NAME','mydb');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST','localhost');
$connect = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$connect){
die('Could not connect:'.mysql_error());
}
$db_selected=mysql_select_db(DB_NAME,$connect);
if(!$db_selected){
die('Can\'t use'.DB_NAME.':'.mysql_error());
}
$dim = $_POST['dim'];
$lag = $_POST['lag'];
$stam = $_POST['stam'];
$keh = $_POST['keh'];
$chry = $_POST['chry'];
if(HERE I WANT TO CHECK THAT IF THE LECTURER NAME IS EQUAL TO DIMOPOULOS THEN DO THE BELOW)
{
$value1=$_POST['a1'];
$value2=$_POST['a2'];
$value3=$_POST['a3'];
$value4=$_POST['l1'];
$value5=$_POST['l2'];
$value6=$_POST['r1'];
$value7=$_POST['r2'];
$value8=$_POST['u1'];
$value9=$_POST['u2'];
$value10=$_POST['u3'];
$value11=$_POST['unit_name'];
$value12=$_POST['year'];
$sql="INSERT INTO dimopoulos(a1,a2,a3,l1,l2,r1,r2,u1,u2,u3,unit_name,year) VALUES('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
if(!mysql_query($sql)){
die('Error:'.mysql_error());
}
}
elseif(HERE I WANT TO CHECK THAT IF THE LECTURER IS EQUAL TO LAGKAS THEN DO THE BELOW)
{
$value1=$_POST['a1'];
$value2=$_POST['a2'];
$value3=$_POST['a3'];
$value4=$_POST['l1'];
$value5=$_POST['l2'];
$value6=$_POST['r1'];
$value7=$_POST['r2'];
$value8=$_POST['u1'];
$value9=$_POST['u2'];
$value10=$_POST['u3'];
$value11=$_POST['unit_name'];
$value12=$_POST['year'];
$sql="INSERT INTO lagkas(a1,a2,a3,l1,l2,r1,r2,u1,u2,u3,unit_name,year) VALUES('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
if(!mysql_query($sql)){
die('Error:'.mysql_error());
}
}
elseif(HERE I WANT TO CHECK THAT IF THE LECTURER IS EQUAL TO STAMATOPOULOU THEN DO THE BELOW)
{
$value1=$_POST['a1'];
$value2=$_POST['a2'];
$value3=$_POST['a3'];
$value4=$_POST['l1'];
$value5=$_POST['l2'];
$value6=$_POST['r1'];
$value7=$_POST['r2'];
$value8=$_POST['u1'];
$value9=$_POST['u2'];
$value10=$_POST['u3'];
$value11=$_POST['unit_name'];
$value12=$_POST['year'];
$sql="INSERT INTO stamatopoulou(a1,a2,a3,l1,l2,r1,r2,u1,u2,u3,unit_name,year) VALUES('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
if(!mysql_query($sql)){
die('Error:'.mysql_error());
}
}
elseif(HERE I WANT TO CHECK THAT IF THE LECTURER IS EQUAL TO KEHAGIAS THEN DO THE BELOW)
{
$value1=$_POST['a1'];
$value2=$_POST['a2'];
$value3=$_POST['a3'];
$value4=$_POST['l1'];
$value5=$_POST['l2'];
$value6=$_POST['r1'];
$value7=$_POST['r2'];
$value8=$_POST['u1'];
$value9=$_POST['u2'];
$value10=$_POST['u3'];
$value11=$_POST['unit_name'];
$value12=$_POST['year'];
$sql="INSERT INTO kehagias(a1,a2,a3,l1,l2,r1,r2,u1,u2,u3,unit_name,year) VALUES('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
if(!mysql_query($sql)){
die('Error:'.mysql_error());
}
}
elseif(HERE I WANT TO CHECK THAT IF THE LECTURER IS EQUAL TO CHRYSOCHOOU THEN DO THE BELOW)
{
$value1=$_POST['a1'];
$value2=$_POST['a2'];
$value3=$_POST['a3'];
$value4=$_POST['l1'];
$value5=$_POST['l2'];
$value6=$_POST['r1'];
$value7=$_POST['r2'];
$value8=$_POST['u1'];
$value9=$_POST['u2'];
$value10=$_POST['u3'];
$value11=$_POST['unit_name'];
$value12=$_POST['year'];
$sql="INSERT INTO chrysochoou(a1,a2,a3,l1,l2,r1,r2,u1,u2,u3,unit_name,year) VALUES('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
if(!mysql_query($sql)){
die('Error:'.mysql_error());
}
}
mysql_close();
?>
我的HTML代码就是这个......
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
<script type="text/javascript">
function resetForm(){
// clearing selects
var selects = document.getElementsByTagName('select');
for (var i = 0; i<selects.length; i++)
selects[i].selectedIndex = 0;
return false;
}
window.load(resetForm());
</script>
</head>
<body>
<div id="nav">
<div id="nav_wrapper">
<ul>
<li><a href="#">Principal</a>
<ul>
<li><a href="../../statistics/principalLecturerForm.php">Lecturer Scores</a></li>
<li><a href="../../statistics/principalServicesForm.php">Services Scores</a></li>
<li><a href="../../statistics/principalLecturerYears.php">Lecturer Over the Years</a></li>
</ul>
</li>
<li><a href="#">Academic Director</a>
<ul>
<li><a href="../../statistics/ac_directorLecturerForm.php">Lecturer Scores</a></li>
<li><a href="../../statistics/ac_directorServicesForm.php">Services Scores</a></li>
<li><a href="../../statistics/ac_directorLecturerYears.php">Lecturer Over the Years</a></li>
</ul>
</li>
<li><a href="#">Lecturer</a>
<ul>
<li><a href="../../statistics/lecturerForm.php">Lecturer</a></li>
<li><a href="../../statistics/lecLecturerYears.php">Lecturer Over the Years</a></li>
</ul>
</li>
<li><a href="#">Admin</a>
<ul>
<li><a href="../../statistics/adminFormLecturer.php">Save Lecturer Scores</a></li>
<li><a href="../../statistics/adminFormServices.php">Save Services Scores</a></li>
<li><a href="../../statistics/upload.php">Update Lecturers</a></li>
</ul>
</li>
<li><a href="../../statistics/logout.php">Logout</a></li>
</ul>
</div>
</div>
<br />
<br />
<br />
<div id="lecform">
<form action="savelagkasscores.php" method="POST">
<b>Lecturers:<b/>
<select name="lecturer">
<option value="Choose">Please select..</option>
<option id="dim" value="dimopoulos">Dimopoulos</option>
<option id="lag" value="lagkas">Lagkas</option>
<option id="stam" value="stamatopoulou">Stamatopoulou</option>
<option id="keh" value="kehagias">Kehagias</option>
<option id="chry" value="chrysochoou">Chrysochoou</option></select><br/><br/>
<br />
A1: <input type="text" name="a1" size="5"> <br /><br/>
A2: <input type="text" name="a2" size="5"> <br /><br/>
A3: <input type="text" name="a3" size="5"> <br /><br/>
L1: <input type="text" name="l1" size="5"> <br /><br/>
L2: <input type="text" name="l2" size="5"> <br /><br/>
R1: <input type="text" name="r1" size="5"> <br /><br/>
R2: <input type="text" name="r2" size="5"> <br /><br/>
U1: <input type="text" name="u1" size="5"> <br /><br/>
U2: <input type="text" name="u2" size="5"> <br /><br/>
U3: <input type="text" name="u3" size="5"> <br /><br/>
Unit Name:<input type="text" name="unit_name" size="20"> <br /><br/>
Year:<input type="text" name="year" size="10"> <br /><br/>
<br/>
<input type="submit" value="submit" name="Save">
<input type="reset" name="reset" value="Clear">
</form>
</div>
<br />
<br />
<div id="footer">
Copyright e-Statistics 2015
</div>
</body>
</html>
我无法做的是为php代码中的每个if找到合适的if语句,以便将数据保存在数据库的正确表中。
答案 0 :(得分:1)
无需使用IF ELSE您可以简单地编写动态插入语句,这将是一个动态变量,是的,您必须在表单提交期间将相关的TABLE_NAME作为变量从UI传递。
$MY_TABLE=$_POST['TABLE_NAME'];
$sql="INSERT INTO {$MY_TABLE} (a1,a2,a3,l1,l2,r1,r2,u1,u2,u3,unit_name,year) VALUES('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
答案 1 :(得分:-1)
您可以直接通过名称进行检查。
if ($dim == 'dimopoulos') {
...
} elseif ($lag == 'lagkas') {
...
} // same way try for all