Select and insert php data

0

Guys, I doubt this data

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "
Select a.fecha_y_hora, a.usuario 
from jos2p_fb_contact_sample_repeat_cursos as b 
inner join jos2p_fb_contact_sample as a on a.id = b.parent_id 
where b.cursos=53";

$result = $conn->query($sql);

if ($result->num_rows > 0) 
    {
    // output data of each row
        while($row = $result->fetch_assoc()) 
    {   
        $usuario = $row["usuario"];
        $fecha = $row["fecha_y_hora"];
        echo "" . $usuario. 
             " "  . $fecha. "<br>";
        $sql = "INSERT INTO actividades 
        (id, date_time, actividad, usuario_actividad)
         VALUES (NULL,'$fecha', '["1"]', '$usuario')";
         var_dump($sql);
        if ($conn->query($sql) === TRUE) {
            echo "New records created successfully";
        } else 
        {
        echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }   
    } 
    else 
    {
    echo "0 results";
    }
$conn->close();
?>

I already have the while the doubt is like doing an insert to a table in the same database but with the data that I just selected please help me know very little about php and I can not find how to do it :(

The table where I'm going to save the selected data has 4 fields but I can put the fields only I do not know how to do the insert and the cycle of what I got in the select

UPDATE 1: the error that sends now is this Parse error: syntax error, unexpected '1' (T_LNUMBER) on line 28 exactly in the insert

    
asked by Quiroz Luis 28.05.2018 в 23:06
source

1 answer

0

You can declare variables to be inserted later, example

$usuario = $row["usuario"];
$fecha = $row["fecha_y_hora"];

These can now be used afterwards outside the while in a insert

example

INSERT INTO users (user, fecha) values ($usuario, $fecha)
    
answered by 28.05.2018 в 23:28