Interesting problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ward@

    #1

    Interesting problem

    First off, thanks to those who assisted me with the nl2br() issue.

    Now here is another interesting "break."

    I brought up a record in my edit_task page, didn't make a change, and
    hit "submit." Now please forget for a moment the fact that I hit
    submit to update a record that hadn't changed...but I received a error
    as if MySQL doesn't like making an update to a record when there isn't
    a change.

    Of course I wanted to check that everything was a normal and by simply
    adding another character or line break, it took the change.

    Thoughts as to why it wouldn't update a record that wasn't changed?
    How would I get around this if/when the client chooses this page,
    decides not to update anything, but hits Submit to return to the
    previous page???

    Thanks.

    --Ward
  • Andy Hassall

    #2
    Re: Interesting problem

    On Wed, 01 Mar 2006 14:12:18 -0500, ward@ wrote:
    [color=blue]
    >I brought up a record in my edit_task page, didn't make a change, and
    >hit "submit." Now please forget for a moment the fact that I hit
    >submit to update a record that hadn't changed...but I received a error
    >as if MySQL doesn't like making an update to a record when there isn't
    >a change.[/color]

    What was the error?

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • frizzle

      #3
      Re: Interesting problem


      ward@ wrote:[color=blue]
      > First off, thanks to those who assisted me with the nl2br() issue.
      >
      > Now here is another interesting "break."
      >
      > I brought up a record in my edit_task page, didn't make a change, and
      > hit "submit." Now please forget for a moment the fact that I hit
      > submit to update a record that hadn't changed...but I received a error
      > as if MySQL doesn't like making an update to a record when there isn't
      > a change.
      >
      > Of course I wanted to check that everything was a normal and by simply
      > adding another character or line break, it took the change.
      >
      > Thoughts as to why it wouldn't update a record that wasn't changed?
      > How would I get around this if/when the client chooses this page,
      > decides not to update anything, but hits Submit to return to the
      > previous page???
      >
      > Thanks.
      >
      > --Ward[/color]

      I bet there are better ways to achieve this, but you could compare the
      data in
      $_POST against the corresponding data in the DB. if they match, do
      nothing.
      If they don't match, update it ...

      Frizzle.

      Comment

      • ward@

        #4
        Re: Interesting problem

        On 1 Mar 2006 11:33:23 -0800, "frizzle" <phpfrizzle@gma il.com> wrote:

        Thanks again for the helpo Frizzle.

        I believe it's this statement that is causing the error...

        if (mysql_affected _rows() == 1) { // If it ran OK.

        This is from...

        if (mysql_affected _rows() == 1) { // If it ran OK.

        // Print a message.
        echo '<h1 id="mainhead">E dit aTask</h1>
        <p>The task has been edited.</p><p><a
        href="edit_task .php">Edit another task?</a><br /><br /></p>';
        exit();

        } else { // If it did not run OK.
        echo '<h1 id="mainhead">S ystem Error</h1>
        <p class="error">T he task could not be edited due to a system
        error. We apologize for any inconvenience.</p>'; // Public message.

        echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query .
        '</p>'; // Debugging message.

        include ('./includes/footer.html');
        exit();
        }


        So I guess this means, since there wasn't a change then the statement
        above if FALSE and there for I got the error message.

        Now I like your idea so what am I comparing against each other?

        I assume an array is created when I hit Submit and thus I'm comparing
        the contents of that array with the existing data in the database.

        If this is the case, am I comparing the created array with the content
        in the page or am I needing to connect to the database and compare the
        data there?

        Am I even in the ballpark?

        Thanks.

        Ward


        [color=blue]
        >
        >I bet there are better ways to achieve this, but you could compare the
        >data in
        >$_POST against the corresponding data in the DB. if they match, do
        >nothing.
        >If they don't match, update it ...
        >
        >Frizzle.[/color]

        Comment

        • Andy Hassall

          #5
          Re: Interesting problem

          On Wed, 01 Mar 2006 15:02:41 -0500, ward@ wrote:
          [color=blue]
          >On 1 Mar 2006 11:33:23 -0800, "frizzle" <phpfrizzle@gma il.com> wrote:
          >
          >Thanks again for the helpo Frizzle.
          >
          >I believe it's this statement that is causing the error...
          >
          >if (mysql_affected _rows() == 1) { // If it ran OK.
          >
          >This is from...
          >
          >if (mysql_affected _rows() == 1) { // If it ran OK.[/color]

          MySQL already checks if the updated values match the existing values, and you
          get mysql_affected_ rows == 0 if they all match.

          mysql> insert into t values (1, 'test');
          Query OK, 1 row affected (0.00 sec)

          mysql> update t set c = 'test' where id = 1;
          Query OK, 0 rows affected (0.00 sec)
          Rows matched: 1 Changed: 0 Warnings: 0

          mysql> update t set c = 'not test' where id = 1;
          Query OK, 1 row affected (0.01 sec)
          Rows matched: 1 Changed: 1 Warnings: 0

          It's the "Changed:" count that makes it out to PHP.

          Don't start doing it yourself by selecting and checking, as you just introduce
          race conditions, and MySQL is already doing this for you anyway.

          Do check for an actual error through, as in mysql_query() returning false;
          _that_ should be flagged up with the error message.

          --
          Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
          http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

          Comment

          Working...