Reputation: 773
I'm using mysql and php for registration on my web-site. Registration is ok. Mysql do queries immediately. But in login there strange things begin to happen. I insert a test in my php code to insert test row to database. First test code inserted immediately, but 2nd was inserted after series of refresh and relog actions only after 10 minutes. The 3rd test query is the same-after approximately 10 minutes after 2nd query.
Here is login code:
<?php
session_start();
if(isset($_SESSION['id'])){
echo 'You have logged in.';
echo $_SESSION['id'];
}
else {
$email=$_POST['email'];
$password=$_POST['password'];
$db=new mysqli('','','','');
if (mysqli_connect_errno()) {
echo 'Unable to connect to database: '.mysqli_connect_error().'. Please e- mail our system administrator. We will fix this error as soon as possible. Thanks for patience and understanding. ';
exit();
}
//TEST QUERY
$query="insert into test values(3, 'test')";
$result=$db->query($query);
//LOGIN QUERY
$query="select id from users where email='$email' and password='$password'";
$result=$db->query($query);
if ($result->num_rows==0) {
echo 'Incorrect email or password.';
}
else {
$row=$result->fetch_assoc();
$_SESSION['id']=$row['id'];
echo 'You have logged in.';
echo $_SESSION['id'];
//THIS TEST QUERY IS NOT IMPLEMENTED
$query="insert into test values(1, test)";
$result=$db->query($query);
}
}
?>
Where is mistake?
Test table consists of 2 columns: id (medium int, primary key, unsigned) and test(text)
Thanks in advance.
Upvotes: 0
Views: 445
Reputation: 16460
Just a little security hint: your SQL queries are very dangerous as they are prone to SQL injection attacks. See the Wikipedia article for alternatives ...
Upvotes: 1
Reputation: 2821
This looks suspicious to me.
$query="insert into test values(3, 'test')";
Is it trying to set the ID of every row inserted to 3? ID's have gotta be unique.
EDIT:
This probably won't fix your problem, but it will make your life easier by not forcing you to manually change ID's each time.
INSERT INTO test SET <colname>='test'
where <colname>
is the name of the column that "test" is going into.
Upvotes: 1
Reputation: 3523
insert into test values(1, test)
-- test -- needs quotes or you are going to get an error that the column test doesn't exist (unless it does). If it does exist, it's probably going to be empty, as mysql probably doesn't know what you mean by test -- maybe your version thinks it's a constant or something.
If you posted what the table structure of your test table is, that would help solve it probably.
Upvotes: 1
Reputation: 40223
Sounds like the cookie could be expiring after 10 minutes. Run echo session_cache_expire();
to see what your expiration time is set to. More details at http://php.net/manual/en/ref.session.php
Upvotes: 1