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">×</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 );