Page 1 of 1

Inserting $_GET data from URL to database ...

Posted: Mon Dec 27, 2010 11:09 am
by rukix1x
Hi, all! I wanna ask something. Well, here's a code:

HTML:
<a href="something.php?pid=somevalue">Link</a>

something.php:

Code: Select all

<?php
$somevar = $_GET['pid'];
$insert = mysql_query("INSERT INTO table (column1, column2) VALUES ('sometext', '{$somevar}')"); 
?>
where: column1 is of type longtext and column2 is of type int.

(Database connection assumed)

After running something.php, I checked the table using SELECT on MySQL, and somevar becomes zero. Why is it so?

Thanks in advance.

Re: Inserting $_GET data from URL to database ...

Posted: Mon Dec 27, 2010 12:14 pm
by Technical
Maybe because you trying to put a string to integer column?
By the way, use intval() function like this:

Code: Select all

<?php
$somevar = intval($_GET['pid']);
?>

Re: Inserting $_GET data from URL to database ...

Posted: Mon Dec 27, 2010 12:27 pm
by rukix1x
I've already done that. Even with settype(). It doesn't work. The value stored is still zero.

Re: Inserting $_GET data from URL to database ...

Posted: Mon Dec 27, 2010 12:48 pm
by Technical
rukix1x wrote:I've already done that. Even with settype(). It doesn't work. The value stored is still zero.
Maybe you should dig MySQL then. Check your table column types again.
Can you post table creating query here?

Re: Inserting $_GET data from URL to database ...

Posted: Mon Dec 27, 2010 12:53 pm
by rukix1x
Here's my CREATE TABLE:

CREATE TABLE table (id int AUTO_INCREMENT, column1 longtext, column2 int, PRIMARY KEY (id));

Thanks for the quick reply, by the way.

Re: Inserting $_GET data from URL to database ...

Posted: Mon Dec 27, 2010 1:00 pm
by Technical
rukix1x wrote:Here's my CREATE TABLE:

CREATE TABLE table (id int AUTO_INCREMENT, column1 longtext, column2 int, PRIMARY KEY (id));

Thanks for the quick reply, by the way.
Seems fine. Can you post your PHP code, please?