-1

He hecho un formulario pero solo puedo insertar datos pero no usar una variable por ejemplo que sera incluida en el formulario al ser mandado:

<?php

require_once 'database.php';
try{
$database_connection = database_connect();
} catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }
$users = $database_connection->query('SELECT * FROM coffee')->fetchAll();

$title = 'Home';
$content = '
<h4>Title 1</h4>
<form method="POST">
<table>
<tr>
    <td><input type="number" name="fname" required placeholder="First Name"></td>

</tr>
<tr>
    <td><input type="text" name="lname" required placeholder="Last Name"></td>

</tr>
<tr>
     <td><input type="text" name="age" required placeholder="Age" min="10" > </td>

</tr>
<tr>
    <td><input type="submit" name="insert"></input></td>
</br>
</tr>
</form></table>
<br>
';
$content .=  '<br><table>';
if(isset($_POST['insert']))
{


// get values form input text and number
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $age = $_POST['age'];

$uso = $database_connection->query('INSERT INTO coffee(id) VALUES(fname)');

}else{
    echo "nothing";
}
foreach($users as $user) {

    $content .= '<tr>';
    $content .= '<td>' . $user["id"] . '</td>';
    $content .= '<td>' . $user["type"] . '</td>';
    $content .= '<td>' . $user["price"] . '</td>';
    $content .= '<td><a href="index.php">' . $user["price"] . '<a/></td>';
    $content .= '<td><a href="index.php">' . $user["price"] . '<a/></td>';
    $content .= '</tr>';
}




$content .=  '</table>';

include 'Template.php';
?>

Es basicamente que no puedo usar :fname, :lname, :age alguna solucion a este problema¿

1
  • De qué tipo es el campo "id" de la tabla coffee? Por otro lado, el valor que le estás pasando, de ser un texto, no está formateado correctamente. En caso de ser un parámetro, no lo estás asociando en ninguna parte.
    – Muriano
    el 14 sep. 2016 a las 12:39

1 respuesta 1

1

Según la documentación de php, podría ser algo así:

$uso = $database_connection->prepare("INSERT INTO COFFEE (fname, lname, age) VALUES (:fname, :lname, :age)");
$uso->bindParam(':fname', $fname);
$uso->bindParam(':lname', $lname);
$uso->bindParam(':age', $age);
$uso->execute();

Puedes ver más ejemplos en el siguiente enlace: http://php.net/manual/es/pdo.prepared-statements.php

12
  • 1
    no me funciona en mi caso
    – Perl
    el 13 sep. 2016 a las 0:02
  • Necesitamos un poco más de información para poder ayudar.
    – Shaz
    el 13 sep. 2016 a las 0:03
  • pongo al codigo :fname es decir el input que introduce y me da el error Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fname' in 'field list'' in C:\wamp\www\www\tool.php on line 46 un salu2
    – Perl
    el 13 sep. 2016 a las 0:06
  • El mensaje es bastante explícito: El campo 'fname' no existe en la tabla.
    – Shaz
    el 13 sep. 2016 a las 0:09
  • perdona me equivoque de error el error es este Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2031 ' in C:\wamp\www\www\tool.php on line 46 ( ! ) PDOException: SQLSTATE[HY000]: General error: 2031 in C:\wamp\www\www\tool.php on line 46
    – Perl
    el 13 sep. 2016 a las 0:21

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.