SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check box result into database

    I am trying to take the result from a check box and enter it into mysql. The column type in the database is ENUM('n','y')

    All i get is no value in the database. Please advise where i am going wrong

    Thanks


    Code PHP:
    <?php
    if (isset($_POST['joketext'])):
     
    //a new joke has been entered
    //using the form below
     
    $aid = $_POST['aid'];
    $joketext = $_POST['joketext'];
    $visible = $_POST['visible'];
     
    if ($aid == '') {
    	exit('<p>you must choose an author for this joke. click ' .'"back" and try again.</p>');
    }
     
    $sql = "insert into joke set
    	joketext='$joketext',
    	jokedate=CURDATE(),
    	visible='$visible',
    	authorid='$aid'";
    if (@mysql_query($sql)) {
    	echo '<p> New joke added</p>';
    }else {
    	echo '<p> Error adding new joke: ' .
    	mysql_error() . '</p>';
    }
     
    ?>
     
     
     
    <?php 
     
    echo "<p><label><input type='checkbox' name='visible' " .
    	" />Tick for joke to be viewed by everyone</label><br /></p>";
     
    ?>
     
     
    <input type="submit" value="SUBMIT" />
    </form>			
    <?php endif; ?>
     
    <?php 
    if(isset($_POST['visible'])) {
    	$visible = 'y';
    } else {
    	$visible = 'n';
    }
    ?>

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try changing
    PHP Code:
    $visible $_POST['visible']; 
    to
    PHP Code:
    if(!is_null($_POST['visible'])){$visible true}else{$visible false); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (!empty($_POST['visible'])) $visible 'y';
    else 
    $visible 'n'
    Also, I would recommend using tinyint(1) (1 for true, 0 for false) instead of enum. Enum is a close relative of the devil, just read the manual or ask in the MySQL forum.
    MySQL v5.1.58
    PHP v5.3.6

  4. #4
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try moving:
    PHP Code:
    if(isset($_POST['visible'])) {
        
    $visible 'y';
    } else {
        
    $visible 'n';

    after:
    PHP Code:
    $visible $_POST['visible']; 
    And some general advices:

    1) don't use MySQL's enums;
    2) use INSERT INTO table (field1, field2) VALUES (value1, value2) syntax instead of what you're using, as the former is the standard one;
    3) don't put quotes around numeric values (assuming that author id is integer in your case);
    4) protect your script from SQL Injection

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $visible = isset($_POST['visible']) ? 0
    I second the nomination of TINYINT for this column

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Dan, quick question related to the alternative "if" code you just submitted.

    I have used if statements like that before:
    PHP Code:
    $var = ($ifstatement) ? $iftrue $iffalse
    But what is the method called?

    I am thinking of writing a tutorial on handy methods which aren't very well known, but I can't find out what that method's called.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Thanks everyone for you help
    Regarding ENUM's, i used this becuase it is what in said in Kevin Yanks 'PHP and mysql book'. I see 3 of you do not like them so i'll try the other way using tinyint(1).
    Thanks for all the other hints everyone.

  10. #10
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really recommend looking at the MySQL threads about ENUM. Its a nasty "nonstandard" (as Rudy would stress) datatype.
    MySQL v5.1.58
    PHP v5.3.6


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
  •