It is not advisable that you mix two styles, that is, the procedural style with the object-oriented style or in your case the extension mysql
with mysqli
, in fact the mysql*
extension was declared obsolete in PHP 5.5.0
and deleted in PHP 7.0.0.
.
I give you some examples how to use each style, even so I advise you to use the last mysqli::prepare
, the query and the data are sent to the server separated from each other, and therefore there is no possibility that they interfere.
mysqli
Object-oriented style
connection:
<?php
$con = new mysqli("localhost", "user", "password", "vende");
/* comprobar la conexión */
if ($con->connect_errno) {
printf("Falló la conexión: %s\n", $con->connect_error);
exit();
}
//Caracteres UTF-8 para MySQL.
if (!$con->set_charset("utf8")) {
printf("Error cargando el conjunto de caracteres utf8: %s\n", $con->error);
exit();
}
?>
Your insert statement:
$insertarv="INSERT INTO vendedor(idVendedor,nombreVendedor,fonoVendedor,emailVendedor) VALUES ('$rut','$nom','$fono','$email')";
//ejecutar insert
$ejecutar = $con->query($insertarv);
if(!$ejecutar){
printf("Error en ejecución: %s\n", $con->error);
}else{
echo "El vendedor de registro exitosamente en la BD";
}
Procedural style (The one you were using)
$con = mysqli_connect("localhost", "user", "password", "vende");
/* comprobar la conexión */
if (mysqli_connect_errno()) {
printf("Falló la conexión: %s\n", mysqli_connect_error());
exit();
}
/* cambiar el conjunto de caracteres a utf8 */
if (!mysqli_set_charset($con, "utf8")) {
printf("Error cargando el conjunto de caracteres utf8: %s\n", mysqli_error($con));
exit();
}
Your insert statement:
$insertarv="INSERT INTO vendedor(idVendedor,nombreVendedor,fonoVendedor,emailVendedor) VALUES ('$rut','$nom','$fono','$email')";
//ejecutar insert
$ejecutar= mysqli_query($con,$insertarv);
if(!$ejecutar){
printf("Error en ejecución: %s\n", mysqli_error($con));
}else{
echo "El vendedor de registro exitosamente en la BD";
}
mysqli::prepare
Sentences prepared in object-oriented style
connection:
<?php
$con = new mysqli("localhost", "user", "password", "vende");
/* comprobar la conexión */
if ($con->connect_errno) {
printf("Falló la conexión: %s\n", $con->connect_error);
exit();
}
//Caracteres UTF-8 para MySQL.
if (!$con->set_charset("utf8")) {
printf("Error cargando el conjunto de caracteres utf8: %s\n", $con->error);
exit();
}
?>
Your insert statement:
$insertarv="INSERT INTO vendedor(idVendedor,nombreVendedor,fonoVendedor,emailVendedor) VALUES (?,?,?,?)";
//Sentencia preparada
$ejecutar = $con->prepare($insertarv);
/* ligar parámetros para marcadores */
$ejecutar->bind_param("isss",$rut,$nom,$fono,$email);
//Ejecutar sentencia.
$rc = $ejecutar->execute();
//Comprobacion de ejecución
if(false===$rc){
printf("Error en ejecución: %s\n", $ejecutar->error);
}else{
echo "El vendedor de registro exitosamente en la BD";
}
mysqli::prepare
mysqli_stmt::bind_param
Note: If idVendedor
is auto_increment
and it's also your primary key
you do not need to add it.
It might look like this:
"INSERT INTO vendedor (nombreVendedor,fonoVendedor,emailVendedor) VALUES ('$nom','$fono','$email')";
Now if $rut = $_POST['rut'];
is a different data to your idVendedor
then you should add that column to your sentence, it is important that you add your columns according to the order in how you created it in your phpMyAdmin
.
It might look like this:
"INSERT INTO vendedor (columna_ruta,nombreVendedor,fonoVendedor,emailVendedor) VALUES ('$rut','$nom','$fono','$email')";