Inserting current date into table - php

I want to insert the current date into the table from my php page but i don't know how to do so. I would also like to know if i am constructing the table right wrt the data type being used. Its a normal "date" data type. Thank you!!

You can do this a variety of ways like:
INSERT INTO tablename (datecolname) VALUES (CURRENT_DATE)
INSERT INTO tablename (datecolname) VALUES (CURRENT_DATE())
INSERT INTO tablename (datecolname) VALUES (NOW())
INSERT INTO tablename (datecolname) VALUES (CUR_DATE())
INSERT INTO tablename (datecolname) VALUES (CURRENT_TIMESTAMP())
INSERT INTO tablename (datecolname) VALUES (CURRENT_TIMESTAMP)
See Mysql's Date and Time functions for more

Related

PHP - Does it need to fetch_array when select then insert

Hello I'm writing PHP and mysql. If I want to select then insert to database, I will
step1.Select from table
step2. mysqli_fetch_array and contain to variable such as $foo
step3. INSERT INTO MyTable (firstname)
VALUES ('$foo')"
Is it possible that I select then insert without fetch from database?
If you want to do this via mysql you can use
INSERT INTO Table2(field1, field2)
SELECT old_field1 as field1, old_field2 as field2 FROM Table1
Just be sure that you match fieldnames with "as".
Its a very simple concept,
mysqli_fetch_array()
returns an array and you cannot directly store an array into a column of a table in database. Either you have to map with the table column or convert it into json to store it into table column.

MySql Bulk(ish) Insert

I'm attempting to do a "bulk" insert with the following query:
INSERT INTO `order_status_histories` VALUES ('3602','52efabe9-5f8c-4512-a994-3227c63dd20e','1','','Order recieved','2014-02-03 16:47:05','2014-02-03 16:47:05'),('3603','52eff713-54fc-4be0-9389-68d5c63dd20e','1','','Order recieved','2014-02-03 22:07:47','2014-02-03 22:07:47'),('3604','52effd1a-bc14-4095-97fd-6d46c63dd20e','1','','Order recieved','2014-02-03 22:33:30','2014-02-03 22:33:30')
However, it is failing, because the first column in the table is recordID, and does not exist in the insert statement.
How can I get around this issue?
When you use
INSERT INTO `order_status_histories` VALUES (....)
you need to pass each value that the table structure requires, in the correct order. You can omit fields from the values if they have a default value in the table structure.
Most of the time, it's better to explicitly list the fields that you want to insert to - if nothing else, it means that if the table structure changes, you won't need to re-write your SQL statements. Try changing your SQL statement so it's structured as:
INSERT INTO `order_status_histories` (field1, field2, ....) VALUES ('value1', 'value2', ....)

INSERT query not working in mysqli

I have a table named user_data which contains 5 rows-id(primary key),name,address,phone,sex.When I try to insert values into the table via this query
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
it doesnt work.But When I tried this one,it works.
mysqli_query($con,"INSERT INTO user_data (name,address,phone,sex) VALUES ('Peter_Gregory', 'Griffin door',35897,'male')");
I didnt understand what is the real issue behind this.I am using PHP 5.4.7 and XAMPP 1.8.1.
instead of this:
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
Use this when the field is set as NOT NULL
mysqli_query($con,"INSERT INTO user_data VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male')");
Or use this when the field is set as NULL
mysqli_query($con,"INSERT INTO user_data VALUES (0, 'Peter_malik', 'Griffin door',35897,'male')")
See the mysql manual: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
It does not insert in the first query since it assumes the first value i.e. 'Peter_malik' is for your primary key and it fails.
When you specify the column names in the query it knows which value is for which column.
I addition in the first case i.e.
'Peter_malik', 'Griffin door',35897,'male'
will refer to
id(pk),name,address,phone
So u need to pass the first value as NULL so that id gets auto incremented. So the first query should be as
VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male')
If your table has 5 columns id, name, address, phone and sex your staement has to provide 5 values, one for each column. Since your stament provides only 4 values, you need a column list to tell MySQL which values you do provide.
If your id column is a auto_increment column, you can provide null in your values clause.
So you have to say:
INSERT INTO user_data VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male');
OR
INSERT INTO user_data (name,address,phone,sex) VALUES
('Peter_malik', 'Griffin door',35897,'male')
You need to include the id column on your insert statement:
mysqli_query($con,"INSERT INTO user_data VALUES (null, 'Peter_malik', 'Griffin door',35897,'male')");
, provided that your id field is set to autoincrement! ;)
If you provide the ID also in your first query, it will work without any problem.
Assuming that id=1
mysqli_query($con,"INSERT INTO user_data VALUES (1,'Peter_malik', 'Griffin door',35897,'male')");
As others said when you have not explicitly mentioned your column names in a query, you have to provide all the values.
when you are trying the following query then...
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
In this query you only inserting 4 values in the table but the table has 5 field so it is causing problem because in the above query the value is inserting form first field & the sequence or datatype mismatching in the database because of this this query causing problem .
If u want insert wuthout specifing field then you may use the following query....
mysqli_query($con,"INSERT INTO user_data VALUES (0,'Peter_malik', 'Griffin door',35897,'male')");<br><br>
It Will work properly.
and in the your second query you also specifying the field name & corresponding their values so that's query not causing any problem.

MySQL query gone wrong?

another query gone wrong!
I am trying to INSERT INTO anretOrders (order, orderNumber) VALUES ('test', 15)
Now, the anretOrders table has 3 columns, but one is the id so i am leaving that out since it auto-increments. The other is "order" which is a text column, and orderNumber which is an int.
I cannot, for the life of me, figure out what is wrong here? what am i missing?
escape the field names since order is a keyword in mysql
INSERT INTO anretOrders (`order`, `orderNumber`) VALUES ('test', 15)
order is a Reserved Words
So try like this
create table anretOrders (`order` varchar(20),orderNumber int);
INSERT INTO anretOrders (`order`, `orderNumber`) VALUES ('test', 15)
Sql FIDDLE

Insert statement not updating without ID

I am doing an ajax to write records into the db. The table has id,fullname. The id is set to primary and auto increment. Now without the id (8 in the below statement) the record is not written.
Below is the insert statement
$sql =
"INSERT INTO user
VALUES ('8','".$_POST['name']."','test4','test5','test6')";
Is it possible to write without the id?
Place NULL instead of '8', that will tell MYSQL to do default auto increment:
$sql="insert into user values (NULL,'".$_POST['name']."','test4','test5','test6')";
Other possibility is to rewrite your query to this from:
$sql="insert into user (field1, field2, field3, field4) values ('".$_POST['name']."','test4','test5','test6')";
In this case you didnt specify id as a column to be inserted, so MySQL will again do the default auto increment
You can also omit the ID column in the query so that It automatically increments the ID Value
Yes it's totally possible but you need to specify which columns you are inserting into... for example:
$sql="insert into user (username, column2, column3, column4) values ('".$_POST['name']."','test4','test5','test6')";
Try
$sql="insert into user values (NULL,'".$_POST['name']."','test4','test5','test6')";
or
$sql="insert into user(`field1`, `field2`, `field3`, `field4`) values ('".$_POST['name']."','test4','test5','test6')";
You can use DEFAULT or NULL instead of '8'
insert into user(DEFAULT, column1, column2, column3) VALUES (....

Categories