zelig Posted April 4, 2012 Share Posted April 4, 2012 Okay, so, I have a script that allows a forum inside of a room in a game. If you want a forum, you mark Yes from a dropdown menu inside the editing portion of the room. It will then save that room's title into a field called `board` in the database. However, when you mark No from the dropdown, it still leaves it in there. I don't want this. I want it to change to NULL. How do I go about doing this? Here's my current coding: if (!islevel($p,$co)) newlevel($p,$co); setlevel($p,$co,"title",filter(stripslashes($title))); setlevel($p,$co,"board",filter(stripslashes($title))); } Explanations: setlevel just connects to the database and inserts these values. $title is obviously the title of the room $co = coordinates for the room Here is the editing page, where the dropdown menu is: <table width=100% style=text-align:center><tr> <td>BOARD:<br /> <select name=b> <option value=0>No</option> <option value=1 <? if ($b == 1) echo "selected" ?>>Yes</option></select><br />Was <?=$b?> </tr></table> Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/ Share on other sites More sharing options...
AyKay47 Posted April 4, 2012 Share Posted April 4, 2012 we can't help you with this without seeing all of the relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334348 Share on other sites More sharing options...
zelig Posted April 4, 2012 Author Share Posted April 4, 2012 Good point. Here it is: Input form: <form action=levedit.php method=post> <table border=0><tr> <td> TITLE:<br> <input type=text name=title value="<?=$title?>"> <table width=100% style=text-align:center><tr> <td>BOARD:<br /> <select name=b> <option value=0>No</option> <option value=1 <? if ($b == 1) echo "selected" ?>>Yes</option></select><br />Was <?=$b?> </tr></table> <input type=hidden name=p value=<?=$p?>><input type=hidden name=co value=<?=$c?>><input type=hidden name=username value=<?=$username?>><input type=hidden name=password value=<?=$password?>><input type=hidden name=no value=<?=$no?>> <input type=submit value="Edit Level"> <input type=reset value=Reset> Levedit.php: <? require("function.s"); if (!is_numeric(get("admin"))){ if (!islevel($p,$co)) newlevel($p,$co); setlevel($p,$co,"title",filter(stripslashes($title))); if(b == 1) setlevel($p,$co,"board",filter(stripslashes($title))); if(b == 0) setlevel($p,$co,"board",NULL); echo "Level editted/created.<br>\n"; } ?> The setlevel and islevel functions (from function.s): function islevel($world,$coords){ $data = false; $dbh=dbconnect() or die ('IsUser error: ' . mysql_error()."<br>"); mysql_select_db("XX"); $result = mysql_query("SELECT * FROM levels WHERE coords = '$coords' AND world = '$world'"); if (mysql_num_rows($result) > 0) $data = true; mysql_close($dbh); return $data; } function setlevel($world,$coords,$var,$value){ $value = str_replace("'","'",$value); $dbh=dbconnect() or die ('SetUser error: ' . mysql_error()."<br>"); mysql_select_db("XX"); if(is_numeric($value)) mysql_query("UPDATE levels SET $var = $value WHERE coords = '$coords' AND world = '$world'"); else mysql_query("UPDATE levels SET $var = '$value' WHERE coords = '$coords' AND world = '$world'"); mysql_close($dbh); return 1; } Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334351 Share on other sites More sharing options...
batwimp Posted April 4, 2012 Share Posted April 4, 2012 I've never sent NULL like that to a function, so I'm not sure it will propagate all the way into your database the way you want it to. You may try echoing out the completed mySQL query (assign it to a variable first) and see if it contains the correct information. I would recommend assigning to the field something besides NULL in this case, like the string "None" or something that would be easier to write to/check against down the road. But that's just me. Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334355 Share on other sites More sharing options...
zelig Posted April 4, 2012 Author Share Posted April 4, 2012 Okay, I've got it to put a 0 in now, but I thought there was a way for it to change that column in the `board` field to NULL... Updated portion: if($b == 1) setlevel($p,$co,"board",filter(stripslashes($title))); if($b == 0) setlevel($p,$co,"board",$b); Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334356 Share on other sites More sharing options...
batwimp Posted April 4, 2012 Share Posted April 4, 2012 There is, but you would probably have to check for it inside the function, then add NULL to the DB query. Like I said, I've never done it before, so maybe one of the gurus here can help you with the specifics. Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334359 Share on other sites More sharing options...
zelig Posted April 4, 2012 Author Share Posted April 4, 2012 Ah, okay. Thanks batwimp! Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334360 Share on other sites More sharing options...
AyKay47 Posted April 4, 2012 Share Posted April 4, 2012 I would simply set the arg $value to null as default: setlevel($world,$coords,$var,$value = null) Then, if $b == 0, do not pass a value for that arg: if($b == 0) setlevel($p,$co,"board"); which will keep $value as NULL in your function. Depending on your logic for this, the best option would be to change the mysql field definition to default NULL, changing the value when yes is selected. Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334361 Share on other sites More sharing options...
zelig Posted April 4, 2012 Author Share Posted April 4, 2012 Hmm, all that did was delete out all the info, but not checkmark the field as being NULL. Question: What exactly is the difference between a blank field and one that is checkmarked as NULL? Is there even anything that I need to worry about, or is it pretty much the same thing? Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334363 Share on other sites More sharing options...
batwimp Posted April 4, 2012 Share Posted April 4, 2012 But it also needs to be put back to NULL once a value is already in there if the user selects it. A default value in MySQL won't do that if he's doing an update. Will it? Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334366 Share on other sites More sharing options...
AyKay47 Posted April 4, 2012 Share Posted April 4, 2012 Question: What exactly is the difference between a blank field and one that is checkmarked as NULL? Is there even anything that I need to worry about, or is it pretty much the same thing? check marked as null? But it also needs to be put back to NULL once a value is already in there if the user selects it. A default value in MySQL won't do that if he's doing an update. Will it? I'm trying to wrap my head around the logic here, but I haven't been given enough information, so it's hard to tell. Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334394 Share on other sites More sharing options...
zelig Posted April 4, 2012 Author Share Posted April 4, 2012 Marked as null. In the MySQL DB, there's a checkmark to indicate that it is null. Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334395 Share on other sites More sharing options...
AyKay47 Posted April 4, 2012 Share Posted April 4, 2012 Marked as null. In the MySQL DB, there's a checkmark to indicate that it is null. Oh, right, how are these values initially being inserted? Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334397 Share on other sites More sharing options...
zelig Posted April 4, 2012 Author Share Posted April 4, 2012 Through the input form near the top of the post. Quote Link to comment https://forums.phpfreaks.com/topic/260344-need-to-change-to-null-if-variable-0/#findComment-1334398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.