0

i have this little trouble when i press submit on the form all save's ok! but when i see the db instead save id/name/email/......../date this change the date YEAR to id and email to the name...

this is the PHP Code...

    <?php
if(isset($_POST['submit']))
{
//Conexión a la base de datos
$servidor = "localhost"; //Nombre del servidor
$usuario = "user"; //Nombre de usuario en tu servidor
$password = "pass"; //Contraseña del usuario
$base = "databse"; //Nombre de la BD
//conection:
$conn = mysqli_connect("$servidor","$usuario","$password","$base") or die("Error " . mysqli_error($conn));
if(! $conn )
{
die('Could not connect: ' . mysqli_error($conn));
}
if(! get_magic_quotes_gpc() )
{
$nombre = addslashes ($_POST['name']);
$correo_electronico = addslashes ($_POST['email']);
$celular = addslashes ($_POST['celular']);
$oficina = addslashes ($_POST['oficina']);
$placa = addslashes ($_POST['placa']);
$modelo = addslashes ($_POST['modelo']);
$color_vehiculo= addslashes ($_POST['colorvehiculo']);
$kilometraje = addslashes ($_POST['kilometraje']);
$tipo_de_tapiceria = addslashes ($_POST['tapiceria']);
$tipo_de_cita = addslashes ($_POST['cita']);
$comentario = addslashes ($_POST['message']);
$fecha_de_cita = addslashes ($_POST['datepicker']);
}
else
{
$nombre = $_POST['name'];
$correo_electronico= $_POST['email'];
$celular = $_POST['celular'];
$oficina= $_POST['oficina']; 
$placa= $_POST['placa'];
$modelo= $_POST['modelo'];
$color_vehiculo= $_POST['colorvehiculo'];
$kilometraje= $_POST['kilometraje'];
$tipo_de_tapiceria= $_POST['tapiceria'];
$tipo_de_cita= $_POST['cita'];
$fecha_de_cita= $_POST['datepicker'];
$comentario= $_POST['message'];
}
$sql = "INSERT INTO citas
"."(id,nombre,email,celular,oficina,placa,modelo,colorvehiculo,kilometraje,tapiceria,cita,comentario,fecha) ".
   "VALUES('$nombre','$correo_electronico','$celular','$oficina','$placa','$modelo','$color_vehiculo','$kilometraje','$tipo_de_tapiceria','$tipo_de_cita','$comentario','$fecha_de_cita', NOW())";
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('Could not enter data: ' . mysqli_error($conn));
}
echo "Entered data successfully\n";
mysqli_close($conn);
}
else
{
?>

ofc on the DB the ID is autoincrement and primary key

when i change the query like this

(nombre,email,celular,oficina,placa,modelo,colorvehiculo,kilometraje,tapiceria,cita,comentario,fecha,id)

this save me all in order like i want BUT the id take again the year 2014(the year that i put on the date) and when i want a second register says me Duplicate entry ’2014′ for key ‘PRIMARY’ and couldnt save...

Thx for the Help

  • 1
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Jul 08 '14 at 13:55
  • 1
    That's because __you__ set the field ``id`` in code and doesn't let the database create the id! – thedom Jul 08 '14 at 13:57
  • @thedom but if i delete the id on the code give me the next error.. **Could not enter data: Column count doesn't match value count at row 1** – Ramón León Jul 08 '14 at 14:01
  • @RamónLeón You have to remove ``'$nombre',`` within the SQL statement... – thedom Jul 08 '14 at 14:03
  • @thedom good, but still on my trouble the id now is 2014/2015/2016....../20xx the Name have the email field, as well as the other fields also moved a field, but the name does not appear – Ramón León Jul 08 '14 at 14:12

2 Answers2

1

First, remember that things will go into the positions you put them in... Your current code has this:

INSERT INTO Citas(id, nombre, etc.)
VALUES('$nombre', etc.)

You are actually attempting to insert the persons name into the ID column! That's not going to work!

Second, remember that you need to have the same number of columns that you're inserting things into as things being inserted... So something like this won't work:

INSERT INTO Citas(nombre, email, celular)
VALUES('john', 'asdf@asdf.com')

Be sure to count very carefully!

Finally, you do not need to put anything into the ID column if you have auto-increment on! Auto-increment will put a value in there for you. Simply leave ID off after the INTO and make sure you have the same number of items in your VALUE list as you do in your INTO list.

I suspect that you have a problem with your dates. You seem to be trying to insert both $fecha_de_cita' and NOW() into the fecha column. This is probably throwing things off. You'll need to pick one. But my Spanish is also rusty. ;-)

John Chrysostom
  • 3,973
  • 1
  • 34
  • 50
  • ok, but look my friend, now i have this query `$sql = "INSERT INTO citas ". "(nombre,email,celular,oficina,placa,modelo,colorvehiculo,kilometraje,tapiceria,cita,comentario,fecha) ". "VALUES('$nombre','$correo_electronico','$celular','$oficina','$placa','$modelo','$color_vehiculo','$kilometraje','$tipo_de_tapiceria','$tipo_de_cita','$comentario','$fecha_de_cita', NOW())";` when i submit the form i have this error **Could not enter data: Column count doesn't match value count at row 1** – Ramón León Jul 08 '14 at 14:19
  • Like I said above, you need to count carefully. You are trying to insert 13 values into 12 columns. Count them. – John Chrysostom Jul 08 '14 at 14:24
0

You're actually passing the value of $nombre to the id field. You have to pass a valid id, even if the field is auto-incremented (in that case, it would be 'DEFAULT'). And also remove the NOW() at the end of the query.

The query should be something like:

INSERT INTO citas (id,nombre,email,celular,oficina,placa,modelo,colorvehiculo,kilometraje,tapiceria,cita,comentario,fecha) 
VALUES('DEFAULT', '$nombre','$correo_electronico','$celular','$oficina','$placa','$modelo','$color_vehiculo','$kilometraje','$tipo_de_tapiceria','$tipo_de_cita','$comentario','$fecha_de_cita')
ECMachado
  • 31
  • 3