Inserting $_GET data from URL to database ...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rukix1x
Forum Newbie
Posts: 5
Joined: Thu Nov 25, 2010 11:35 pm

Inserting $_GET data from URL to database ...

Post 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.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post 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']);
?>
rukix1x
Forum Newbie
Posts: 5
Joined: Thu Nov 25, 2010 11:35 pm

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

Post by rukix1x »

I've already done that. Even with settype(). It doesn't work. The value stored is still zero.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post 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?
rukix1x
Forum Newbie
Posts: 5
Joined: Thu Nov 25, 2010 11:35 pm

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

Post 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.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post 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?
Post Reply