I'm using a time method:
$time= date('h:i:s');
What I want is to put this time into database in mySQL,
I used:
$query = mysqli_query($conn, "INSERT INTO tab ('ltime') VALUES ($time)"); but it's not working
Where tab is a table and a ltime is a Column with time Type.
What am I doing wrong?
Regards
Your query will goes like this.
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ('$time')");
Your query should be like this
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");
Columns and tables should have backticks and not single quotes
INSERT INTO `tab` (`ltime`) VALUES ($time)
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");
Don't use '' when assigning the column name
The first way specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)VALUES (value1, value2, value3, ...);
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query
INSERT INTO table_name VALUES (value1, value2, value3, ...);
In your case:
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");
there is no need to single quote with column name:
Reference: https://www.w3schools.com/sql/sql_insert.asp
Related
I am updating image using below command and its working fine. Is there any way to update one more column in the table at the same time. There is one more column in which I need to update image description like Human, Animals,Aliens etc.... How to perform this function?
Below Code:
if($_POST["action"] == "insert")
{
$file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$query = "INSERT INTO tbl_images(name) VALUES ('$file')";
if(mysqli_query($connect, $query))
{
echo 'Image Inserted into Database';
}
}
Modify your insert query like this:
$query = "INSERT INTO tbl_images(name, description) VALUES ('$file', '$description')";
Note: You can insert more than one values in a single insert query.
INSERT INTO table_name (column1, column2, column3, ...)VALUES (value1, value2, value3, ...);
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Syntax
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
ex -
INSERT INTO tbl_images (name, dessc) VALUES ($file, $desc);
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()')";
$fname = addslashes($fname);
$lname = addslashes($lname);
$dob = addslashes($dob);
$email = $_POST['email'];
$sql =
"INSERT INTO subscriber
(fname, lname, dob)
VALUES
('".$fname."', '".$lname."', '".$dob."')
WHERE email='".$email."'";
$register = mysql_query($sql) or die("insertion error");
I am getting error in sql query "insertion error". Query is inserting data into DB after removing WHERE statement. What is the error.
You can't use where in an insert statement. You might be thinking of an update instead?
$sql = "update subscriber set fname='".$fname."', lname = '".$lname."', dob = '".$dob."' WHERE email='".$email."'";
If your email is a unique value, you can also combine an insert with an update like this:
insert into
subscriber (fname, lname, dob, email)
values ('".$fname."', '".$lname."', '".$dob."', '".$email."')
on duplicate key update set fname='".$fname."', lname='".$lname."', dob='".$dob."'
This second syntax will insert a row if there isn't one with a matching email (again, this has to be set to a unique constraint on the table) and if there is one there already, it will update the data to the values you passed it.
Basically INSERT statement cannot have where. The only time INSERT statement can have where is when using INSERT INTO...SELECT is used.
The only syntax for select statement are
INSERT INTO TableName VALUES (val1, val2, ..., colN)
and
INSERT INTO TableName (col1, col2) VALUES (val1, val2)
The other one is the
INSERT INTO tableName (col1, col2)
SELECT col1, col2
FROM tableX
WHERE ....
basically what it does is all the records that were selected will be inserted on another table (can be the same table also).
One more thing, Use PDO or MYSQLI
Example of using PDO extension:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
?>
this will allow you to insert records with single quotes.
Oops !!!! You cannot use a WHERE clause with INSERT statement ..
If you are targeting a particular row then please use UPDATE
$sql = "Update subscriber set fname = '".$fname."' , lname = '".$lname."' , dob = '".$dob."'
WHERE email='".$email."'";
$register = mysql_query($sql) or die("insertion error");
Wondering if there is a shorthand version to insert a new record into a table that has the primary key enabled? (i.e. not having to include the key column in the query)
Lets say the key column is called ID, and the other columns are Fname, Lname, and Website
$query = "INSERT INTO myTable VALUES ('Fname', 'Lname', 'Website')";
Use the DEFAULT keyword:
$query = "INSERT INTO myTable VALUES (DEFAULT,'Fname', 'Lname', 'Website')";
Also, you can specify the columns, (which is better practice):
$query = "INSERT INTO myTable
(fname, lname, website)
VALUES
('fname', 'lname', 'website')";
Reference:
http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
I prefer this syntaxis:
$query = "INSERT INTO myTable SET fname='Fname',lname='Lname',website='Website'";
$query = "INSERT INTO myTable VALUES (NULL,'Fname', 'Lname', 'Website')";
Just leaving the value of the AI primary key NULL will assign an auto incremented value.
This is phpMyAdmin method.
$query = "INSERT INTO myTable
(mtb_i_idautoinc, mtb_s_string1, mtb_s_string2)
VALUES
(NULL, 'Jagodina', '35000')";
You can also use blank single quotes for the auto_increment column. something like this. It worked for me.
$query = "INSERT INTO myTable VALUES ('','Fname', 'Lname', 'Website')";
So, I want to insert some data into a MySQL table where the table name that the data is being put into is a PHP Variable.
Something like this:
$tablename = "databasetable";
$insert = mysql_query(
"INSERT INTO '".$tablename."' (column1, col2)
VALUES ('Blah', 'Blah')
");
But that of course doesn't work, so I'm not sure what to do.
By the way, I'm new to PHP and StackOverflow.
Remove the single quotes from around the table name variable and it'll work:
$tablename = "databasetable";
$insert = mysql_query(
"INSERT INTO ".$tablename." (column1, col2) VALUES ('Blah', 'Blah')");
What about :
$tablename = "databasetable";
$insert = mysql_query("INSERT INTO ".$tablename." (column1, col2) VALUES ('Blah', 'Blah')");
ie, without the simple quotes you were putting arround the table name.
Or, as you are using double-quoted string, which means variables are interpolated :
$tablename = "databasetable";
$insert = mysql_query("INSERT INTO $tablename (column1, col2) VALUES ('Blah', 'Blah')");
As a sidenote, in both cases, you must be really sure that $tablename doesn't contain any malicious data !
My answer is similar to Doug's, although I would use 'back-ticks' in the query around the table name to distinguish it as a table as further prevent the possibility of malicious injection...
E.g.
$tablename = "databasetable";
$insert = mysql_query("INSERT INTO `{$tablename}` (column1, col2)
VALUES ('Blah', 'Blah')"
);