新来的,我有4个提交按钮如下:主页,注销,为用户插入记录并插入建筑记录。但是,如果我按下四个中的任何一个,它将在添加用户和添加建筑物中插入一行。我希望有更多经验的人比我能帮助,并告诉我如何让我的按钮独立工作。另外,我可能会有一些不良习惯,请查看它们或共同点。
<?php
$connect=mysql_connect("localhost","root","");
mysql_select_db("crud",$connect);
if(isset($_POST["insert_2"])) {
if($_POST["insert_2"]=="yes_2"){
$Building=$_POST["Building"];
$Building = Str_replace(" ","_",ltrim(rtrim($Building)) );
$query2="insert into Facility(Building) values('$Building')";
if(mysql_query($query2))
echo "<center>Record Inserted!</center><br>";
}
}
if(isset($_POST["update2"])){
if($_POST["update2"]=="yes2"){
$Building=$_POST["Building"];
$Building = Str_replace(" ","_",ltrim(rtrim($Building)) );
$query2="update Facility set Building='$Building' where id2=".$_POST['id2'];
if(mysql_query($query2))
echo "<center>Record Updated</center><br>";
}
}
if(isset($_GET['operation2'])){
if($_GET['operation2']=="delete"){
$query2="delete from Facility where id2=".$_GET['id2'];
if(mysql_query($query2))
echo "<center>Record Deleted!</center><br>";
}
}
//======================================================
if(isset($_POST["insert"])){
if($_POST["insert"]=="yes"){
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$username=$_POST["username"];
$password=$_POST["password"];
$usertype=$_POST["usertype"];
$query="insert into user(firstname, lastname, username, password,usertype) values( '$firstname', '$lastname','$username', MD5('$password'),'$usertype')";
if(mysql_query($query))
echo "<center>Record Inserted!</center><br>";
}
}
if(isset($_POST["update"])){
if($_POST["update"]=="yes"){
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$username=$_POST["username"];
$password=$_POST["password"];
$usertype=$_POST["usertype"];
$query="update user set firstname='$firstname' , lastname='$lastname' , username='$username' , password=MD5('$password'), usertype='$usertype' where id=".$_POST['id'];
if(mysql_query($query))
echo "<center>Record Updated</center><br>";
}
}
if(isset($_GET['operation'])){
if($_GET['operation']=="delete"){
$query="delete from user where id=".$_GET['id'];
if(mysql_query($query))
echo "<center>Record Deleted!</center><br>";
}
}
//------------------------------------------------------------------------------------------------
//if(isset($_POST['button1'])){
//}
//---------------------------------------------------------------------------------------------------------------
//if(isset($_POST['button2'])){
//}
//----------------------------------------------------------------------
?>
// -------------------删除了代码------------------------ ----------------
<table align="center" border="0">
<tbody><tr>
<td> <font color="#000000">First Name:</font></td>
<td><input name="firstname" type="text"></td>
</tr>
<tr>
<td><font color="#000000">Last Name:</font></td>
<td><input name="lastname" type="text"></td>
</tr>
<tr>
<td><font color="#000000">User Name:</font></td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td><font color="#000000">Password:</font></td>
<td><input name="password" type="password"></td>
</tr>
<tr>
</tr><tr>
<td><font color="#000000">User Type:</font></td>
<td><input name="usertype" type="usertype"></td>
<td><font color="#000000">(1 for User or 2 for User Admin)</font></td>
</tr>
<tr>
<td> </td>
<td align="right">
<input name="insert" value="yes" type="hidden">
<input value="Insert Record" type="submit">
</td>
</tr>
</tbody></table>
<?php if(isset($_GET['operation'])){
if($_GET['operation']=="edit"){
?>
<form method="post" action="ADPage.php">
<table align="center" border="0">
<tbody>
<tr>
<td> <font color="#000000">First Name:</font></td>
<td><input name="firstname" value="<?php echo $_GET['firstname']; ?>" type="text"></td>
</tr>
<tr>
</tr>
<tr>
<td><font color="#000000">Last Name:</font></td>
<td><input name="lastname" value="<?php echo $_GET['lastname']; ?>" type="text"></td>
</tr>
<tr>
</tr><tr>
<td><font color="#000000">User Name:</font></td>
<td><input name="username" value="<?php echo $_GET['username']; ?>" type="text"></td>
</tr>
<tr>
<td><font color="#000000">Password:</font></td>
<td><input name="password" value="<?php echo $_GET['password']; ?>" type="text"></td>
</tr>
<tr>
<td><font color="#000000">User Type:</font></td>
<td><input name="usertype" value="<?php echo $_GET['usertype']; ?>" type="text"></td>
<td><font color="#000000">(1 for User or 2 for User Admin)</font></td>
</tr>
<tr>
<td> </td>
<td align="right">
<input name="id" value="<?php echo $_GET['id'] ?>" type="hidden">
<input name="update" value="yes" type="hidden">
<input value="update Record" type="submit">
</td>
</tr>
</tbody></table>
</form>
<?php }}
?>
<?php $query="select * from user";
$result=mysql_query($query);
if(mysql_num_rows($result)>0){
echo "<table align='center' border='1'>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>firstname</th>";
echo "<th>lastname</th>";
echo "<th>Username</th>";
echo "<th>Password</th>";
echo "<th>usertype</th>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['usertype']."</td>";
echo "<td><a href='ADPage.php?operation=edit&id=".$row['id'] ."&firstname=".$row['firstname']."&lastname=".$row['lastname']."&username=".$row['username']."&password=".$row['password']."&usertype=".$row['usertype']."'>edit</a></td>";
echo "<td><a href='ADPage.php?operation=delete&id=".$row['id']."'>delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "<center>No Records Found!</center>";
}
?>
</td></tr></tbody></table><br><table style="width: 1157px; height: 507px; text-align: left; margin-left: auto; margin-right: auto;" border="4" cellpadding="2" cellspacing="2"><caption><big><font style="font-weight: bold;" size="+2"><big><span style="color: white;">Add and Remove Buildings</span></big></font></big></caption><tbody><tr><th style="white-space: nowrap; background-color: white;">
<form method="post" action="ADPage.php">
<table align="center" border="0">
<tr>
<td>Building Name:</td>
<td><input type="text" name="Building" /></td>
</tr>
<td> </td>
<td align="right">
<input type="hidden" name="insert_2" value="yes_2" />
<input type="submit" value="Insert Record_2"/>
</td>
</tr>
</table>
</form>
<?php
if(isset($_GET['operation2'])){
if($_GET['operation2']=="edit"){
?>
<form method="post" action="ADPage.php">
<table align="center" border="0">
<tr>
<td>Building Name:</td>
<td><input type="text" name="Building" value="<?php echo $_GET ['Building']; ?>" /></td>
</tr>
<tr>
<td> </td>
<td align="right">
<input type="hidden" name="id2" value="<?php echo $_GET['id2'] ?>" />
<input type="hidden" name="update2" value="yes2" />
<input type="submit" value="update Record"/>
</td>
</tr>
</table>
</form>
<?php
}}
?>
<?php
$query2="select * from Facility";
$result2=mysql_query($query2);
if(mysql_num_rows($result2)>0){
echo "<table align='center' border='1'>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Building Name</th>";
echo "</tr>";
while($row2=mysql_fetch_array($result2)){
echo "<tr>";
echo "<td>".$row2['id2']."</td>";
echo "<td>".$row2['Building']."</td>";
echo "<td><a href='ADPage.php?operation2=edit&id2=".$row2['id2'] ."&Building=".$row2['Building']."'>edit</a></td>";
echo "<td><a href='ADPage.php?operation2=delete&id2=".$row2['id2']."'>delete</a></td>";
echo "</tr>";
}
echo "</table>";
} else{
echo "<center>No Records Found!</center>";
} ;
?>
</th></tr></tbody></table></td>
</tr>
</tbody>
</table>
</center>
</form></td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>
</center>
</body></html>
答案 0 :(得分:1)
这样做:
<form method="POST">
<input name="button1" type="submit" value="Button 1 Label"/>
<input name="button2" type="submit" value="Button 2 Label"/>
<input name="button3" type="submit" value="Button 3 Label"/>
<input name="button4" type="submit" value="Button 4 Label"/>
</form>
然后:
// Do we have $_POST data?
if(!empty($_POST)){
var_dump($_POST); // to debug
if(!empty($_POST['button1'])){
// user clicked button 1
}elseif(!empty($_POST['button2'])){
// user clicked button 2
}elseif(!empty($_POST['button3'])){
// user clicked button 3
}elseif(!empty($_POST['button4'])){
// user clicked button 4
}
}
我认为你可以弄清楚如何将它应用到你的代码中。提交按钮由其名称标识。如果'name'
中存在$_POST
,则表示您已点击该按钮。独立于标签(值)。
知道点击了哪个submit
按钮,使用$_POST
中的数据执行您需要的操作。
PS :您似乎用来跟踪点击按钮的那些隐藏字段,它们不能像那样工作。
PPS :显然,我没有仔细阅读该代码。它是可怕的!我猜对了你的问题。可能是错的......
答案 1 :(得分:0)
回答你的第二个问题..你不需要这样的代码:
$firstname; // Does nothing
$lastname; // Does nothing
$username; // Does nothing
$password; // Does nothing
$usertype; // Does nothing
$Building; // Does nothing
$connect; // Does nothing
global $firstname; // Not needed
global $lastname; // Not needed
global $username; // Not needed
global $usertype; // Not needed
global $row2; // Not needed
global $row; // Not needed
global $result2; // Not needed
global $result; // Not needed
如果你想初始化变量,你应该这样做:
$firstname = "";
答案 2 :(得分:0)
在您的代码中
<input type="hidden" name="insert_2" value="yes_2" />
你的php代码说:
if(isset($_POST["insert_2"])) {
if($_POST["insert_2"]=="yes_2"){
$Building=$_POST["Building"];
$Building = Str_replace(" ","_",ltrim(rtrim($Building)) );
$query2="insert into Facility(Building) values('$Building')";
if(mysql_query($query2))
echo "<center>Record Inserted!</center><br>";
}
}
隐藏字段始终设置为“yes_2”,无论如何。这就是你的脚本添加它们的原因。
还要记住
if(isset($_POST["update2"]))
即使值为空,也会为真,请尝试使用
if(!empty($_POST["update2"]))
由于明显的原因,我没有查看整个代码。这有帮助吗?