Update Doesn't Work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Antonio Bacasno
    New Member
    • Sep 2011
    • 22

    Update Doesn't Work

    My UPDATE script just doesn't work. I just couldn't find what was wrong, every variable is declared. Please find what's wrong with my code. Thanks.

    ***THIS IS THE CODE FROM WHERE THE EDITING WILL TAKE PLACE***
    Code:
    <?php require_once("includes/db_connect.php"); ?>
    <?php require_once("includes/functions.php"); ?>
    <?php
    	
    	$equipid = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipid']))); //EquipID variable
    	
    	$query = "SELECT * FROM tblequipmentmasterlist ";
    	$query .= "WHERE equipid =" . $equipid;
    	$query .= " LIMIT 1";
    	
    	$query_result = mysqli_query($db_cxn, $query);
    
    	while ($row = mysqli_fetch_array ($query_result)) {
    			//Variables to be used to output on the textfields
    				$equipname = $row['equipname'];
    				$manufacturer = $row['manufacturer'];
    				$serno = $row['serno'];
    				$modelno = $row['modelno'];
    				$capacity = $row['capacity'];
    				$curloc = $row['curlocation'];
    				$equipcode = $row['equipcode'];
    				$seqno = $row['seqno'];
    				$adddate = $row['adddate'];
    			 }
    		
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>EST-Equipment Intranet-Calibration-Update Equipment Record</title>
    <meta http-equiv="Content-Language" content="English" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
    <style type="text/css">
    <!--
    .style1 {
    	color: #000000;
    	font-size: medium;
    }
    
    #wrap #content .right .newequipment {
    	font-family: Verdana, Arial, Helvetica, sans-serif;
    	font-size: 14px;
    	font-style: normal;
    	font-weight: lighter;
    	text-transform: capitalize;
    	text-align: left;
    	text-indent: 5px;
    	margin: 10px;
    	padding: 10px;
    	width: 350px;
    }
    -->
    </style>
    
    <?php include("includes/calheader.php"); ?>
    
    <h2>Update Equipment Information Form</h2>
    <form action = "processform_update.php" method = "POST">
    <div class="DataInput">
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname6">
        <tr>
          <td width="200" class="style1">Equipment ID No.</td>
          <td width="507"><input name="equipid" type="text" id="equipid" tabindex="" value="<?php echo $equipid; ?>" size="7" />
         </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname">
        <tr>
          <td width="200" class="style1">Equipment Name</td>
          <td width="507">
            <input name="equipname" type="text" id="equipname" tabindex="1" value="<?php echo $equipname; ?>" size="40" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="708" border="0" cellpadding="0" cellspacing="0" id="Equipname2">
        <tr>
          <td width="200" class="style1">Manufacturer</td>
          <td width="508">
            <input name="manufacturer" type="text" id="manufacturer" tabindex="2" value="<?php echo $manufacturer; ?>" size="40" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="561" border="0" cellpadding="0" cellspacing="0" id="Equipname3">
        <tr>
          <td width="200" class="style1">Serial No. (if any)</td>
          <td width="361">
          <input name="serno" type="text" id="serno" tabindex="3" value="<?php echo $serno; ?>" size="30" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="561" border="0" cellpadding="0" cellspacing="0" id="Equipname4">
        <tr>
          <td width="200" class="style1">Model No. (if any)</td>
          <td width="361">
          <input name="modelno" type="text" id="modelno" tabindex="4" value="<?php echo $modelno; ?>" size="30" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
      <label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname5">
        <tr>
          <td width="200" class="style1">Capacity / Range</td>
          <td width="507">
          <input name="capacity" type="text" id="capacity" tabindex="5" value="<?php echo $capacity; ?>" size="40" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
    	<label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname6">
        <tr>
          <td width="200" class="style1">Current Location</td>
          <td width="507">
          <input name="curlocation" type="text" id="curlocation" value="<?php echo $curloc; ?>" tabindex="6" size="20" />
          <a href='locationcode.php' target='_blank' class='style7'>...Click here for definition...</a> 
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
    	<label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname5">
        <tr>
          <td width="200" class="style1">Equipment Code</td>
          <td width="507">
          <input name="equipcode" type="text" id="equipcode" value="<?php echo $equipcode; ?>" tabindex="7" size="20" />
          <a href='equipmentcode.php' target='_blank' class='style7'>...Click here for definition...</a> 
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <table width="561" border="0" cellpadding="0" cellspacing="0" id="Equipname8">
        <tr>
          <td width="200" class="style1">Sequence Number</td>
          <td width="361">
          <input name="seqno" type="text" id="seqno" tabindex="8" size="7" maxlength="7" value="<?php echo $seqno; ?>" />
          </td>
        </tr>
      </table>
    </div>
    <div class="DataInput">
     <label class="style6"></label>
      <table width="707" border="0" cellpadding="0" cellspacing="0" id="Equipname5">
        <tr>
          <td width="200" class="style1">Date Added</td>
          <td width="507">
          <input name="adddate" type="text" id="adddate" tabindex="9" value="<?php echo $adddate; ?>" size="20" /> 
          <strong>YYYY-MM-DD</strong> </td>
        </tr>
      </table>
    </div>
    <p>&nbsp;</p>
    <div class="DataInput">
    <input type="submit" name="submit" id="submit" value="Submit" />
    </div>
    </form>
    <p><br />
    <br />
      <br />
      <br />
      <br />
      </div>
      
    <?php require("includes/footer.php"); ?>
    ***THIS IS THE UPDATE PROCESSING CODE***

    Code:
    <?php include("includes/db_connect.php"); ?>
    <?php require_once("includes/functions.php"); ?>
    
    <?php
    
    	$equipid = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipid'])));
    	$equipname = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['equipname'])));
    	$manufacturer = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['manufacturer'])));
    	$serno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['serno'])));
    	$modelno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['modelno'])));
    	$capacity = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['capacity'])));
    	$curloc = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['curlocation'])));
    	$equipcode = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipcode'])));
    	$seqno = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['seqno'])));	
    	$addDate = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['adddate'])));
    	
    	
    	$query_update = "UPDATE tblequipmentmasterlist SET ";
    	$query_update .= "equipname = '{$equipname}', ";
    	$query_update .= "manufacturer = '{$manufacturer}', ";
    	$query_update .= "serno = '{$serno}', ";
    	$query_update .= "modelno = '{$modelno}', "; 
    	$query_update .= "capacity = '{$capacity}', "; 
    	$query_update .= "curlocation = '{$curloc}', "; 
    	$query_update .= "equipcode = '{$equipcode}', "; 
    	$query_update .= "seqno = '{$seqno}', "; 
    	$query_update .= "adddate = '{$addDate}' ";
    	$query_update .= "WHERE 'tblequipmentmasterlist'.'equipid' = {$equipid} LIMIT 1";
    					
    					
    	if (mysqli_affected_rows() == 1) {
    		header ("LOCATION : updatedata.php");
    	} else {
    		print_r($_POST);
    		var_dump($_POST);
    		echo "<p>Update failed : <br />". $query_update.  " <br/>Please contact the site administrator.</p>";
    		echo "<p>" . mysqli_error($db_cxn) . "</p>";
    	}
    		
    ?>
    
    <?php 
    	if (isset($db_cxn)) {
    		mysqli_close($db_cxn); 
    	}
    ?>
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If you're trying to qualify a table/field/schema name, I think you have to use ` and not '.

    Comment

    • Antonio Bacasno
      New Member
      • Sep 2011
      • 22

      #3
      I tried revising the code of my PROCESSING page as per your suggestion, and still the same problem occurred. Please kindly check my code. Also, I've attached an image showing the error.

      Code:
      <?php include("includes/db_connect.php"); ?>
      <?php require_once("includes/functions.php"); ?>
      
      <?php
      
      	$equipid = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipid'])));
      	$equipname = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['equipname'])));
      	$manufacturer = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['manufacturer'])));
      	$serno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['serno'])));
      	$modelno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['modelno'])));
      	$capacity = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['capacity'])));
      	$curloc = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['curlocation'])));
      	$equipcode = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipcode'])));
      	$seqno = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['seqno'])));	
      	$addDate = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['adddate'])));
      	
      	
      	$query_update = "UPDATE `tblequipmentmasterlist` SET ";
      	$query_update .= "`equipname` = '{$equipname}', ";
      	$query_update .= "`manufacturer` = '{$manufacturer}', ";
      	$query_update .= "`serno` = '{$serno}', ";
      	$query_update .= "`modelno` = '{$modelno}', "; 
      	$query_update .= "`capacity` = '{$capacity}', "; 
      	$query_update .= "`curlocation` = '{$curloc}', "; 
      	$query_update .= "`equipcode` = '{$equipcode}', "; 
      	$query_update .= "`seqno` = '{$seqno}', "; 
      	$query_update .= "`adddate` = '{$addDate}' ";
      	$query_update .= "WHERE `tblequipmentmasterlist`.`equipid` = {$equipid} LIMIT 1";
      					
      					
      	if (mysqli_affected_rows() == 1) {
      		header ("LOCATION : updatedata.php");
      	} else {
      		print_r($_POST);
      		var_dump($_POST);
      		echo "<p>Update failed : <br />". $query_update.  " <br/>Please contact the site administrator.</p>";
      		echo "<p>" . mysqli_error($db_cxn) . "</p>";
      	}
      		
      ?>
      
      <?php 
      	if (isset($db_cxn)) {
      		mysqli_close($db_cxn); 
      	}
      ?>
      Attached Files

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I can't really see what the error says, it's too small. Have you tried outputting the SQL and running it manually through the DBMS, which I'm assuming is MySQL?

        Comment

        • Antonio Bacasno
          New Member
          • Sep 2011
          • 22

          #5
          If you mean if I tried updating via the MySQL console, YES, I tried that and it works fine. I was able to update the database manually. I'm sorry about the image, I just can't have a larger image due to the forum restrictions (500 x 600) so I have to resize it before posting it here. Please find again the image on this link:





          Also, I made some revision with the mysqli_num_rows function. Please find the code here:

          Code:
          <?php include("includes/db_connect.php"); ?>
          <?php require_once("includes/functions.php"); ?>
          
          <?php
          
          	$equipid = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipid'])));
          	$equipname = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['equipname'])));
          	$manufacturer = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['manufacturer'])));
          	$serno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['serno'])));
          	$modelno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['modelno'])));
          	$capacity = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['capacity'])));
          	$curloc = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['curlocation'])));
          	$equipcode = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipcode'])));
          	$seqno = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['seqno'])));	
          	$addDate = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['adddate'])));
          	
          	
          	$query_update = "UPDATE `tblequipmentmasterlist` SET ";
          	$query_update .= "`equipname` = '{$equipname}', ";
          	$query_update .= "`manufacturer` = '{$manufacturer}', ";
          	$query_update .= "`serno` = '{$serno}', ";
          	$query_update .= "`modelno` = '{$modelno}', "; 
          	$query_update .= "`capacity` = '{$capacity}', "; 
          	$query_update .= "`curlocation` = '{$curloc}', "; 
          	$query_update .= "`equipcode` = '{$equipcode}', "; 
          	$query_update .= "`seqno` = '{$seqno}', "; 
          	$query_update .= "`adddate` = '{$addDate}' ";
          	$query_update .= "WHERE `tblequipmentmasterlist`.`equipid` = {$equipid} LIMIT 1";
          					
          					
          	if (mysqli_affected_rows($db_cxn) == 1) {
          		header ("LOCATION : updatedata.php"); //Record was successfully updated
          	} else {
          		print_r($_POST);
          		var_dump($_POST);
          		echo "<p>Update failed : <br />". $query_update.  " <br/>Please contact the site administrator.</p>";
          		echo "<p>" . mysqli_error($db_cxn) . "</p>";
          	}
          		
          ?>
          
          <?php 
          	if (isset($db_cxn)) {
          		mysqli_close($db_cxn); 
          	}
          ?>

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            When you say that you tried updating via the MySQL console, what does that mean? What SQL string did you use? Did you output the SQL string from PHP and then use that? Or did you manually type it in?

            Comment

            • Antonio Bacasno
              New Member
              • Sep 2011
              • 22

              #7
              I only tried a manual update of my database using the MySQL console to check if the commands are right and working if encoded directly in MySQL. My MySQL version has GUI (I'm using WAMP), so I can also directly check if my webpage did changed my database. Right now, I don't really know what's the problem with my code, the data variables from GETDATA page is being transferred to my PROCESSING page, as the print_r and var_dump shows from the imaged linked, but the data stops there, it doesn't get into my database, whatsoever.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Instead of the manual update, try outputting the string from PHP and then pasting that into MySQL to see if it works.

                Comment

                • Antonio Bacasno
                  New Member
                  • Sep 2011
                  • 22

                  #9
                  You mean the whole UPDATE string from my PHP and then pasting it to the MySQL sql query? Ok, I'll try it, thanks for the advice, hope this helps...

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    Yep, I'm thinking there might be an issue with the string creation. Outputting it and using what the PHP engine uses will either confirm or deny it. And then we can proceed from there.

                    Comment

                    • Antonio Bacasno
                      New Member
                      • Sep 2011
                      • 22

                      #11
                      Please find the linked images. As per your suggestion, I copied the PHP string and then pasted it on the MySQL sql query, and it successfully updated my dbase.

                      ***The data to be be sent as an update***

                      ***Update failed, copied the query string***

                      ***Database doesn't show any update activity at all***

                      ***Pasted the copied string***

                      ***Query was a success!***

                      ***Update was reflected on the main dbase***

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        I can't believe I missed this but you didn't actually run the update query in your code. You create the string but then you don't execute it.

                        Comment

                        • Antonio Bacasno
                          New Member
                          • Sep 2011
                          • 22

                          #13
                          Could you please elaborate?

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            After you build the string, you need to execute it using the mysql_query($qu ery_update) function.

                            Comment

                            • Antonio Bacasno
                              New Member
                              • Sep 2011
                              • 22

                              #15
                              Wow, thanks dude, that surely solves the UPDATE problem. But, I can't make my redirect to work. When a successful update was performed, it should redirect to UPDATEDATA.PHP. What happens is it only opens a blank PROCESSFORM_UPD ATE.PHP page.

                              Code:
                              <?php include("includes/db_connect.php"); ?>
                              <?php require_once("includes/functions.php"); ?>
                              
                              <?php
                              
                              	$equipid = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipid'])));
                              	$equipname = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['equipname'])));
                              	$manufacturer = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['manufacturer'])));
                              	$serno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['serno'])));
                              	$modelno = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['modelno'])));
                              	$capacity = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['capacity'])));
                              	$curloc = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['curlocation'])));
                              	$equipcode = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['equipcode'])));
                              	$seqno = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['seqno'])));	
                              	$addDate = mysqli_real_escape_string($db_cxn, trim(strip_tags($_POST['adddate'])));
                              	
                              	
                              	$query_update = "UPDATE `tblequipmentmasterlist` SET ";
                              	$query_update .= "`equipname` = '{$equipname}', ";
                              	$query_update .= "`manufacturer` = '{$manufacturer}', ";
                              	$query_update .= "`serno` = '{$serno}', ";
                              	$query_update .= "`modelno` = '{$modelno}', "; 
                              	$query_update .= "`capacity` = '{$capacity}', "; 
                              	$query_update .= "`curlocation` = '{$curloc}', "; 
                              	$query_update .= "`equipcode` = '{$equipcode}', "; 
                              	$query_update .= "`seqno` = '{$seqno}', "; 
                              	$query_update .= "`adddate` = '{$addDate}' ";
                              	$query_update .= "WHERE `tblequipmentmasterlist`.`equipid` = {$equipid} LIMIT 1";
                              	
                              	$result = mysqli_query($db_cxn, $query_update);
                              	
                              	if (mysqli_affected_rows($db_cxn) == 1) {
                              		header ("LOCATION : updatedata.php"); //Record was successfully updated
                              	} else {
                              		print_r($_POST);
                              		var_dump($_POST);
                              		echo "<p>Update failed : <br />". $query_update.  " <br/>Please contact the site administrator.</p>";
                              		echo "<p>" . mysqli_error($db_cxn) . "</p>";
                              	}
                              		
                              ?>
                              
                              <?php 
                              	if (isset($db_cxn)) {
                              		mysqli_close($db_cxn); 
                              	}
                              ?>
                              Another thing, how do I make a text field uneditable? I tried DISABLING my EQUIPID text field, but then I always encounter an UNDEFINED INDEX error. I would like my EQUIPID to be uneditable to prevent errors.

                              Comment

                              Working...