Results 1 to 12 of 12

Thread: [RESOLVED] PHP randomly updates data!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Resolved [RESOLVED] PHP randomly updates data!

    Hi,
    When I execute this code, it is quite random when PHP updates the table!
    PHP Code:
    $query "UPDATE $username SET `dato` = '$ny_dato', `aktivitet` = '$ny_aktivitet', `varighed` = '$ny_varighed' WHERE `id` = $id";
    mysql_query($query) or die(mysql_error()); 

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: PHP randomly updates data!

    What do you mean randomly updates? Does it give an error when it doesn't update?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: PHP randomly updates data!

    Well, I think I found the real prob.
    I'm trying to update several textfields from a page. It seems like I can't post all the fields' own id's to this page.
    How do I post several id's and then php can update the correct rows in mysql?

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: PHP randomly updates data!

    try using arrays ie,

    Code:
    <form method="post">
    <!--First Item -->
    <input type="text" name="name[]"/>
    <input type="text" name="age[]"/>
    <input type="hidden" name="id[]" value="1"/>
    
    <!-- Second Item-->
    <input type="text" name="name[]"/>
    <input type="text" name="age[]"/>
    <input type="hidden" name="id[]" value="2"/>
    
    <!-- Third Item -->
    <input type="text" name="name[]"/>
    <input type="text" name="age[]"/>
    <input type="hidden" name="id[]" value="3"/>
    
    <input type="submit"/>
    </form>
    PHP Code:
    for($i 0$i count($_POST['name']); $i++)
    {
        
    $query "UPDATE table SET( name = '" $_POST['name'][$i] . "', age = " $_POST['age'][$i] .") WHERE id = " $_POST['id'][$i];
        
    mysql_query($query);

    [EDIT]
    If you nothing has changed in the row then it won't get updated, so if you are checking to see if an update has worked with mysql_affected_rows you will get 0 returned.
    Last edited by john tindell; Apr 21st, 2006 at 03:21 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: PHP randomly updates data!

    Ok, my code is now:
    HTML Code:
    <!-- Blah bla --!>
    		echo "
    		<tr>
              <tr> 
                <td align='center'><input name='nydato[]' type='text' value='$dato' size='25'></td>
                <td align='center'><input name='nyaktivitet[]' type='text' value='$aktivitet' size='25'></td>
                <td align='center'><input name='nyvarighed[]' type='text' value='$varighed' size='25'></td>
    			<input type='hidden' name='id[]' value='$id'>
              </tr>
              ";
    <!-- Blah blah --!>
    PHP Code:
        for($i 0$i count($_POST['nydato']); $i++)
        {
        
    $query "UPDATE table SET( dato = '" $_POST['nydato'][$i] . "', aktivitet = " $_POST['nyaktivitet'][$i] ."', varighed = " $_POST['nyvarighed'][$i] .") WHERE id = " $_POST['id'][$i];
        
    mysql_query($query);

    but nothing works :/

  6. #6
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: PHP randomly updates data!

    Have you tried using mysql_affected_rows() to see if its updated or not, it return -1 if the update failed and 0 if no changes have taken place, assuming that mysql_error returns nothing

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: PHP randomly updates data!

    I've tried alerting the values with Javascript, but they return blank

  8. #8
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: PHP randomly updates data!

    The values where? In the form? If they are blank you need to look at the code thats getting them from the browser

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: PHP randomly updates data!

    Yeah, the form data. But I'm confused, it's only on this page it won't work

  10. #10
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: PHP randomly updates data!

    Well if there are no values on the form then there will be nothing to update? Also instead of echoing out huge chunks of html from php you can use <?=$id?> to embed php variables into PHP

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: PHP randomly updates data!

    The form is filled, that's why it confuses me.
    I'm quite new to php, so I don't know other methods then echoing these chunks of html. But I'll try

  12. #12
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: PHP randomly updates data!

    try printing out the query that are being executed and make sure that they are alid, and have the correct information in them.
    PHP Code:
    for($i 0$i count($_POST['nydato']); $i++)
        {
        
    $query "UPDATE table SET( dato = '" $_POST['nydato'][$i] . "', aktivitet = " $_POST['nyaktivitet'][$i] ."', varighed = " $_POST['nyvarighed'][$i] .") WHERE id = " $_POST['id'][$i];
        print 
    $query "<br/>";


Posting Permissions

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



Click Here to Expand Forum to Full Width