Make Insert in a database with php

1

Good morning,

I'm trying to add a statement to the database, I'm doing it from an insert. I have five variables, the id, the name, the description, the image and if I want to show the news or not. The connections to the database I have already verified that they are done well and such, what fails is that it does not do the INSERT statement well

This is the php where I try to enter the data to the BD, data2.php

if($_POST['add'] == 1) {
    echo "Add";
    $sql = "INSERT INTO noticias (id, name, description, img, show) VALUES (:id, :name, :description, :img, :show)";
    $query = $db->prepare( $sql );
    $imgfile="http://placehold.it/140x120/";
    $image=$_FILES['image']['name'];
    if ($image){
        $extension = getExtension($image);
        $extension = strtolower($extension);
        if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
            echo "Invalid file type";
        } else {
            $size=filesize($_FILES['image']['tmp_name']);
            if ($size > MAX_SIZE*1024) {
                echo "File size error";
            } else {
                $temp=resizeImage($_FILES['image']['tmp_name'],140,120);
                $filename = $url.".".$extension;
                $imgfile="./assets/img/noticias/".$filename;
                imagejpeg ( $temp, $imgfile );
            }
        }
    }
    $id=NULL;
    if ($_POST['enabled'] == "on") {
        $show = 1;
    } else {
        $show = 0;
    }
        $query->execute( array( 
                            ':id'=>$id,
                            ':name'=>$_POST['name'],
                            ':description'=>$_POST['description'],
                            ':img'=>$imgfile,
                            ':show'=>$show ) );

    foreach($db->query ("SELECT MAX(id) AS id FROM cursos") as $row) {
        $id=$row['id'];
    }
    echo ($id);
    echo ($form_data['convs']);
    header('Location: panel.php');

}

And here the html code where the user enters the information, panel.php:

  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#añadirCurso">Añadir Noticia</button>

<div class="modal fade" id="añadirCurso" role="dialog">
<div class="modal-dialog">
  <form enctype="multipart/form-data" role="form" method="post" action="data2.php">
    <input type="hidden" name="add" id="add" value="1">   
    <input type="hidden" name="mod" id="mod" value="0">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Añadir Noticia</h4>
      </div>
      <div class="form-group">
          <label for="name">Nombre</label>
          <input type="text" class="form-control" id="name" name="name" value="" placeholder="Nombre de la noticia">
      </div>
      <div class="form-group">
          <label for="description">Descripción</label>
          <textarea class="form-control rte-zone" name="description" id="description" placeholder="Descripción de la noticia" rows="3"></textarea>
      </div>
      <div class="form-group">
          <label for="image">Imagen</label>
          <input type="file" id="image" name="image">
      </div>
      <div class="checkbox">  
          <input type="checkbox" id="enabled" name="enabled" checked><label for="enabled">Mostrar noticia</label>
      </div>
      <div class="modal-footer">
        <button type="submit" class="btn btn-default">Enviar</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
      </div>
    </div>
  </form>

</div>

These are the errors:

mod_fcgid: stderr: PHP Notice:  Undefined variable:data2.php on line 15

mod_fcgid: stderr: PHP Fatal error:  Call to a member function prepare() on null on line 15 

Line 15:

$query = $db->prepare( $sql );
    
asked by CMorillo 28.08.2017 в 11:20
source

2 answers

4

The error message says that your variable $ db, which should be an instance of the PDO class, is at the time of calling the prepare () method equal to null.

Check that you are effectively instantiating this class before calling the method.

    
answered by 28.08.2017 / 12:04
source
1

You have to clarify in which columns you want to insert data:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...) 

In your case (you will have to put the correct name of your columns):

$sql = "INSERT INTO noticias (id, nombre, descripcion, imagen, show) VALUES (:id, :name, :description, :img, :show)";

Reference: link

    
answered by 28.08.2017 в 11:34