Insert data using PDO

0

Hi, I'm running the following PHP-mySql codes, but I'm not one of them, and I have the slightest idea why, to see if any of you knows how to help me and thanks in advance

Upload.php, this updates perfectly.

<?php
include_once "con.php";
switch ($_GET['tipo']) {
    case 1:
    //Modificar el nombre //error
        if (isset($_GET['code'])) {
            $id=$_GET['code'];
            $nombre = $_POST['name'];
            $sentencia = $con->prepare("UPDATE birthdays SET name = ? WHERE id = ?;");
            $resultado = $sentencia->execute([$nombre, $id]);
            header('Location: ../admin');


        } 
        else {
            die("No se a recibido nada");
        }   
    break;
    case 2:
    //Modificar el apellido
        if (isset($_GET['code'])) {
            $id=$_GET['code'];
            $apellido = $_POST['surname'];
            $sentencia = $con->prepare("UPDATE birthdays SET surname = ? WHERE id = ?;");
            $resultado = $sentencia->execute([$apellido, $id]);
            ?>
            <script type="text/javascript">
                window.location.replace("../admin");
            </script>
            <?php
        } 
        else {
            die("No se a recibido nada");
        }   
    break;
    case 3:
    //Modificar el cargo
        if (isset($_GET['code'])) {
            $id=$_GET['code'];
            $job = $_POST['work'];
            $sentencia = $con->prepare("UPDATE birthdays SET job = ? WHERE id = ?;");
            $resultado = $sentencia->execute([$job, $id]);
            ?>
            <script type="text/javascript">
                window.location.replace("../admin");
            </script>
            <?php
        } 
        else {
            die("No se a recibido nada");
        }   
    break;
    case 4:
    //Modificar la fecha de nacimiento
        if (isset($_GET['code'])) {
            $id=$_GET['code'];
            $fecha = explode("/", $_POST['birthday']);
            $day = $fecha[0];
            $month = $fecha[1];
            $year = $fecha[2];
            $sentencia = $con->prepare("UPDATE birthdays SET day = ?, month = ?, year = ? WHERE id = ?;");
            $resultado = $sentencia->execute([$day, $month, $year, $id]);
            ?>
            <script type="text/javascript">
                window.location.replace("../admin");
            </script>
            <?php
        } 
        else {
            die("No se a recibido nada");
        }   
    break;
    case 5:
    //Modificar la fecha en la que empezo a trabjar
        if (isset($_GET['code'])) {
            $id=$_GET['code'];
            $inicio = $_POST['contratation'];
            $sentencia = $con->prepare("UPDATE birthdays SET start = ? WHERE id = ?;");
            $resultado = $sentencia->execute([$inicio, $id]);
            ?>
            <script type="text/javascript">
                window.location.replace("../admin");
            </script>
            <?php
        } 
        else {
            die("No se a recibido nada");
        }   
    break;
    case 6:
    //Modificar la imagen
        if (isset($_GET['code'])) {
            $id=$_GET['code'];
            if (!file_exists('../images/'.$id)) {
                $oldmask = umask(0);
                mkdir('../images/'.$id, 0777);
                umask($oldmask);
            }
            if($fila['image']!=NULL) {
                unlink('../images/'.$id."/".$id.'.png');
            }
            $ds = DIRECTORY_SEPARATOR;
            $storeFolder = '../images/'.$id;
            if (!empty($_FILES)) {

                $tempFile = $_FILES['file']['tmp_name'];             
                $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
                $targetFile =  $targetPath.$id. ".png";
                move_uploaded_file($tempFile,$targetFile); 
                $sentencia = $con->prepare("UPDATE birthdays SET image = ? WHERE id = ?;");
                $resultado = $sentencia->execute([$id.'.png', $id]);
            }
            else {
                die("No se a subido nada");
            }

        }
        else {
                die("No se a recibido nada");
        }
    break;
}

?>

Save.php this should create a new record but never do it, it always goes through the else, and when I go to look in the Database has not inserted anything.

<?php
include_once "con.php";
$nombre = 'Seadog';
$apellido = 'Gato';
$job = 'Dañar';
$fecha = explode("/", $_POST['birthday']);
$day = '24';
$month = '09';
$year = '1992';
$inicio = '10/05/2000';
$sentencia = $con->prepare("INSERT INTO 'birthdays' ('id', 'name', 'surname', 'year', 'month', 'day', 'job', 'start', 'image', 'content')  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
$resultado = $sentencia->execute([NULL, $nombre, $apellidos, $year, $month, $day, $job, $inicio, NULL, NULL]); 

//header('Location: ../admin');
if($resultado === TRUE) echo "Insertado correctamente";
else echo "Algo salió mal. Por favor verifica que la tabla exista";
?>

and I did a test with a try..catch and I get the following error:

and the code by which I get the error is this:

This is the BD

    
asked by Andrés Vélez 06.11.2018 в 18:04
source

1 answer

0

In PDO, before executing your query with the function

execute();

Do you have to replace the characters "?" with the variables that you are going to insert, It should be something like that.

$sentencia = $con->prepare("INSERT INTO 'birthdays' ('id', 'name', 'surname', 'year', 'month', 'day', 'job', 'start', 'image', 'content')  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");

$sentecia->bindParam(1, $nombre);
$sentecia->bindParam(2, $apellido);
$sentecia->bindParam(3, $year);
...

$resultado = $sentencia->execute();
if($resultado) echo "Insertado correctamente";
else echo "Algo salió mal. Por favor verifica que la tabla exista";
    
answered by 07.11.2018 в 20:24