Forum Moderators: coopster

Message Too Old, No Replies

Function won't post values to SQL

         

scalp8

5:44 pm on Jan 20, 2009 (gmt 0)

10+ Year Member



I'm not sure what else to try with this. I've received so much help here, and it looks like I'm not going to stop needing it any time soon!
I wan't to post to a table tbl_events from a form that users can fill out. I can get the function to work (it posts a blank entry into the table), but it is not submitting the values posted in the textboxes. Here's the code for one of the textboxes:

<table width="76%" border="0" cellpadding="2" cellspacing="0" class="maintextCopy">
<tr>
<td colspan="3" align="left"><em><strong>Event name: </strong></em></td>
</tr>
<tr>
<td colspan="3" align="left"><input name="Name" type="text" value="<?=$objU->Name?>" class="inputbox" onKeyDown="changeCSS(this, 'text');" size="35" maxlength="250"></td>
</tr>

Another option I've tried with it is this:

<table width="76%" border="0" cellpadding="2" cellspacing="0" class="maintextCopy">
<tr>
<td colspan="3" align="left"><em><strong>Event name: </strong></em></td>
</tr>
<tr>
<td colspan="3" align="left"><textarea name="Name" cols="60" rows="1" class="textareabox" value=<?=$objU->Name?>></textarea></td>
</tr>

Here's the function:

function insert_event($user_id=0)
{
$message= "";
$sql= "INSERT INTO tbl_events (Name)
VALUES ('".addslashes($this->Name)."')";
$res= @mysql_query($sql, $this->dblink);
if (!$res)
$message.= "Unable to add event, please try again later.<br>";
return $message;
}

Any help is greatly appreciated as I am still very new to this.

enigma1

11:11 am on Jan 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the 2nd option can you try enclosing the value in quotes something like:

<td colspan="3" align="left"><textarea name="Name" cols="60" rows="1" class="textareabox" value="<?php echo $objU->Name; ?>" ></textarea></td>

I also assume the the insert event function gets the posted variables into its member variables. That something else you may want to check once the form is processed.

// Check posted var
echo $_POST['Name'];

scalp8

11:46 am on Jan 21, 2009 (gmt 0)

10+ Year Member



enigma,
Thank you for the response. I was able to get it working last night by changing the function:

Originally:
function insert_event($user_id=0)
{
$message= "";
$sql= "INSERT INTO tbl_events (Name)
VALUES ('".addslashes($this->Name)."')";
$res= @mysql_query($sql, $this->dblink);
if (!$res)
$message.= "Unable to add event, please try again later.<br>";
return $message;
}

New function:
function insert_event($user_id=0)
{
$message= "";
$sql= "INSERT INTO tbl_events (Name)
VALUES ('".addslashes($_POST[Name])."')";
$res= @mysql_query($sql, $this->dblink);
if (!$res)
$message.= "Unable to add event, please try again later.<br>";
return $message;
}

It seems to be working well. There are other places on the site where the $this-> seems to get things to post to the database though. Do you know when the appropriate time to use either $_POST or $this-> would be?

enigma1

1:46 pm on Jan 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$this can be used inside a class and not in a global function or within the global scope as it's basically an internal object ref. I cannot tell from what you posted whether or not the insert_event is a member function or not. When the object is created with the new operator it returns a reference that can be used to access the object/instance.

// eg: global scope
$foo_obj = new foo();

Then "Name" must be a member variable that is linked in someway to the $_POST['Name'] before it can be used.

eg:


class foo {
var $Name = '';
function __construct() {
if( isset($_POST['Name']) ) {
$this->Name = some_filter_func($_POST['Name']);
}
}
function insert_event($user_id=0) {
// Name is initialized
}
}

so once a foo instance is created them $foo_obj->Name can retrieve the Name variable contents because that is initialized during the constructor.