does not keep records in mysql database [closed]

1

I put my code to register but at the time of saving, I do not save anything in the database. This table is taken from the internet and works well, but I added two more fields, address and email, but when saving the data, does not save anything in my table, I know that I have to change mysql for mysqli or PDO, but I do not think that the fault is there thank you very much

<?php
error_reporting(0);
//print_r($_POST);
include "conexion.php";
if(isset($_POST))
{
$insert="INSERT INTO contacto 
(nombre,apellido_paterno,apellido_materno,telefono,direccion,email)
VALUES(
'".$_POST['nombre']."',
'".$_POST['apellido_paterno']."',
'".$_POST['apellido_materno']."',
'".$_POST['telefono']."',
'".$_POST['direccion']."',
'".$_POST['email']."'
 )";
//print_r($insert);
mysql_query($insert, $link);
}
?>
 <html>
     <head>
        <title>Mi Agenda</title>
    <link rel="stylesheet" href="estilos.css" type="text/css" media=screen>
</head>
<body>

<div id="container">

    <div id="header"><b>Mi Agenda</b></div>
    <div id="menu">

    <div class="seccion"><a href="index.html">Inicio</a></div>
    <div class="seccion"><a href="registrar.php">Registrar contacto</a>
    </div>
    <div class="seccion"><a href="listar.php">Listar contacto</a></div>



    </div>
    <div id="content">
    <h2>Registrar contacto</h2>
            <form method="POST">
                <table>
                <tr>
                    <td><label>Nombre:</label></td>
                    <td><input type="text" name="nombre"/></td>
                </tr>
                <tr>
                    <td><label>Apellido Paterno:</label></td>
                    <td><input type="text" name="apellido_paterno"/></td>
                </tr>
                <tr>
                    <td><label>Apellido Materno:</label></td>
                    <td><input type="text" name="apellido_materno"/></td>
                </tr>
                <tr>
                    <td><label>Telefono:</label></td>
                    <td><input type="text" name="telefono"/></td>
                                    </tr>
                <tr>
                    <td><label>direccion:</label></td>
                    <td><input type="text" name="direccion"/></td>
                                    </tr>
                <tr>
                    <td><label>Email:</label></td>
                    <td><input type="text" name="email"/></td>
                </tr>
                 <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" value="Aceptar"/>
                <input type="reset" value="Cancelar"/></td>
                </tr>
                </table>
            </form>
    </div>
    <div id="footer">Desarrollado por Diego - Copyright Todos los Derechos 
    Reservados</div>

   </div>

   </body>
   </html>

   <?php
   mysql_close($link);
    ?>

my code to list

 <?php
 include "conexion.php";
 $query="SELECT * FROM contacto";
 $resultado = mysql_query($query);
 //print_r($resultado);
 while ($fila = mysql_fetch_array($resultado))
 {
 //print_r($fila);
 $contactos[]=$fila;

 }

  //print_r($contactos);
  ?>
     <html>
      <head>
       <title>Mi agenda</title>
    <link rel="stylesheet" href="estilos.css" type="text/css" media=screen>

    </style>
   </head>
     <body>

  <div id="container">

    <div id="header"><b>Mi Agenda</b></div>
    <div id="menu">

    <div class="seccion"><a href="index.html">Inicio</a></div>
    <div class="seccion"><a href="registrar.php">Registrar contacto</a>
    </div>
    <div class="seccion"><a href="listar.php">Listar contacto</a></div>


    </div>
    <div id="content">
    <h2>Lista de contactos</h2>
            <table>
            <tr>
                <th>#</th>
                <th>Nombre</th>
                <th>Apellido Paterno</th>
                <th>Apellido Materno</th>
                <th>Teléfono</th>
                                    <th>direccion</th>
                                    <th>email</th>
            </tr>

            <?php
                foreach($contactos as $c)
                                    {
                    echo"
                    <tr>
                        <td>".$c['id']."</td>
                        <td>".$c['nombre']."</td>
                        <td>".$c['apellido_paterno']."</td>
                        <td>".$c['apellido_materno']."</td>
                        <td>".$c['telefono']."</td>
                        <td>".$c['direcion']."</td>
                        <td>".$c['email']."</td>

                    </tr>


                    ";



                }

            ?>

            </table>
    </div>
    <div id="footer">Desarrollado por Diego - Copyright Todos los Derechos 
    Reservados</div>

  </div>

  </body>
   </html>
    <?php
    mysql_close($link);
    ?>

