Really weird!! need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thefishmath
Forum Newbie
Posts: 4
Joined: Sun Apr 17, 2011 11:12 am

Really weird!! need help

Post by thefishmath »

Ok so i have the next problem .... my code looks something like this :
$id_topic=$_GET['id_topic']; this works verry well
mysql_query("INSERT INTO posts (id_topic) VALUES('".$id_topic."'); // this doesn't work
Also if I do this
$id_topic=1;
mysql_query("INSERT INTO posts (id_topic) VALUES('".$id_topic."');
it works.
Also I tried using mysql_query("INSERT INTO posts (id_topic) VALUES('".$_GET['id_topic']."'); but it doesn't work....
Please somebody help me
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Really weird!! need help

Post by fugix »

It looks to me the your $_GET function is not receiving a value. What code is sending information to it?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Really weird!! need help

Post by califdon »

Use or die(mysql_error()) on the same line as your mysql_query, to let MySQL tell you what the error is.

Code: Select all

mysql_query("INSERT INTO posts (id_topic) VALUES('".$id_topic."') or die(mysql_error());
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Really weird!! need help

Post by fugix »

Good thinking. Once you do that post the error on here please
thefishmath
Forum Newbie
Posts: 4
Joined: Sun Apr 17, 2011 11:12 am

Re: Really weird!! need help

Post by thefishmath »

this is what I thought but it isn't from the $_GET because I echo $_GET and it shows the correct value.... also it isn't an error , the data are entered correct in the database only on the id_topic it shows 0 in the database... It is really weird and I really don't understand why.... thank you for your answers , if you have any others ideas please tell me
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Really weird!! need help

Post by fugix »

The field that you are inserting data into. Is it set to a varchar?
thefishmath
Forum Newbie
Posts: 4
Joined: Sun Apr 17, 2011 11:12 am

Re: Really weird!! need help

Post by thefishmath »

I tried with varchar,int,bigint and exactly the same problem i used (int)$id_topic for int ... And for varchar i tried to put an string for example 'hello' and work but when i used the GET didn't work ... Again all these means that GET is wrong but it isn't , it shows me the correct value

Ok i use this mysql_query in a if . I tried using it before the if and it worked but in the if it inserts in the database everything correct except this value which is set to 0 ....
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Really weird!! need help

Post by fugix »

So the get value is correct but it is unable to insert it into the table?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Really weird!! need help

Post by califdon »

You are telling MySQL:

Code: Select all

INSERT INTO posts (id_topic) VALUES('".$_GET['id_topic']."')
Normally a field with a name that includes "id" is an integer datatype. If that is true in your case, you are trying to insert a string value (because you are enclosing it in single quotes!), which cannot be inserted into an integer field.

Did you follow my suggestion and obtain a MySQL error message? What did it say?
thefishmath
Forum Newbie
Posts: 4
Joined: Sun Apr 17, 2011 11:12 am

Re: Really weird!! need help

Post by thefishmath »

i managed to solve the problem . The problem was in that if statement because, only there it didn't see the $_GET value I don't understand why but doesn't matter... Thank you for your answers !!!
Post Reply