Page 1 of 1

how to solve if Value does not insert into database...

Posted: Tue Oct 30, 2007 3:55 am
by arupcse
I have a very simple query for inserting value in the database. that is as follows:

$add_query="insert into sponsors values('','$_REQUEST[idda]','$_REQUEST[myID]')";
$add_query=mysql_query($add_query)or die("Value cannot be inserted..");

here the name of the table is 'sponsors' the structure of the table is as follows:
CREATE TABLE `sponsors` (                                                                                                                                                         
            `id` int(10) NOT NULL auto_increment,                                                                                                                                           
            `sponsoring_id` int(10) default NULL,                                                                                                                                           
            `sponsored_id` int(10) default NULL,                                                                                                                                             
            PRIMARY KEY  (`id`)                                                                                                                                                             
          )
i have checked the value is passing correctly for $_REQUEST[idda]' and $_REQUEST[myID]')". here first field is auto increment.

But these values are not inserted in database

here is a sample . when i echoed the $add_query.:
insert into sponsors values('','171 ','62')Value cannot be inserted.

why it may happen?

Re: how to solve if Value does not insert into database...

Posted: Tue Oct 30, 2007 4:16 am
by marvanni
Have you tried to store the url variable in a separate variable?


Code: Select all

$idda= stripslashes( strval( mosGetParam( $_REQUEST, 'idda', '' ) ) );
$myID= stripslashes( strval( mosGetParam( $_REQUEST, 'myID', '' ) ) );

$add_query="insert into sponsors values('','$idda','$myID')";


Re: how to solve if Value does not insert into database...

Posted: Thu Nov 01, 2007 6:06 pm
by Opie
If the `id` is an auto_increment  based field, you can omit it from the SQL statement.  However, this requires you to specify all of the other fields you are trying to insert.  Maybe something like this:

Code: Select all

$add_query="INSERT INTO sponsors (`sponsoring_id`, `sponsored_id`) VALUES ('$_REQUEST[idda]', '$_REQUEST[myID]')";

Re: how to solve if Value does not insert into database...

Posted: Thu Nov 01, 2007 8:41 pm
by Pentacle
I don't know if you have a reason not to use Joomla api to do things about database. But that's not the question here.

I think the problem is an integer field is being tried to populate with strings. So in the query don't use '$var'. Instead, use only $var.

Re: how to solve if Value does not insert into database...

Posted: Sat Nov 03, 2007 4:49 pm
by lobos
$add_query="insert into sponsors values('','$_REQUEST[idda]','$_REQUEST[myID]')";


For God's sake don't use unfiltered variables from the url in your sql. Google "sql injection" or better yet use this instead of straight $_REQUEST:

$idda = mosGetParam( $_REQUEST, 'idda', '' );
$myID = mosGetParam( $_REQUEST, 'myID', '' );

$add_query="insert into sponsors values('','$idda','$myID')";