Problem to get data from a table and store it in a different second table

0

I have a problem to get data from a table and enter that data in a different table. Obtaining the data from the first table is fine, the problem arises when trying to store the data obtained in the second table. Here is the code used. If anyone can help me ... Thanks!

introducir el c<?php
require '../../include/db_conn.php';
$compra = $_GET['art'];//se recoge de un formulario el valor de art que se almacena en $compra
//hago una consulta en tabla ofertasp donde busca un id= $compra (es unico)
$query = "select * from ofertasp where id = '$compra'";
$result= mysqli_query($con, $query);
//se recogen y muestran los datos)
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        echo "<div class='caja'>". $row['id']. "<br>";
        echo $row['tit']. "<br>";
        echo $row['car1']. "<br>";
        echo $row['car2']. "<br>";
        echo $row['precio'];
        echo "</div>";
//aqui viene lo que falla. No puedeo hacer otra consulta que meta los valores obtenidos en otra tabla
$query2 = "insert into facturas (tit,car1,car2,car3,precio) values ('$row["tit"]','$row["car1"]','$row["car2"]','$row["car3"]','$row["precio"]')";
$result2= mysqli_query($con, $query2);
        }
?>
    
asked by Adrian 03.09.2017 в 19:38
source

1 answer

1

You have a syntax error due to the quotes. You must escape the variables. Change this:

"insert into facturas (tit,car1,car2,car3,precio) values ('$row["tit"]','$row["car1"]','$row["car2"]','$row["car3"]','$row["precio"]')"

for this:

"insert into facturas (tit,car1,car2,car3,precio) values ('".$row["tit"]."','".$row["car1"]."','".$row["car2"]."','".$row["car3"]."','".$row["precio"]."')"
    
answered by 03.09.2017 / 19:43
source