Ramón León
Ramón León

Reputation: 15

Save a Form on Database

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

Upvotes: 0

Views: 78

Answers (2)

ECMachado
ECMachado

Reputation: 31

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')

Upvotes: 0

John Chrysostom
John Chrysostom

Reputation: 3983

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', '[email protected]')

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. ;-)

Upvotes: 1

Related Questions