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

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
arupcse
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Oct 29, 2007 6:38 am

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

Post by arupcse » Tue Oct 30, 2007 3:55 am

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?

marvanni
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Sat Dec 10, 2005 12:15 pm

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

Post by marvanni » Tue Oct 30, 2007 4:16 am

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


Opie
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Thu Jun 22, 2006 7:32 pm
Contact:

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

Post by Opie » Thu Nov 01, 2007 6:06 pm

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]')";
http://springhillalumni.org • Springhill High School Alumni Association

User avatar
Pentacle
Joomla! Intern
Joomla! Intern
Posts: 61
Joined: Wed Oct 25, 2006 12:34 pm
Location: Turkey

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

Post by Pentacle » Thu Nov 01, 2007 8:41 pm

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.
My Joomla! 1.5 extensions - http://joomla.ercan.us
Progress is made by lazy men looking for easier ways to do things.

User avatar
lobos
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Wed Jul 19, 2006 3:33 pm
Location: Sao Paulo, Brasil
Contact:

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

Post by lobos » Sat Nov 03, 2007 4:49 pm

$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')";
Respect and honour, sempre isso, the tribal way, the only way.
http://en.wikipedia.org/wiki/Tribes_Vengeance


Post Reply