Insert data with PHP to BD Mysql

1

I have started learning PHP relatively recently and in the process I am trying to insert the data of a form into a database using PHP as I have been looking at a safe way by means of ready statements and objects and so I am trying it even though I am not if it is the correct form I enclose the data insertion code.

<?php
include('conexion.php');
if (isset($_POST['submit'])) {

$codLibro=$_POST['codLibro'];
$importe=$_POST['importe'];
$tipoMoneda=$_POST['tipoMoneda'];

$peticion="INSERT INTO ejemplares (codLibro,importe,tipoMoneda) 
VALUES (?, ?, ?)";

$stmt = $conn->prepare($peticion);
$stmt->bind_param('dds', $codLibro, $importe, $tipoMoneda);
$stmt->execute();
$stmt->close();

echo "Registro Insertado";
$conn->close();
}
?>

Here I put my PHP file of connection to the Database

Below is my form for sending data in HTML

<form action="addejemplares.php" method="POST">
    <div class="form-group">
        <div class="col-xs-2">

            <label for="codLibro">codLibro:</label>
            <input type="text" class="form-control" name="codLibro" id="codLibro">
            <label for="importe">importe:</label>
            <input type="text" class="form-control" name="importe" id="importe">
            <label for="tipoMoneda">tipoMoneda:</label>
            <input type="text" class="form-control" name="tipoMoneda" id="tipoMoneda">
            <button type="submit" class="btn btn-default" name="submit">Submit</button>
        </div>
        </div>
</form>

As you can see, a simple submission form and then I attached an image of the table to which I want to insert data

And finally I enclose an image of the errors that PHP is taking me First of all thank you very much in advance to the entire community.

Connection

<?php
$conn = new mysqli("localhost", "root", "", "bdBiblioteca");

if (mysqli_connect_errno()) {
die("No se puede conectar a la base de datos:" . mysqli_connect_error());
}else{

    echo "conexion exitosa";
}

$conn->close();

?>
    
asked by Ricki 29.09.2016 в 00:42
source

2 answers

4

In your file "conexion.php" after creating the connection you are closing it.

Then, in the other php file, where you include "conexion.php" you are using the $ conn variable, on line 14 that's why you give that error.

Do not close the connection in your "conexion.php" file, delete this line or comment: $conn->close();

I think this is the error, you can look at this example in the PHP documentation and it's the same as you have Example # 1 Object oriented style

I hope it helps you. Greetings.

    
answered by 29.09.2016 / 01:17
source
0

If it is only for tests, it is ok to send the value containing POST directly, but it would be good to inform yourself how to "clean up" POST to prevent XSS attacks with htmlentities.

link

    
answered by 29.09.2016 в 03:29