my ensql table

 -- phpMyAdmin SQL Dump
 -- version 4.6.4
 -- https://www.phpmyadmin.net/
 --
 -- Servidor: X.X.X.99
  -- Tiempo de generación: 06-04-2017 a las 18:55:14
  -- Versión del servidor: 5.7.14
    -- Versión de PHP: 5.6.25

    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  SET time_zone = "+00:00";


     /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
     /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
     /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
     /*!40101 SET NAMES utf8mb4 */;

     --
     -- Base de datos: '***********'
     --
      --
      -- Estructura de tabla para la tabla 'contacto'
        --

 CREATE TABLE 'contacto' (
 'id' bigint(20) NOT NULL,
 'apellido_paterno' text NOT NULL,
 'apellido_materno' text NOT NULL,
 'nombre' text NOT NULL,
 'foto' text NOT NULL,
 'telefono' text NOT NULL,
 'direccion' text NOT NULL,
 'email' text NOT NULL
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Ãndices para tablas volcadas
 --

  --
  -- Indices de la tabla 'contacto'
  --
   ALTER TABLE 'contacto'
 ADD PRIMARY KEY ('id');

--
-- AUTO_INCREMENT de las tablas volcadas
--

 --
-- AUTO_INCREMENT de la tabla 'contacto'
 --
  ALTER TABLE 'contacto'
   MODIFY 'id' bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;    
    
asked by novato 06.04.2017 в 21:10
source

3 answers

1

Probably change the id of your table. Use integer with autoincremental.

ALTER TABLE 'contacto' CHANGE 'id' 'id' INT(10) NOT NULL AUTO_INCREMENT;

This is the condition you use before inserting.

if( !isset($_POST['']) and !empty($_POST['nombre']) ){ .. }

After this it changed to me I worked your code to register.php

    
answered by 19.05.2017 в 04:01
-1

First of all you are using mysql functions that are from the version of php 5.5.0 in disuse, and later eliminated in version 7.0.0 (" link "), for better functionality I recommend using the mysqli extension (" link ").

You should follow the same procedures.

<?php
    $mysqli = mysqli_connect("ejemplo.com", "usuario", "contraseña", "basedatos");

    $nombre = $_POST["nombre"];
    $apellido_paterno = $_POST["apellido_paterno"];
    $apellido_materno = $_POST["telefono"];
    $direccion = $_POST["direccion"];
    $email = $_POST["email"];

    $insert = "INSERT INTO contacto (nombre,apellido_paterno,apellido_materno,telefono,direccion,email) VALUES('$nombre','$apellido_paterno','$apellido_materno','$direccion','$email')";
    mysqli_query($mysqli,$insert);
?>
    
answered by 06.04.2017 в 22:25
-1

Hi, here is a function with mysql to see if it works:

       function insert_db($tbname, $cvalue, $dvalue){

       if($cvalue == "" OR $cvalue == null){
           echo "Verifique el datos de los campos"; 
           return false;
       }

       if($dvalue == "" OR $dvalue == null){
           echo "Verifique el los datos de los valores"; 
           return false;
       }

       $sql= "INSERT INTO ".$tbname." ('".$cvalue."')";
       $sql .= " values (".$dvalue.")";
       $insertResult = mysql_query($sql,$link) or die (mysql_error());
       return $insertResult;

   }

To use it I'll give you the example:

    $tbname = 'contacto ';
    $cvalue = 'nombre,apellido_paterno,apellido_materno,telefono,direccion,email';
    $dvalue = "$_POST['nombre'],$_POST['apellido_paterno'],$_POST['apellido_materno'],$_POST['telefono'],$_POST['direccion'],$_POST['email']";
    insert_db($tbname,$cvalue,$dvalue);
    
answered by 10.04.2017 в 21:12