这是我的register.php(表单):
<table width="350" border="0">
<center><tr>
<td><form action="newregister.php" method="post">
Nama</td>
<td> : </td>
<td></td>
<td width="200"><center><input name="nama_pelajar" type="text"></center></td>
</tr>
<tr>
<td>No Kad Pengenalan</td>
<td> : </td>
<td></td>
<td><center><input name="ic_pelajar" type="text"></center></td></tr>
<tr>
<td>ID Pelajar</td>
<td> : </td>
<td></td>
<td><center><input name="id_pelajar" type="text"></td>
</tr>
<tr>
<td><br>Sesi</td>
<td> : </td>
<td></td>
<td><center><select name="sesi">
<option><center>-Sila pilih-</option>
<option value="Jun 14">Jun 2014</option>
<option value="Dis 14">Dis 2014</option>
<option value="Jun 15">Jun 2015</option>
<option value="Dis 15">Dis 2015</option>
<option value="Jun 16">Jun 2016</option>
<option value="Dis 16">Dis 2016</option>
<option value="Jun 17">Jun 2017</option>
<option value="Dis 17">Dis 2017</option>
</select>
</td>
</tr>
<tr>
<td><br>Kursus</td>
<td> : </td>
<td></td>
<td><center><select name="kursus">
<option>-Sila pilih-</option>
<option value="Senibina">Kursus Lukisan Seni Bina</option>
<option value="Elektrik">Kursus Pemasangan Elektrik</option>
<option value="Fesyen">Kursus Fesyen & Pakaian</option>
<option value="Makanan">Kursus Pemprosesan Makanan</option>
</select></td>
</tr>
<tr>
<td><br>No Telefon</td>
<td> : </td>
<td></td>
<td><center><input name="no_tel" type="text"></td>
</tr>
<tr>
<td><br>Email</td>
<td> : </td>
<td></td>
<td><center><input name="email" type="text"></td>
</tr>
<tr>
<td><center><input name="" type="submit" value="Simpan">
<input name="" type="reset" value="Set Semula">
</center>
</form>
</td>
</tr>
</table>
这是我的php代码(newregister.php):
<?php
require_once "conn.php";
$conn = connect();
$db = connectdb();
mysql_select_db($db,$conn) or die (mysql_error() . "\n");
$query_usr = "select * from register_jun14";
$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$row_usr=mysql_fetch_assoc($usr);
$nama_pelajar=$_REQUEST["nama_pelajar"];
$ic_pelajar=$_REQUEST["ic_pelajar"];
$id_pelajar=$_REQUEST["id_pelajar"];
$sesi=$_REQUEST["sesi"];
$kursus=$_REQUEST["kursus"];
$no_tel=$_REQUEST["no_tel"];
$email=$_REQUEST["email"];
$query = "INSERT INTO register_jun14(nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysql_query($query);
echo "<script languange = 'Javascript'>
alert('Pendaftaran berjaya!');
location.href = 'register.php';</script>";
?>
我的问题是:
如果数据库中有“Jun14”、“Dis14”、“Jun15”、“Dis15”等表,如何将选中的“sesi”(下拉菜单)插入到新表中?
有人吗?
最佳答案
我假设您想根据这个框中的下拉选择选择表(意味着您有多个表jun14、dis14、jun15等等)
<td><center><select name="sesi">
<option><center>-Sila pilih-</option>
<option value="Jun 14">Jun 2014</option>
<option value="Dis 14">Dis 2014</option>
<option value="Jun 15">Jun 2015</option>
<option value="Dis 15">Dis 2015</option>
<option value="Jun 16">Jun 2016</option>
<option value="Dis 16">Dis 2016</option>
<option value="Jun 17">Jun 2017</option>
<option value="Dis 17">Dis 2017</option>
</select>
</td>
首先你需要像这样改变上面的部分
//you should remove the space and add underscore in order for your query to work
//for the if statement
<td><center><select name="sesi">
<option><center>-Sila pilih-</option>
<option value="jun_14">Jun 2014</option>
<option value="dec_14">Dis 2014</option>
<option value="Jun_15">Jun 2015</option>
<option value="Dis_15">Dis 2015</option>
<option value="Jun_16">Jun 2016</option>
<option value="Dis_16">Dis 2016</option>
<option value="Jun_17">Jun 2017</option>
<option value="Dis_17">Dis 2017</option>
</select>
</td>
我删除了值中的空格并添加了下划线,如6月14日变成6月14日,12月14日变成12月14日等等,
现在您的新注册php应该如下所示
<?php
//i remove your this line assuming these lines you are connecting with your database these lines i removed
//require_once "conn.php";
//$conn = connect();
// $db = connectdb();
//mysql_select_db($db,$conn) or die (mysql_error() . "\n");
//and i added this line instead in this line i am also connecting
//with database
$conn= mysqli_connect('localhost', 'root', 'yourpassword', 'yourdatabasename')
or die(mysqli_error($con));
$query_usr = "select * from register_jun14";
//$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$usr = mysqli_query($conn,$query_usr) or die(mysqli_error()."\n".$query_usr);
$row_usr=mysqli_fetch_assoc($usr);
$nama_pelajar=$_REQUEST["nama_pelajar"];
$ic_pelajar=$_REQUEST["ic_pelajar"];
$id_pelajar=$_REQUEST["id_pelajar"];
$sesi=$_REQUEST["sesi"];
$kursus=$_REQUEST["kursus"];
$no_tel=$_REQUEST["no_tel"];
$email=$_REQUEST["email"];
if($sesi=="jun_14")
{
//assuming you have Jun14 table in your database with same field as your
//original insert query in the question
$query = "INSERT INTO Jun14(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
}
if($sesi=="dec_14")
{
//assuming you have Dis14 table in your database with same field as your
//original insert query in the question
$query = "INSERT INTO Dis14(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
}
if($sesi=="jun_15")
{
//assuming you have Jun15 table in your database with same field as your
//original insert query in the question
$query = "INSERT INTO Jun15(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
}
//i am leaving this your query here i dont know why you need it, may be you
//need it
$query = "INSERT INTO register_jun14 ( nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
您可以根据下拉菜单中的值进一步编写if语句
为了实现下拉菜单的目标,可以添加任意数量的if条件,为什么要运行select query我不知道。
关于php - 如何从下拉菜单中选择的数据插入数据库中的多个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25597582/