php help
#1

I have this code:

Код:
function new_session($userid, $lifespan="3600", $ipvoid) {
	global $vip, $Db1;
	$expirytime = (string) (time() - $lifespan);
	$delresult = $Db1->query("DELETE FROM sessions WHERE start_time<$expirytime or user_id='$userid'");
	$found=false;
	do {
		$sessid = rand_string(20);
		$sessid2 = rand_string(5);
		$sql=$Db1->query("SELECT * FROM sessions WHERE (sess_id = '$sessid') AND (sess_id2='$sessid2')");
		if($Db1->num_rows() == 0) {
			$found=true;
		}
	} while ($found == false);
	$currtime = (string) (time());
	$sql = $Db1->query("INSERT INTO sessions SET
		sess_id='$sessid',
		sess_id2='$sessid2',
		user_id='$userid',
		start_time='$currtime',
		remote_ip='$vip',
		ipvoid='$ipvoid'
		");
	return array($sessid, $sessid2);
}
But gives me:

Quote:

Database error: Invalid SQL: INSERT INTO sessions SET sess_id='1T1RrMk9UZ3dOVEU1TlR', sess_id2='1T1Rr', user_id='1', start_time='1482959789', remote_ip='***', ipvoid=''
MySQL Error: 1366 (Incorrect integer value: '' for column 'ipvoid' at row 1)
Session halted.

------------

Second code:

Код:
$fivemin=time()-60;
$sql=$Db1->query("DELETE FROM online WHERE dsub<'$fivemin'");
$sql=$Db1->query("SELECT id FROM online WHERE ip='$vip' LIMIT 1");
if($Db1->num_rows() == 0) {
	$sql=$Db1->query("INSERT INTO online (userid, dsub, ip) VALUES ('$userid','".time()."','$vip')");
}
Error:

Quote:

Database error: Invalid SQL: INSERT INTO online (userid, dsub, ip) VALUES ('','1482960025','***')
MySQL Error: 1366 (Incorrect integer value: '' for column 'userid' at row 1)
Session halted.

How to fix?
Reply
#2

$ipvoid and $userid are both string types, how does the function 'new_session' looks filled?

Also, you could cast them to a int if you want, like this for example:
PHP код:
// Normal way
$user_id '20'// user_id is now a string.
$user_id 20// user_id is now a int.
// Casting
$user_id = (int) '20'// user_id is now a int. 
Reply
#3

Should really use prepared statements. Also since $ipvoid is passed into the function as a parameter the error isn't in the function itself but wherever that parameter gets (or rather, doesn't get) assigned a value.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)