Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Update not working

    This script displays data from the table and immediately below the display are inputs to update 2 fields in the row.

    A couple of things are going on, this is strange! First, the data isn't being updated, and then I get the following:

    With error reporting set to: error_reporting (E_ALL) I had a notice saying id was not defined. I defined id: $id =""; at the top of the script and the part of the script that displays the current info in the row disappears, leaving only the inputs to update the info. Then when I echo:
    Code:
    $result = mysql_query ($query) or die (mysql_error());
    It doesn't print any errors.

    Here's the bulk of the script:
    Code:
    <?php # PHP Script - submit debtors resolution.php
    
    // This displays debtors info.
    $query = "SELECT * FROM debtors WHERE id = '$id' LIMIT 1";
    $result = mysql_query ($query);
    	if ($result) {
    	while ($row = mysql_fetch_array($result, MYSQL_NUM)) {// Display each record.
    echo '<table border="2" width="95%" cellspacing="3" cellpadding="3" align="center">';
      echo '<tr>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2"><b>Business Name:</b></font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';  
      echo '<p><font face="arial" size="2" color="#FF0000"><b>' . $row[2] . '</b></font></p>';  
      echo '</td>';
      echo '<td valign="top" align="left">';  
      echo '<font face="arial" size="2">&nbsp;</font>';
      echo '</td>';
      echo '<td valign="top" align="left">';  
      echo '<font face="arial" size="2">&nbsp;</font>';
      echo '</td>';
      echo '</tr>';
      echo '<tr>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2"><b>Responsible Party:</b></font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2" color="#FF0000"><b>' . $row[3] . '</b></font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2">&nbsp;</font>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2">&nbsp;</font>';
      echo '</td>';
      echo '</tr>';
      echo '<tr>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2"><b>Address:</b></font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2">' . $row[7] . '</font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2"><b>City:</b></font>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2">' . $row[4] . '</font>';
      echo '</td>';
      echo '</tr>';
      echo '<tr>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2"><b>State:</b></font></p>'; 
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2">' . $row[5] . '</font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2"><b>Zip Code:</b></font>'; 
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2">' . $row[6] . '</font>'; 
      echo '</td>';
      echo '</tr>';
      echo '<tr>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2"><b>Amount Owed:</b></font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2">' . $row[8] . '</font></p>'; 
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2"><b>Date of Default:</b></font>'; 
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2">' . $row[10] . '</font>'; 
      echo '</td>';
      echo '</tr>';
      echo '<tr>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2"><b>Resolved?</b></font></p>';   
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<p><font face="arial" size="2">' . $row[11] . '</font></p>';
      echo '</td>';
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2"><b>Date Resolved:</b></font>';
      echo '</td>'; 
      echo '<td valign="top" align="left">';
      echo '<font face="arial" size="2">' . $row[12] . '</font>';
      echo '</td>'; 
      echo '</tr>';
    
    echo '</table>';
    }
    }
    
    
    if (isset($_POST['Submit'])) { // Handle the form.
    
    $query = "SELECT * FROM debtors WHERE id = '$id' LIMIT 1";
    $result = mysql_query ($query) or die (mysql_error());
    
    
    // Validate the purchase date, web site, pixels.
    
    // Check for resolved status
    if (!empty($_POST['resolved'])){
    	$r = escape_data($_POST['resolved']);
    } else {
    	$r = FALSE;
    	echo '<p><font color="red">Please update resolved status!</font></p>';
    }
    
    // Check for date resolved
    if (!empty($_POST['date_resolved'])){
    	$dr = escape_data($_POST['date_resolved']);
    } else {
    	$dr = FALSE;
    	echo '<p><font color="red">Please enter the date this debt was resolved!</font></p>';
    }
    
    if ('$r' && '$dr') {
    	
    	
        $query = "UPDATE debtors SET resolved ='$r', date_resolved = '$dr' WHERE id = '$id' LIMIT 1";		
    	$result = mysql_query ($query) or die (mysql_error()); // Run the query.
    	if ($result) { // If it ran OK.
    			
    				echo '<h3>Your debtor resolution has been posted.</h3>';
    				echo '<br/>';
    				echo '<br/>';
    				echo '<br/>';
    				echo '<br/>';
    				echo '<br/>';
    				echo '<br/>';
    				echo '<br/>';
    				echo '<br/>';
    				exit();				
    				
    			} else { // If it did not run OK.
    				// Send a message to the error log, if desired.
    				echo '<p><font color="red" size="+1">Your debtor information could not be updated due to a system error.</font></p>'; 
    			}		
    			
    		} 
    		
    		mysql_close(); // Close the database connection.
    
    	} // End of the main Submit conditional.
    	
    ?>
    
    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    <input type="hidden" name="MAX_FILE_SIZE" value="524288">
            <fieldset><legend><font size="3" color="#0000CC" face="Arial, Helvetica, sans-serif"><b>Update 
                  your debtor's info here:</b></font></legend> 
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td colspan="5">&nbsp;</td>
                <td colspan="2" rowspan="2">&nbsp;</td>
              </tr>
              <tr> 
                <td colspan="5">&nbsp;</td>
              </tr>
              <tr> 
                <td colspan="2" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><b>Is 
                  this debt resolved?</b></font></td>
                <td width="12%" valign="top">&nbsp;</td>
                <td colspan="2" valign="top"> 
                  <input type="text" name="resolved" maxlength="3">
                </td>
                <td colspan="2" valign="top" rowspan="3">&nbsp;</td>
              </tr>
              <tr> 
                <td colspan="2" valign="top">&nbsp;</td>
                <td width="12%" valign="top">&nbsp;</td>
                <td colspan="2" valign="top">&nbsp;</td>
              </tr>
              <tr> 
                <td colspan="2" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><b>Date 
                  the debt was resolved:<br>
                  </b><font color="#FF0000">(mm/dd/yyyy)</font></font></td>
                <td width="12%" valign="top">&nbsp;</td>
                <td colspan="2" valign="top">
                  <input type="text" name="date_resolved" maxlength="10" size="10">
                </td>
              </tr>
              <tr> 
                <td valign="top" colspan="2">&nbsp;</td>
              </tr>
              <tr align="center"> 
                <td colspan="7"><font face="Arial, Helvetica, sans-serif" size="2"><b><i>Click&quot;Submit&quot;, 
                  to update!<br>
                  </i></b></font></td>
              </tr>
              <tr> 
                <td align="center" colspan="7"> <font face="Arial, Helvetica, sans-serif"> 
                  <input type="submit" name="Submit" value="Submit" />
                  </font></td>
              </tr>
            </table>
      </fieldset> 
    </form><!-- End of Form -->	
    	
    </td>
              <td width="15%">&nbsp;</td>
            </tr>
          </table>
    What could be causing this?

    Thanks

    T

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    it looks to me like the code is relying on register_globals being turned on. If you change $id="" to
    PHP Code:
    $id=$_GET['id']; 
    what happens?

    (for the form-submission bit, you'll probably want $id=$_POST['id'])

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    from what i can see in your code you dont seem to be putting the id in your form or retreiving it in your update code so the query does not know what id it is updating...

    try putting in a hidden field in the form with value of the id and then define the id variable before the update query is processed.

  • #4
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Now, I'm no expert but wouldn't the fact that you have multiple instances "$query" and you call upon "$query" several different times affect what happens to your info?

    What I mean is you have the code below, more than once in a single HTML Document wouldn't it just by default use the first instance of "$query" that it finds within the doc instead of the one closes to the "result".

    Code:
    $query = "Your Query Here";
    If this is infact what the problem is then you would need to replace several of the "$query" with a different name each time. Such as the first one in your doc, call it "$display" instead of "$query" and the change your result statement to something like below...

    Code:
    $result = mysql_query ($display);
    Just my thoughts, but I am positive that if you have the same variable more than once in a document/file it will ignore every instance but the first it comes across. That would be why your update statement was being ignored.
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    Variables get set to the last value/string placed into them, otherwise they would be called "constants" and things like loops and most other code would never work.

    The re-use of the $query variable is not an issue so long as the programmer is not expecting it to have a previous value right after he assigns a new value to it.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the suggestions!

    Here's what happened:

    When I used
    Code:
    $id=$_GET['id'];
    and
    Code:
    $id=$_POST['id'];
    I got undefined index warnings.

    When I put a hidden field in the form and defined $id before the update, it returned the text box where the ID is entered with $id in the text box, still no update.

    I even tried changing the name of the 2nd $query and $result to $query2 and $result2 which made no difference at all.

    Any other suggestions?

    Thanks

    T

  • #7
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    I suggest you print your $query to find out what is wrong? There should be no need for guessing what is wrong. Place immediately after each of the two $query=... statements:
    PHP Code:
    echo $query
    Also, can you confirm whether GJay is right that register_globals are on?

  • #8
    New Coder
    Join Date
    Mar 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, register_globals is on. The error is coming from the second select query:

    Code:
    $query = "SELECT * FROM debtors WHERE id = ' $id ' LIMIT 1";
    The error that is printing is:

    You have an error in your SQL syntax near 'echo SELECT * FROM debtors WHERE id = '' LIMIT 1' at line 1
    This doesn't make sense to me because the first select query is working fine. What could be causing this?

    Thanks

    T

  • #9
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    the word 'echo' shouldn't be there

  • #10
    New Coder
    Join Date
    Mar 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Echo is displaying because I echoed the query.

    Code:
    $query = "SELECT * FROM debtors WHERE id = '$id' LIMIT 1 echo $query";
    Isn't it?

  • #11
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Quote Originally Posted by hightechredneck
    Echo is displaying because I echoed the query.

    Code:

    $query = "SELECT * FROM debtors WHERE id = '$id' LIMIT 1 echo $query";

    Isn't it?
    The echo statement shouldn't be inside the actual query.

    Try this:
    PHP Code:
    $query "SELECT * FROM debtors WHERE id = '$id' LIMIT 1";
    echo 
    $query
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #12
    New Coder
    Join Date
    Mar 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I did that, I got basically the same result without it saying "echo", which still leaves me wondering why the id isn't being passed to this segment of the script?

    Here's what prints:

    SELECT * FROM debtors WHERE id = '' LIMIT 1
    Thanks

    T

  • #13
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    How about echoing $id as well, to see what its value is?

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #14
    New Coder
    Join Date
    Mar 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried that, and nothing printed. Doesn't that just confirm what I already knew? That the id isn't being passed?

  • #15
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Where would it be passed from? I don't see anywhere where the $id variable is actually being defined..

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •