Updating MySQL table's fields with variables using PHP

Discussion in 'PHP' started by reinaldo83, Mar 16, 2008.

  1. #1
    Hi.

    I need your help. I don't know if this topic is to be posted here or in MySQL sub-forum but i think this will be easy for you.

    I have a PHP script with this.

    $count = 3
    for($i = 0;$i < $count; $i++) {
    $abcde = $result['example'][$i];
    $id = $i+1
    ;

    Then, after i connect the MySQL DB i have this:

    $query = "UPDATE table_name SET field_name = '$abcde' where id='$id'";

    mysql_query($query) or die (mysql_error());


    All i want to do is to update some fields of one of my db's table with a variable.

    I mean, this code works perfectly if i replace $abcde at the UPDATE row for simple text, no variable. If i do this, the text is added to all fields i want to modify perfectly, but if i use a variable like $abcde just the field where id=1 (just the first one, in this case) is updated.

    Just the first one is updated perfectly, the others two are not modified because the system gives to me an error that says:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

    By testing, i think i have to put the variable at the UPDATE row using another syntax.

    Can someone help me with this, saying to me how to write the variable in this point to get my table's fields updated with the information depending of the value of $abcde???

    Thanks in advance!!!

    Reinaldo.

    P.D.: Sorry for my english, i know it isn't very good.
     
    reinaldo83, Mar 16, 2008 IP
  2. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    try this:
    
    $query = "UPDATE table_name SET field_name = '{$abcde}' where id='{$id}'";
    
    Code (markup):
     
    nebhotep, Mar 17, 2008 IP
  3. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do an echo of $query and see what it looks like. You might also want to post the whole script, or at least the whole part that is interested, instead of only some bits and pieces.
     
    CreativeClans, Mar 17, 2008 IP
  4. reinaldo83

    reinaldo83 Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've tried that but it still doesn't working. I don't know what to do... Can anybody else help me?.

    I think the problem is in UDPATE row but i'm not sure because it works fine with any text and with variables just work fine for the first cycle of the loop, after this, when the code arrives to the UPDATE row for second time the system gives to me an error, this:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near....

    Can you help me with this.

    Thank you.

    Reinaldo.
     
    reinaldo83, Mar 17, 2008 IP
  5. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    try to echo the variables before update, see if they get any value
     
    nebhotep, Mar 18, 2008 IP
  6. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #6
    wont $i always be < (less than) $count .. since $i = 0 and $count=3

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    To infinity ...and Beyond!

    j/k :)


    $i = 0;
    while ($i <= 3) {
    echo $i++;
    }
     
    ezprint2008, Mar 18, 2008 IP