insert save all the records of one table in another more date parameter

0

I want to save all the records that contain a "table A"
in another "table B" when pressing submith event "button save current period

Currently, my main page already contains 2 forms and I thought that to avoid saturating my tables and editable fields anymore,

I decided that I would prefer a "link" button that guides me to save_period.php where only run:

INSERT INTO tabla B SELECT * FROM tabla A

and I returned to insert the table with:

header("location: index.php");

My problem starts in that additionally I want to add an additional value for an extra field that has the "table B" that is period who has to attach the date when this record was added

my example:

INSERT INTO tabla B ('nombre','email','current_time')
SELECT * FROM 'Tabla A'

but I think I'm doing it wrong:

<?php
include "conn.php";
// (variable con el querry = $conn )
INSERT INTO tabla B ('nombre','email','current_time')
    SELECT * FROM 'Tabla A';

header("location: dashboard.php");
?>

Could someone tell me what I'm doing wrong?

    
asked by claus 16.07.2018 в 21:55
source

5 answers

0

I doubt very much that it can be done in that way that you intend to do it, since the result returned by the query is an array or json then only in this case you would be passing a parameter to it, therefore it needs to enter data ...

<?php
include "conn.php";

//nota: creo que aqui le falta invocar el metodo query de la conexion (ya tu sabras como esta el metodo de conexcion y de ejecusion query

// aqui en la variable resultado se alamcenas los valores del resultado

$resultado= SELECT * FROM 'Tabla A';

INSERT INTO tabla B ('nombre','email','current_time') VALUES ('$resultado[0]', '$resultado[1]', current_time());


header("location: dashboard.php");
?>

I hope you serve and mark it as a solution ... By: JJ

    
answered by 16.07.2018 / 22:05
source
0

In principle if you enumerate the fields of the insert you must also list those of the select of the other table, which must have the same name. As for the new field, it occurs to me that you can use the current date (formatted according to your destination date field) taken from the moment you select in table A.

INSERT INTO tabla B (nombre, email, current_time)
    SELECT nombre, email, DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS current_time FROM Tabla A

I hope it serves you.

    
answered by 16.07.2018 в 22:17
0

In addition to the previous response which sent the date from the web server, you could use the server's own getdate () function, which would put the current date of the database server

$resultado= SELECT * FROM 'Tabla A';

INSERT INTO tabla B ('nombre','email','current_time') VALUES ('$resultado[0]', '$resultado[1]', SELECT getdate());
    
answered by 16.07.2018 в 22:22
0

Another possibility is the following:

Create column current_time of tabla B of type TIMESTAMP , indicating as default value CURRENT_TIMESTAMP and setting the restriction NOT NULL .

In the CREATE TABLE it would be:

... current_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL

That way you avoid being driving dates and the database manager will be responsible for putting the time stamp to each record when it is created.

If you opt for that possibility, the insert query could be like this:

INSERT INTO 'tabla B' ('nombre','email')
    SELECT 'nombre','email' FROM 'Tabla A';
    
answered by 16.07.2018 в 22:27
0


I already solved it and added a couple of extra notes, I thank the respondents enormously and even that I did not have time to answer everyone if I took them.
I add as I finish my solution that is very similar to that of srJJ

<?php
include "../controler/conn.php";

$resultado= "SELECT * FROM 'usuario' ";

$query = "INSERT INTO periodo ('nombre', 'email', 'periodo') SELECT nombre, email, current_time  FROM usuario";
sqlsrv_query($conn, $query) or die (print_r( sqlsrv_errors(), true));

header("location: dashboard.php");
?>  

Sorry for not answering before but I got an error that prevented me from identifying the data using the first example and wanted it to be good before thanking and placing the solution implementing it in all my forms.

    
answered by 17.07.2018 в 00:12