Why wont this update?

Discussion in 'PHP' started by nickjason, Aug 9, 2009.

  1. #1
    I dont understand why this code is not updating the database. When I click submit all I get back is "Array()" at the top of the unchanged form.

     
    nickjason, Aug 9, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    You are getting array() at the top of the form when submitted because of this line:

    print_r($errors);

    Here you are outputting the contents of the array $errors to the screen, and as it would seem there are no errors when you are submitting the form it is just ouputting as a blank array.

    To get down to why the database is not updating, just do the following to debug what is going on when you submit the form. To do this we just output simple messages to the screen, for different circusmstances.

    Change:

    $result = mysql_query($query, $connect);
    PHP:
    to:

    $result = mysql_query($query, $connect) or die(mysql_error());
    PHP:
    This will output a helpful error if there is a problem with your query.

    Also I would change:
    
    		if (mysql_affected_rows() == 1) {
    			// Success
    		}
    		else{
    			// Failed
    		}
    
    PHP:
    to:
    
    		if (mysql_affected_rows == 1) {
    			echo "A record was updated";
    		}
    		else{
    			echo "A record was NOT updated";
    		}
    
    PHP:
    Note there is no need for the () after mysql_affected_rows and I have added some simple echo statements.

    Obviously also verify that a table subjects exists, and that their is a record within this table with the ID being specified.
     
    wd_2k6, Aug 9, 2009 IP