emekamba
emekamba

Reputation: 188

Having issues with multiple forms in one page in php

I issue i am having is that, I created two forms, it is A PROFILE PAGE one is for uploading user image to the database, while the other is for updating user basic information, like name, email etc. The issue i am having is that only one of the form is working, the other form stops working. here is my code.

 <?php if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] ==     "pic_form") &&(($_POST['check'])=== 1)) {
$id=$_POST['monitorimg'];
move_uploaded_file($_FILES['pic']['tmp_name'],"imageupload/".$id);
   $updateSQL = sprintf("UPDATE administrator SET pix=%s WHERE adminid=%s",
                  GetSQLValueString($_POST['monitorimg'], "text"),
                   GetSQLValueString($_POST['ids'], "int"));

    mysqli_select_db($myconn,$database_myconn);
   $Result1 = mysqli_query($myconn,$updateSQL) or    die(mysqli_connect_error());
} 
  ?>      


    <div class="box box-primary">
                    <div class="box-body box-profile">


           <img class="profile-user-img img-responsive img-circle"    width="68" height="68" src="imageupload/<?php echo $row_rslinks['adminid']; ?>".""                     alt="profile picture">
              <form action="<?php echo $editFormAction; ?>" name="pic_form" enctype="multipart/form-data" method="POST">
                   <input type="file" name="pic" class="btn-facebook" required>
                   <input type="submit" class="btn-dropbox" name="sub" id="sub" value="Upload">
                   <input type="hidden" name="ids" id="ids" value="<?php echo $row_rslinks['adminid'];?>">
                   <input type="hidden" name="monitorimg" id="monitorimg" value="<?php echo $row_rslinks['adminid'];?>">
                   <input type="hidden" name="MM_update" value="pic_form">
                  <input type="hidden" name="check" id="check" value="1">
              </form>

                        <h3 class="profile-username text-center"></h3>

                        <p class="text-muted text-center"></p>


  <!-----------This is the second Table code--------------> 


 <?php  if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") &&(($_POST['check'])=== 1)) {
  $updateSQL = sprintf("UPDATE administrator SET password=%s, adminname=%s, email_id=%s, contactno=%s WHERE adminid=%s",
                   GetSQLValueString($_POST['pwd'], "text"),
                   GetSQLValueString($_POST['inputName'], "text"),
                   GetSQLValueString($_POST['inputEmail'], "text"),
                   GetSQLValueString($_POST['num'], "text"),
                   GetSQLValueString($_POST['stuid'], "int"));

   mysql_select_db($database_myconn, $myconn);
   $Result1 = mysql_query($updateSQL, $myconn) or die(mysql_error());
    }

  ?>
    <div class="tab-content">
                    <div class="active tab-pane" id="settings">
                    <form action=<?php echo $editFormAction; ?>"" name=form11" method=POSTT" enctype="application/x-www-form-urlencoded" class="form-horizontal" id="form1">
                    <div class="form-group" id="stuid">
                      <label for="stuid" class="col-sm-2 control-label">User ID</label>
                      <div class="col-sm-10">
                        <input name="stuid" type="text" class="form-control"  id="stuid"
                                               value="<?php echo $row_rslinks['adminid']; ?>" readonly>
                      </div>
                    </div>
                    <div class="form-group" id="inputeName">
                      <label for="inputName" class="col-sm-2 control-label">Name</label>
                      <div class="col-sm-10">
                        <input type="text" class="form-control" name="inputName" id="inputName"
                                               value="<?php echo $row_rslinks['adminname']; ?>">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="inputEmail" class="col-sm-2 control-label" >Email</label>
                      <div class="col-sm-10">
                        <input type="email" class="form-control" name="inputEmail" id="inputEmail" 
                                               value="<?php echo $row_rslinks['email_id']; ?>">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="pwd" class="col-sm-2 control-label">Password</label>
                      <div class="col-sm-10">
                        <input type="password" class="form-control" id="pwd" name="pwd" value="">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="rpwd" class="col-sm-2 control-label">Re-Password</label>
                      <div class="col-sm-10">
                        <input type="password" class="form-control" id="rpwd" name="rpwd" value="">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="num" class="col-sm-2 control-label">Mobile</label>
                      <div class="col-sm-10">
                        <input type="text" class="form-control" id="num" name="num" value="<?php echo $row_rslinks['contactno']; ?>">
                      </div>
                    </div><input type="hidden" name="check" id="check" value="2">

                    <div class="form-group">
                      <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-primary">Save</button>
                      </div>
                    </div>

Sorry guys for my long code....will appreciate your contribution. the second Table is not updating to my database..when the update button is pressed nothing happens. no error message is shown.

Upvotes: 1

Views: 96

Answers (1)

kojow7
kojow7

Reputation: 11384

The problem and solution:

The main issue I see here is that your second form uses method="POSTT". Of course, there is no such method as POSTT so it will default to using the GET method instead. Remove the extra 'T' and it should fix the issue.

How to debug step by step:

I also recommend applying some common debugging techniques to trace through any future errors you may encounter.

Step 1: Check your PHP error logs and make sure they are reporting errors.

Step 2: Look at your resulting source code in the browser and run it through a validator such as https://validator.w3.org/

Step 3: If no errors were encountered above use lots of echo statements at various places throughout your program or at least in the problem areas. For example, if you had placed an echo statement such as:

echo "Second if block"; 

(I often go a bit overboard placing echo statements in my program just in case I am overlooking something. If this is the case, I'll usually just use number such as ...echo 7;...echo 8..., etc. before, after, and inside of each block of code)

inside of your second if block you would have noticed that it did not run. That would have clued you in to the fact that your if statement containing your $_POST variables evaluated to false. As you know it should have evaluated to true you know this is the source of your problem. You could then display the contents of your form variables as follows:

echo "<pre>";
echo "GET:";
print_r($_GET);
echo "POST:";
print_r($_POST);
echo "</pre>";

This would have shown you that the data is in your $_GET variable rather than your $_POST variable as you had expected.

Step 4: If your error is not detected in any of the previous steps, your last step is to verify that your SQL statement is correct. Try running it outside of PHP directly in your SQL environment and see if you get any errors. Also, set your PHP script to display SQL errors.

Before your site goes live make sure that any errors get sent only to the log file and not to the browser.

Upvotes: 3

Related Questions