Forum Moderators: open

Message Too Old, No Replies

SQL default value not entering into table

         

scalp8

4:45 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



I have a table that I've set the default value of a number of the values to "Y". When an entry into the table is made though it still enters "N". I haven't seen any similar posts on here. What could be causing this and what would be the best way to test it. Currently I go through the process of setting up an account on the front end and seeing if it saves correctly.

Demaestro

5:11 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you insert a new record are you passing values to the columns that have a default set to them?

What does your insert statement look like?

scalp8

5:18 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



I thought about that, but it doesn't look like there's a value being entered. Here's the code:

function insert_user_alerts($uid)
{
$message= "";
$sql= "INSERT INTO tbl_alert (nUser_Id, nNewUser, nAddFavourites,
nProfileViewed, nAddHot, nNewMessageMobile,
nNewInterestMobile)
VALUES ('{$uid}', '".addslashes($this->nNewUser)."',
'".addslashes($this->nAddFavourites)."',
'".addslashes($this->nProfileViewed)."',
'".addslashes($this->nAddHot)."',
'".addslashes($this->nNewMessageMobile)."',
'".addslashes($this->nNewInterestMobile)."')";
$res= @mysql_query($sql, $this->dblink);
if (!$res) {
$message.= "Unable to set alerts, please try later.<br>";
} else {
$this->nAlert_Id= @mysql_insert_id($this->dblink);
}
return $message;
}

The table can be updated properly. Here's that code:
function update_user_alerts($uid, $id)
{
$message= "";
$sql= "UPDATE tbl_alert
SET nNewUser = '".addslashes($this->nNewUser)."',
nAddFavourites = '".addslashes($this->nAddFavourites)."',
nProfileViewed = '".addslashes($this->nProfileViewed)."',
nAddHot = '".addslashes($this->nAddHot)."',
nNewMessageMobile = '".addslashes($this->nNewMessageMobile)."',
nNewInterestMobile = '".addslashes($this->nNewInterestMobile)."'
WHERE nUser_Id = '{$uid}' AND nAlert_Id = '{$id}'";
$res= @mysql_query($sql, $this->dblink);
if (!$res)
$message.= "Unable to update your alerts, please try again later.<br>";
return $message;
}

Demaestro

6:00 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What fields have the default values assigned to them?

scalp8

6:10 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Field Null Default
nNewUser Yes Y
nAddFavourites Yes Y
nProfileViewed Yes Y
nAddHot Yes Y
nNewMessageMobile Yes N
nNewInterestMobile Yes N

If I do an insert through MySQL directly it seems to populated the fields correctly.

Demaestro

6:42 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are passing in values for those fields in your insert statement.

If you pass a value in for one of those then it won't use the default. It only uses the default if you don't pass it a value.

Try changing this code ------>

INSERT INTO tbl_alert (nUser_Id, nNewUser, nAddFavourites,
nProfileViewed, nAddHot, nNewMessageMobile,
nNewInterestMobile)
VALUES ('{$uid}', '".addslashes($this->nNewUser)."',
'".addslashes($this->nAddFavourites)."',
'".addslashes($this->nProfileViewed)."',
'".addslashes($this->nAddHot)."',
'".addslashes($this->nNewMessageMobile)."',
'".addslashes($this->nNewInterestMobile)."')";

To this ----->

INSERT INTO tbl_alert (nUser_Id)
VALUES ('{$uid}')";

scalp8

6:56 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Thank you! Worked perfectly!

scalp8

5:35 pm on Feb 4, 2009 (gmt 0)

10+ Year Member



Demaestro,
Hopefully you're still getting emails from this post. I have a related question. If I want to pass a value only if there is a checkbox checked, but if it is not checked use the default value how would I do that?

rocknbil

5:46 pm on Feb 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$fieldvalue = (isset($_POST['checkboxfield']))?$checked_value:$unchecked_value;

If a box it is not checked, it will not be in the posted values.

scalp8

7:27 pm on Feb 4, 2009 (gmt 0)

10+ Year Member



This should be in the function right?

Demaestro

8:09 pm on Feb 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



do you want this to override a default value if it exists?

scalp8

8:25 pm on Feb 5, 2009 (gmt 0)

10+ Year Member



no. i want the default to remain in place unless the user specifies Y.