简体   繁体   中英

PHP submit buttons will not work independently

New comer, I have 4 submit buttons as follow: home, log out, insert records for user and insert records for building. But if I press any of the four, it will insert a row in both add user and add building. I wish somebody with more experience than I can help and show me how to have my buttons work independently. Also I may have some bad habits please over look them or common.

 <?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'])){ 

//} 
//----------------------------------------------------------------------    
?>

//-------------------Code removed----------------------------------------

<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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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> 

Do this:

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

and then:

// 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
     }
}

I think you can figure out how to apply this to your code. The submit buttons are identified by their names. If the 'name' is present in $_POST , you know that button was clicked. Independent of label (value) .

Knowing which submit button was clicked, do what you need with the data in $_POST .

PS : Those hidden fields you seem to use to track the clicked button, they don't work like that.

PPS : Obviously, I didn't read that code too carefully. It's terrible! I just guessed your problem. Might be wrong...

Answer to your second question.. You do not need code like this:

$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 

if you want to initialize your variables you shoud just do it like:

$firstname = "";

in your code you have

<input type="hidden" name="insert_2" value="yes_2" />

and your php code says :

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>";
  }
}

the hidden fields are always set at "yes_2" regardless. that's why your script is adding them .

Also keep in mind that

if(isset($_POST["update2"]))

will be true even if the value is nothing, try using

if(!empty($_POST["update2"]))

I didn't look through the whole code for obvious reasons lol. Does this help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM