SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 40
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update from checkbox!?

    I'm trying to make a update script, but can't figure out how to update/display if a check box is checked or not!

    Code:
    <form action="insert3_update.php" method="post">
    <input type="hidden" name="ud_id" value="<?php echo ($currUser['ClubID']); ?>">
    First Name: <input name="ClubName" type="text" id="ClubName" value="<?php echo ($currUser['ClubName']); ?>"><br>
    Last Name: <input name="ClubID" type="text" id="ClubID" value="<?php echo ($currUser['ClubID']); ?>"><br>
    <input name="3D" type="checkbox" id="3D" value="???">
    <br>
    
    
    <input type="Submit" value="Update">
    </form>
    How do I retrieve the data from the DB to show in the checkbox?

  2. #2
    The Omnipresent [ArcanE]'s Avatar
    Join Date
    Mar 2005
    Location
    Belgium
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Checkboxes are a little tricky since they don't submit at all if they aren't checked.

    If you have a checkboxe like this
    PHP Code:
    <input name="3D" type="checkbox" id="3D" value="yes"
    If you check it and submit $_POST will contain an entry with key 3D and value "yes"

    If you don't check it and submit $_POST will not contain any entry "3D"
    Webdevelopment : Skyrocket Concepts • Inventis Web Architects
    Ain't got time for the future or the past.
    Live for the moment, make it last.

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That’s a snap:

    Let’s say you database contains value 1 for the column ‘3D’ if it is checked and 0 if it is selected.

    Now retrieve that value from
    PHP Code:
    while($row mysql_fetch_array($result))
    {
    …………
    ………
    .

    $curuser[‘3d’] = $row[‘3d’];


    Now do following in the form
    PHP Code:
    <form action="insert3_update.php" method="post">
    ………………
    ………...
    <input name="3D" type="checkbox" id="3D" value ="1" 
                           <?php 
                            
    if($curuser[‘3d’] == 1
                             print 
    " checked=\"checked\" "
                              
    ?>
                             >CheckboxName

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But how can I make it show if it is checked in my update form? I want to see if its checked in my DB

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you want?
    PHP Code:
    if ($_POST[‘3d’] ==1)
        
    //The checkbox is checked
    else
        
    //The checkbox is not checked 

  6. #6
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also don't forget to put value ="1" in the checkbox field of the form.

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my DB I have used "ENUM ('yes','no')" as my variable. So shouldn it be == 'yes'?

    PHP Code:
    <input name="3D" type="checkbox" id="3D" value="yes"
    <?php 
                            
    if($curuser['3D'] == 'yes'
                             print 
    " checked=\"checked\" "
                              
    ?> ">
    Still no outputt? Any Ideas???

  8. #8
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please show the code where you retreive data from database and assign it to $curuser['3D']. Beside make sure case of '3D' is same in both places (inside input field and where you assign database value to $curuser['3D']). I think you are using '3d' in the first part and '3D' in the second.

    AnyWay show me the code.

  9. #9
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the code...

    PHP Code:
    if (($_GET['ClubID']) && (is_numeric($_GET['ClubID']))) {
    $clubid $_GET['ClubID'];

    $dbhost 'MyHost';
    $dbuser 'username';
    $dbpass 'password';

    $conn mysql_connect($dbhost$dbuser$dbpass) or die                    ("Error connecting to mysql: " mysql_error());

    $dbname 'mydb';
    mysql_select_db($dbname);

    //var_dump($uid);
     
    $query="SELECT * FROM clubs WHERE ClubID='".$clubid."'"
    $result=mysql_query($query) or die("Unable to find requested user");
    $currUser mysql_fetch_array($result); 
    Hope this helps!

  10. #10
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now please run
    following command at the end of the above script after the line that reads $currUser = mysql_fetch_array($result);
    PHP Code:
    print_r($currUser); 
    and show me the output.

    And have you used $currUser['3d'] inside the checkbox. Earlier we had used $curuser['3D'], which off course wouldn't work.

  11. #11
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The result looks like this!

    Array ( [0] => 98 [ClubID] => 98 [1] => mansa_nar3 [ClubName] => mansa_nar3 [2] => yes [3D] => yes )

  12. #12
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now put this line where you need checkbox.
    <input name="3D" type="checkbox" id="3D" value="yes"
    <?php
    if($currUser['3D'] == 'yes')
    print " checked=\"checked\" ";
    ?> ">
    I think it should work now.

  13. #13
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx Kailash... It works. Now how do i update the field in my db...
    PHP Code:
    $ClubID=$_POST['ClubID'];
    $ClubName=$_POST['ClubName'];
    $
    3D=$_POST['3D'];

    $dbhost 'MyHost';
    $dbuser 'username';
    $dbpass 'password';
    $conn mysql_connect($dbhost$dbuser$dbpass) or die                    ("Error connecting to mysql: " mysql_error());

    $dbname 'database';
    mysql_select_db($dbname);


    $query="UPDATE clubs SET ClubID='$ClubID', ClubName='$ClubName', 3D='$3D' WHERE ClubID='$ClubID'";
    mysql_query($query);
    echo 
    "Record Updated";
    mysql_close(); 
    It doesnt update my 3D field?

  14. #14
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn’t work?

    What error does it show?
    Execute your query like this:


    PHP Code:
    if(!mysql_query($query)){
    print 
    “Error Occurred:”mysql_error() ;
    exit();
    }
    echo 
    "Record Updated"
    mysql_close(); 
    And tell me what error does it show.

  15. #15
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get this error...

    "Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /hsphere/local/home/myname/mysite.com/clubadmin/insert3_update.php on line 4"

    The error refers to $3D=$_POST['3D'];

    Can't see the error?

  16. #16
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the error is that variable name in PHP cannot begin with a digit. So replace $3D with $D3. Make that change in all places where you use $3D.

  17. #17
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That helped, but now I get this problem...

    Parse error: syntax error, unexpected '}' in /hsphere/local/home/myname/mysite.com/clubadmin/insert3_update.php on line 21

    It refers to the addon script you gave me...

  18. #18
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which particular line of the addon script, will you mention?

  19. #19
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please post your whole code..
    happy is the man that finds wisdom....wisdom in {PHP}.


  20. #20
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if(!mysql_query($query)){
    print "Error Occurred:". mysql_error() ;
    exit()
    }

  21. #21
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put semicolon after exit().
    like this:
    PHP Code:
    exit(); 
    happy is the man that finds wisdom....wisdom in {PHP}.


  22. #22
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this instead and if error occurs again, mention the line where the error has occured.
    if(!mysql_query($query)){
    print "Error Occurred:". mysql_error() ;
    exit();
    }
    echo "Record Updated";
    mysql_close();

  23. #23
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is because curly braces are not matching? you need to paste the entire code.

  24. #24
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh yes you have missed a semicolon after exit().

  25. #25
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx... That helped... Now i Got no errors, and it seems like it updates the db... Perfect. I might have a question using listboxes later... Again. Thanks!


Bookmarks

Posting Permissions

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