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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem updating MySql database..

    Hello. I have a website associated with my IRC channel, and I am trying to get a database update feature working. I have html data from a form, as follows:

    Code:
    <tr>
       <td><input type="checkbox" name="check[]" value="one"></td>
       <td><input type="checkbox" name="check[]" value="two"></td>
       <td><input type="checkbox" name="check[]" value="three"></td>
       <td><input type="checkbox" name="check[]" value="four"></td>   
    </tr>
    This gets passed to the php processor as an array just fine. What I am trying to do, is find out if the userid is already in the database table, and if it is, then to go thru the array and update the fields for each filed checked. If the checkbox is null, then enter a "no", if its checked, then enter a "yes".

    However, if the userid is not in that table, then I want it to insert the userid and the apropriate fields into the table. The names of the check boxes correlate with the names of the fileds in the table. Here is the code so far..

    PHP Code:
    session_start();
    include 
    'config.php';
    $userid $_SESSION['userid'];

    if(!empty(
    $_POST['check'])) {

       
    $query1 "SELECT `userid` FROM `interests` WHERE `userid`= '$userid '";
       
    $result mysql_query($query1);
       while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
          {
           foreach(
    $_POST['check'] as $check) {
              if(
    $check != "yes") {
                 echo 
    'CHECK = NULL </br>';
                 
    $change="no";
                 } else {
                 echo 
    'CHECK = '.$check'</br>';
                 
    $change="yes";
                 }
             
    $query "UPDATE interests SET $choice=$change WHERE userid='$userid'";
             
    mysql_query($query);         
           }    
       }
    }

    echo 
    'Your interests have been updated into the database, and those files will now</br>';
    echo 
    'begin showing up in the files area on your files sections.'
    It is not working yet. Can anyone see where I might need to be heading or doing something differently?

    Thanks

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Change
    PHP Code:
             mysql_query($query); 
    to
    PHP Code:
             mysql_query($query) or die(mysql_error(). '<br>'.$query); 
    ans see the result.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Two things stand out here.
    1.) If $userid is not in the DB nothing happens. maybe put an echo echo $userid." is not in DB"; after last curly bracket to show that
    AND 2.)
    Code:
    $query = "UPDATE interests SET $choice=$change WHERE userid='$userid'";
    $choice is not defined or it should be `choice` as a column name.
    Evolution - The non-random survival of random variants.


  •  

    Tags for this Thread

    Posting Permissions

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