Reputation: 466
Im trying to insert the userid into my table in a php file upload system.Every other value is properly getting inserted except the userid.Heres the code:
<?php
session_start();
$userid = $_SESSION['id'];
$id1 = $_GET['id'];
include_once "connect_to_mysql.php";
function savedata(){
global $_FILES, $_POST, $putItAt;
$sql = "INSERT INTO files (
ID ,
userid ,
Time ,
FileLocation ,
IP ,
Title
)
VALUES (
NULL ,'$id1' , UNIX_TIMESTAMP( ) , '".mysql_real_escape_string($putItAt)."', '".$_SERVER['REMOTE_ADDR']."', '".mysql_real_escape_string($_POST['title'])."'
);";
mysql_query($sql);
}
any way around this?
Upvotes: 1
Views: 124
Reputation: 42093
Possible Problems:
1. I think problem is that you are passing $id1
as string in your SQL query but I think datatype of userid
filed is int
in your database structure.
Try something like this:
$sql = "INSERT INTO files (
ID ,
userid ,
Time ,
FileLocation ,
IP ,
Title
)
VALUES (
NULL , " . $id1 . " , UNIX_TIMESTAMP( ) , '".mysql_real_escape_string($putItAt)."', '".$_SERVER['REMOTE_ADDR']."', '".mysql_real_escape_string($_POST['title'])."'
);";
2. Also you need to check which userid
you want to insert into database:
$userid = $_SESSION['id'];
$id1 = $_GET['id'];
3. Also print_r( $_SESSION );
and print_r( $_GET );
to check if required values exists in array.
4. Also read Den's Answer which I missed while reading your code.
So your function signature may be something like this:
function savedata( $id1 ) {
------
}
Pass $id1
to savedata
function while calling it.
Upvotes: 1
Reputation: 78
Try with this query. it may works for you.
$sql = "INSERT INTO files (ID
,userid
,Time
,FileLocation
,IP
,Title
)
VALUES (NULL ,'$id1' , UNIX_TIMESTAMP( ) , '".mysql_real_escape_string($putItAt)."', '".$_SERVER['REMOTE_ADDR']."', '".mysql_real_escape_string($_POST['title'])."'
)";
some times because of reserve keyword of mysql it will cause this kind of problem. so try to use ` before and after or field name.
Upvotes: 0
Reputation: 601
According to comments, i think the trouble is that $id1
isn't accessable in savedata()
function. So you need to declate $id1
global inside function or use $_GET['id']
in you sql-query, like VALUES (NULL , " . $_GET['id'] . " , UNIX_TIMESTAMP( )
.
Upvotes: 3