I have used following query..
$query1= mysql_query("INSERT into pg(pgmail, pgpass, pgfname, pgmname, pglname, updt, dtcreate) values('$email','$pass', '$fname', '$mname', '$lname', now(), mktime())")or die(mysql_error());
ERROR : FUNCTION mydb.mktime does not exist
You should do this:
...'$mname', '$lname', now(), '".mktime()."')")or die(...
since now() is a valid MYSQL function, but mktime() is a php function, which is not evaluated in string context.
If you're trying to utilise the php mktime() function, you've enclosed the function call in the DB string, so the DB is attempting to call the function (which doesn't exist to the DB).
Simply remove the function call from the string body.
Try NOW(), there is no mktime function in MySQL
There are no mktime mysql function
If you want to use PHP mktime() you should not qoute it
$query1= mysql_query("INSERT into pg(pgmail, pgpass, pgfname, pgmname, pglname, updt, dtcreate) values('$email','$pass', '$fname', '$mname', '$lname', now(), ".mktime().")")or die(mysql_error());
Besides, use time() instead of mktime() without parametrs
$query1= mysql_query("INSERT into pg(pgmail, pgpass, pgfname, pgmname, pglname, updt, dtcreate) values('$email','$pass', '$fname', '$mname', '$lname', now(), " . mktime() . ")")or die(mysql_error());
Related
This question already has an answer here:
Mysql saved my date column as 0000-00-00?
(1 answer)
Closed 6 years ago.
I am trying to add date and time when an account is created when submitting the registration form. I am trying this and it delivers: 0000-00-00 00:00:00
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', now())") or die(mysql_error());
This is completely different than the duplicate answered question you are replacing it with..
$date = date('Y-m-d H:i:s');
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', '$date')") or die(mysql_error());
Is it your fields DATETIME OR TIMESTAMP?, be sure that it is one of them.
Also, if your field is TIMESTAMP you can set the default value as CURRENT_TIMESTAMP
Before using now() function, make sure you have set your timezone and check by echoing wat it is printing before putting it into query directly.
//insert raw time stamp into database
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', '" . time() . "')") or die(mysql_error());
//get time stamp from database and format it
$user = mysql_query("SELECT * FROM users where id = '1'");
if (mysql_num_rows($user)>0) {
$row = mysql_fetch_array($user);
echo 'Created: ', date('F jS Y - g:i a', $row['created']);
} else {
echo 'User doesn\'t exist';
}
Take a look into PHP's date function, it takes 2 parameters. First being the format you want, second being the UNIX time stamp.
So if you enter the raw time stamp into the database, you can retrieve it as that and format it how you like when retrieving it.
On another note, take a look into PDO. mysql_* functions will soon be deprecated.
I've following SQL query which is working absolutely fine .
$sql = "INSERT INTO user_login (user_id, email, password, token)
VALUES ($user_id, '$email', '$user_password', '$token')";
Now I've added two new fields to the respective table called 'token_creation_time' and 'token_last_accessed_time'. These fields will contain the current time values(i.e. UNIX Time stamp values). For this purpose I used time() function from PHP in the SQL query as follows but it didn't work. I don't know where I'm making a mistake.
$sql = "INSERT INTO user_login (user_id, email, password, token, token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', time(), '')";
The last two field's structure in MySQL DB is as follows :
Name : token_creation_time
Type : int(10)
Collation :
Attributes :
Null : No
Default : None
Extra :
Name : token_last_accessed_time
Type : int(10)
Collation :
Attributes :
Null : No
Default : None
Extra :
In my query I want to insert the current time stamp value only into the field 'token_creation_time'.
Thanks.
Just change the Null:No to Null:Yes,it should work
Name : token_last_accessed_time
Type : int(10)
Collation :
Attributes :
Null : Yes
Default : None
Extra :
I had encountered same error in postgresql when i try to insert an empty string '' for timestamps, so i use nullif function to overcome this:
NULLIF(?, '')
i think mysql has same function
AND make sure your column is nullable
You cannot use php time() function inside query.
Either concatenate this:
$sql = "INSERT INTO user_login (user_id, email, password, token, token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', ".time().", '')";
Or use mysql function UNIX_TIMESTAMP():
$sql = "INSERT INTO user_login (user_id, email, password, token, token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', UNIX_TIMESTAMP(),
'')";
You're inserting a literal string containing time() there.
$sql = "INSERT INTO user_login (user_id, email, password, token,
token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', time(), '')";
error here ^^^^^^
either a) use string concatenation to insert the actual timestamp.
$sql = "INSERT INTO user_login (user_id, email, password, token,
token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', " . time() . ", '')";
b) use pdo with a prepared statement (strongly preferred no matter what) to avoid sql injection attacks, or c) use mysql UNIX_TIMESTAMP() function instead of php time.
$sql = "INSERT INTO user_login (user_id, email, password, token,
token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', UNIX_TIMESTAMP(), '')";
a good way to debug this is to print the query statement out before you execute it, and then run it against the database directly, it will no doubt give you an error above shoving a string into an int or an unknown column
Change your table structure and set NULL attribute to Yes. It will work fine :)
Use now() replace with time() it may work.
This works...
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', now())
")
This doesn't work....
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', date('Y-m-d H:i:s', strtotime('+5 hour')))
")
The mydate column in MySQL is of datetime format.
Any ideas why it's not working? I'm currently using now(), but I want it to show the time in my timezone, not the server's timezone.
I'd suggest storing the date in a variable first.
$date = date('Y-m-d H:i:s', strtotime('+5 hour'));
If both your MySQL and PHP servers are operating on the same time-zone and have their clocks properly synchronized, you wont have an issue.
and then execute the query like so:
mysqli_query($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '$date')
")
I hope this helps!
strtotime() is not MySQL function, its PHP function, you need to write it for being executes...
"INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '".date('Y-m-d H:i:s', strtotime('+5 hour'))."'
I'm repeatedly getting a syntax error when inserting in to mysql, normally this works fine but I can't seem to get it to work. I can echo out the variables no problem but for some reason I can't insert them.
variables (the session vars are brought over from another page)
session_start();
$name=$_SESSION['bName'];
$email=$_SESSION['email'];
$ship_address = $_SESSION['sAddress'];
$voucher=$_SESSION['voucher'];
$sku=$_SESSION['sku'];
$credit_card=$_POST['credit_card'];
$security_code=$_POST['security_code'];
$payment_type=$_POST['payment_type'];
$cc_number=substr($credit_card, 0, 4) . str_repeat('x', (strlen($credit_card) - 4)) . substr($credit_card, -4, 4);
$phone=$_SESSION['billPhone'];
$status="Redeemed";
$date = date('Y/m/d');
$tracking ="";
insert query
//Insert Queries
$sqlInsert = "INSERT INTO `customers`(`name`, `email`, `address`, `phone`, `sku`, `creditcard`, `securitycode`, `paymenttype`, `voucher`, `purchase_id`, `tracking`, `status`, `date_recieved`)
VALUES( $name, $email, $ship_address, $phone, $sku, $credit_card, $security_code, $payment_type, $voucher, $purchase_id, $tracking, $status, $date)";
mysql_query($sqlInsert) or die ('Error Inserting into database' . mysql_error());
I've also tried
VALUES( '$name', '$email', '$ship_address', '$phone', '$sku', '$credit_card', '$security_code', '$payment_type', '$voucher', '$purchase_id', '$tracking', '$status', '$date')
but it doesn't work. The error I get is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lastname, fahad#semail.com, 22 toronto ont l6a0l4, 416-123-4567, 1001234, 1234567' at line 1
Any ideas?
Thanks
all string values must be quoted.
VALUES("'.$name.'", "'.$email.'" ...
Do it like this, so the fields are delimited:
VALUES( '$name', '$email', ...
check your error message to see what kind of garbage you are currently generating.
You could use PDO to create prepared statements instead. Then you won't have to worry about escaping your values like drdwilcox's example 'Jerry''s'. It also helps as a counter measure against SQL Injection attacks.
I would almost guarantee that you have a single-quote in your name field. If you want to place a single quote into a string field in SQL, you must double it: 'Jerry''s'
And you need the '$name' version.
mysql_query("INSERT INTO contact_forms(name,ts,ip,email,option,msg)
VALUES('".$name."',
NOW(),
'".$_SERVER['REMOTE_ADDR']."',
'".$email."',
'".$option."',
'".$message."')");
For some reason this thing doesn't work. It throws no errors but it just doesn't work. Can someone tell me why?
Assuming you are doing this in PHP, which is what it appears to be, try changing your code to this to see if you get an error that might be able to add a little more information:
mysql_query("INSERT INTO contact_forms(name,ts,ip,email,option,msg)
VALUES('".$name."',
NOW(),
'".$_SERVER['REMOTE_ADDR']."',
'".$email."',
'".$option."',
'".$message."')", $link);
echo mysql_errno($link) . ": " . mysql_error($link) . "<br>";
In this example the variable $link is your database connection string.
See http://php.net/manual/en/function.mysql-error.php for more information on usage.
Put mysql_error() as zerkms suggested.
Update
option is a MySQL [link=http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html]reserved word[/link] and you can not use it unless you enclose it with back tick (`).
mysql_query("INSERT INTO contact_forms(name,ts,ip,email,`option`,msg)
VALUES('".$name."',
NOW(),
'".$_SERVER['REMOTE_ADDR']."',
'".$email."',
'".$option."',
'".$message."')");
Always use mysql_error() to debug query issues. It is not a good practice to use reserve words in database schema.
Try
$date = now();
$ip = $_SERVER['REMOTE_ADDR'];
$query = "INSERT INTO contact_forms (name,ts,ip,email,option,msg)
VALUES('$name',
'$date',
'$ip',
'$email',
'$option',
'$message')";