Social Icons

Pages

martes, 18 de febrero de 2014

Como insertar informacion en una Base de datos




En esta ocasión les traigo el siguiente tutorial de como insertar datos en nuestra base de datos en mysql.

Crearemos la base de datos con el nombre bdportal y el nombre de nuestra tabla será tarticulo  



Nombre de la base de datos :  bdportal
Nombre de la tabla :  tarticulo 

Crear tabla

 CREATE TABLE IF NOT EXISTS `tarticulo` (
  `idarticulo` int(11) NOT NULL AUTO_INCREMENT,
  `articulo` varchar(70) COLLATE utf8_spanish_ci DEFAULT NULL,
  `precio` varchar(10) COLLATE utf8_spanish_ci DEFAULT NULL,
  `descripcion` varchar(150) COLLATE utf8_spanish_ci DEFAULT NULL,
  PRIMARY KEY (`idarticulo`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=1 ;


trabajo.php

<?php

class Trabajo
{
 //se declaran las variables
    private $dbh;
    
//se crea la conexion a la base de datos
    public function __construct()
    {
        $this->dbh=new PDO('mysql:host=localhost;dbname=bdprestamo', "root", "");

 //se define las variables tipo arreglo
$this->findcliente=array();
        $this->pro=array();
    }

//se definte el tipo de cotejamiento que e utfo   
private function set_names()
    {
        return $this->dbh->query("SET NAMES 'utf8'");   
    }
        

public function add_cliente()
    {
      //se crea la consulta y los campos a guardar
        self::set_names();
        $sql="insert into cliente
        values
        (null,?,?,?,?,?,?);";
        //print_r($_POST);
        $stmt=$this->dbh->prepare($sql);
        $stmt->bindValue(1,$_POST["nom"],PDO::PARAM_STR);
        $stmt->bindValue(2,$_POST["cedula"],PDO::PARAM_STR);
        $stmt->bindValue(3,$_POST["telf"],PDO::PARAM_STR);
        $stmt->bindValue(4,$_POST["mov"],PDO::PARAM_STR);
        $stmt->bindValue(5,$_POST["claro"],PDO::PARAM_STR);
        $stmt->bindValue(6,$_POST["dir"],PDO::PARAM_STR);
        $stmt->execute();
        $this->dbh=null; // se cierra la conexion a la bd
              
    }
}

index.php


<?php
require_once("trabajo.php");//se llama el archive php donde se encuatra toda la //configuracion

$tra=new Trabajo();

if(isset($_POST["grabar"]) and $_POST["grabar"]=="si")
{
    $tra->add_cliente();
    exit;
}
?>

<!doctype html>
<html lang="es">
<head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="css/estilo.css">
        <title>Agregar Nuevo articulo</title>
</head>

<body>
                               <h1> Agregar Nuevo Articulo</h1>
                        </div>
       


                <div id="cuerpo">
                <form name="form" action="" method="post">
                        <div class="alin">
                               <label for="cedula">Nombre del articulo:</label>
                               <input type="text" " name="articulo"  />
                        </div>
                        <div class="alin">
                               <label  for="nombre">precio:</label>
                               <input type="text" name="precio"/>
                        </div>
                        <div class="alin">
                <label class="lb" for="dir">descripcion:</label>
                <textarea id="dir"  name="des" cols="30" rows="10"></textarea>
                        </div>
                        <br>
                        <input type="hidden" name="grabar" value="si">
                    <input type="submit" class="boton" value="Guardar" title="Guardar" />
                </form>
</body>
</html>






















No hay comentarios :

Publicar un comentario