SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: check box result into database
-
Aug 28, 2007, 13:39 #1
- 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'; } ?>
-
Aug 28, 2007, 14:08 #2
- 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'];
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
-
Aug 28, 2007, 14:41 #3
- 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';
MySQL v5.1.58
PHP v5.3.6
-
Aug 28, 2007, 15:44 #4
- 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';
}
PHP Code:$visible = $_POST['visible'];
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
-
Aug 28, 2007, 15:49 #5
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:$visible = isset($_POST['visible']) ? 1 : 0;
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Aug 28, 2007, 15:59 #6
- 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;
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
-
Aug 28, 2007, 16:02 #7
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
The ternary operator.
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Aug 28, 2007, 16:07 #8
- 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
-
Aug 29, 2007, 03:13 #9
- 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.
-
Aug 29, 2007, 08:02 #10
- 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