PHP NOW() Error - php

I need a little help with this, I am trying to insert some data into a MYSQL table which includes the now values for date & time for orders.
Code:
$query= "INSERT INTO ordertable
VALUES ('".$listauthor."','".$ordernumber.",'"NOW()"','"NOW()"')";
Error:
Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\createbasket.php on line 15
How can I fix this?

Remove the quotes from around NOW() ... it's a MySQL function ... not a string.

You don't want to encapsulate NOW() with quotes. You'd need to do:
$query= "INSERT INTO ordertable
VALUES ('".$listauthor."','".$ordernumber."',NOW(),NOW())";

$query= "INSERT INTO ordertable VALUES ('".$listauthor."','".$ordernumber.",'"NOW()"','"NOW()"')";
Shouldn't be quotes around NOW
$query = "INSERT INTO ordertable VALUES ('".$listauthur."','".$ordernumber."', NOW(), NOW())";

Now() is a mysql function so don't need to put it inside single/double quotes.When you put inside quotes then it will treat it as variable.Just write as follows :
$query= "INSERT INTO ordertable VALUES ('".$listauthor."','".$ordernumber.",NOW(),NOW())";

Related

Trouble with MYSQL insert and PHP quotations

$sql = "INSERT INTO users (name, password, email, phone, address)
VALUES ('$_POST['name']', '$_POST['password']', '$_POST['email']', '$_POST['phone']', '$_POST['address']', )";
As one can possibly see, I am trying to insert these values into my table; however I am getting an unexpected error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/csc4370FA14_18/public_html/program/assignments/group project3/register.php on line 35.
I assume it has something to do with the single quotations; is there a way to fix this with double quotes, backslash characters?
Try assigning post values to new variable and then use the new variables in your sql statement. For example,
$name = $_POST['name'];
$sql = "INSERT INTO users ".
"(name) ".
"VALUES('$name')";
This should solve your purpose.
$sql = "INSERT INTO users (name, password, email, phone, address)
VALUES ('".$_POST['name']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['phone']."', '".$_POST['address']."', )";
Note: I hope you are adding something after the last , in the query, otherwise this query will fail.

PHP : how to insert (into an Sql table) a variable and concatenate it with the date function

i've been trying to insert a row into an Sql database table , and that row's last column is supposed to contain a variable and i can't figure out how to concatenate that variable with a date function. The problem becomes the single quote marks
$SQL = "INSERT INTO news VALUES (NULL, '$user', '$text'.'date('Y-m-d H:i:s')')";
That $text is supposed to have a "date now" function called right after it so that i would have the date that it was inserted into the table...
Thanks
Try this:
$sql = "INSERT INTO news VALUES (NULL, '$user', '$text<br>".date('Y-m-d H:i:s')."')";
You can try this
$SQL = "INSERT INTO news VALUES (NULL, '$user', '".$text." now()')";

PHP / MySql - Concatenation in query

I am having problems concatenating a variable to a number (with the variable first, because $info1002 is not a known variable), I appreciate my problem here must be my single double quotations and I've tried a lot of combinations and my googling hasn't helped.
mysql_query("INSERT INTO users (ID, info1) VALUES ('','.$info.''002')")or die(mysql_error());
you need to format it like this:
mysql_query("INSERT INTO users (ID, info1) VALUES ('','".$info."002')") or die(mysql_error());
Also, if you have the ID field set to AutoIncrement, you can ommit it like this:
mysql_query("INSERT INTO users (info1) VALUES ('".$info."002')") or die(mysql_error());
This will insert value of $info followed by 002 into the database.
mysql_query("INSERT INTO users (ID, info1) VALUES ('','".$info."002"')")or die(mysql_error());
why don't you concatenate if before adding it to the query? I think it is much easier so you don't have that mass with quotation.
$var = $info.'002';
mysql_query("INSERT INTO users (ID, info1) VALUES (null ,'".$var."') or die(mysql_error());
mysql_query("INSERT INTO users (info1) VALUES ('{$info}002')")or die(mysql_error());
It may not work only if your ID is set NOT NULL and it not set as autoincrement!

Is there a template to insert querys?

I want to insert a few certain values from a php file, to my MySQL database, using a query.
I use the following code snippet:
mysql_query("INSERT INTO `text` VALUES ('', '$user_id', '$text', '"$categories"')");
but I get an error saying the following:
Parse error: syntax error, unexpected T_VARIABLE in
C:\xampp\htdocs\Project\Func\idea.func.php on line 10
Does anyone know what I'm doing wrong?
I have stated both variables, earlier, by making them into real escape strings. My MySQL table structure is as follows:
idea_id (auto)
user_id
text
categories
timestamp
You need to use string concatenation:
mysql_query("INSERT INTO `text` VALUES ('', '$user_id', '$text', '" . $categories . "')");
or get rid of the double quotes surrounding $categories:
mysql_query("INSERT INTO `text` VALUES ('', '$user_id', '$text', '$categories')");
I would prefer
mysql_query(sprintf(
"INSERT INTO `text` VALUES ('', '%s', '%s', '%s')",
mysql_real_escape_string($user_id),
mysql_real_escape_string($text),
mysql_real_escape_string($categories))
);
Change '"$categories"')") to '" . $categories . "')".
You need to put fullstops between the variables to tell PHP that you want to concatenate (join) all of it together as a string.
Point is very important in this case it serves to separate the two variables in this case. If there is no point return error

MySQL Insert syntax error - Cant find it!

There's gotta be something small I keep missing here, but I can't find it for the life of me.
$insert = mysql_query("INSERT INTO USERS
(`FBID`, `FIRST_NAME`, `LAST_NAME`, `GENDER`)
VALUES ('$fbid', '$firstName', '$lastName', '$gender')");
The error is:
Error: 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 '1' at line 1
Any ideas?
You are not having variables correctly escaped. Use mysql_real_escape_string and code like this:
$insert = mysql_query("INSERT INTO USERS (`FBID`, `FIRST_NAME`, `LAST_NAME`, `GENDER`)
VALUES (
'".mysql_real_escape_string($fbid)."',
'".mysql_real_escape_string($firstName)."',
'".mysql_real_escape_string($lastName)."',
'".mysql_real_escape_string($gender)."'
)");
If the variables contain any quotes, they create the problem if you don't properly escape them.
Do any of your names contain single quotes?
Try writing out the value of the query to log/console/debug to ensure that it's what you expect.
Try wrapping your variables in {}.
'{$fbid}', '{$firstName}', '{$lastName}', '{$gender}'
Otherwise you are going to have to use string concatenation.
'".$fbid."','".$firstName."','"...
I'm assuming your variables already contain proper escaped data.
Try doing it like this:
$sql = <<EOL
INSERT INTO USERS (`FBID`, `FIRST_NAME`, `LAST_NAME`, `GENDER`)
VALUES ('$fbid', '$firstName', '$lastName', '$gender')
EOL;
$stmt = mysql_query($sql) or die("MySQL error: " . mysql_error());
This will preserve the query for you in $sql so you can echo it out elsewhere and see what was actually produced.

Categories