Jump to content

Need to change to NULL if variable = 0


zelig

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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("'","&#39;",$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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.