-2

another big problem here!

I'm trying to save the information in my form to an mysql database, something simple! But is not working..

Well, the connection works, php is getting form's information but not saving..

This is my html code for the form


                    <form class="formularioregistro" method="post" action="conexion.php">
              <div class="row">
                 <div class="col-sm-6">
                    <div class="form-group">
                       <label class="control-label">Nombre <span class="required">*</span></label>
                       <input class="form-control border-form-control" name="usuario" value="" placeholder="Nombre de usuario" type="text">
                    </div>
                 </div>
                    <div class="col-sm-6">
                    <div class="form-group">
                       <label class="control-label">Correo electrónico <span class="required">*</span></label>
                       <input class="form-control border-form-control " name="correo" value="" placeholder="iamosahan@gmail.com" type="email">
                    </div>
                 </div>
              </div>



                    <input type="submit" class="btn btn-success border-none" style="display:block; margin:auto; 
                    background: linear-gradient(120deg, #8A2908 20%, #2A0A1B 80%);" value="Guardar datos">

              </div>
           </form>

This is my php code


//guardando

$usuario = trim($_POST['usuario']);
$correo = trim($_POST['correo']);


$guardar = "INSERT INTO usuarios(usuario, correo) VALUES ('$usuario', '$correo')";

$resultado = mysqli_query($guardar, $conexion);

if(!$resultado){

    echo "problemas para grabar. " . $usuario ;


}else { 
    echo "todo bien";


} 

?>


hope you can help me guys!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • What is the problem you are having? – Nick Jan 19 '20 at 03:15
  • Start with https://stackoverflow.com/q/60174/2864740 .. – user2864740 Jan 19 '20 at 03:25
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is largely an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Jan 19 '20 at 05:23

1 Answers1

2

It seems you're missing a connection here, or mixed up the order.

mysqli_query(connection, query, resultmode)

$con = mysqli_connect("localhost","my_user","my_password","my_db");
...
$resultado = mysqli_query($con, $guardar);

And recommend that using mysqli_prepare() instead of mysqli_query for preventing sql injection:

mysqli_prepare(connection, query)

mysqli_stmt_bind_param(stmt, type, parameters);

type: i - integer d - double s - string b - BLOB

$con = mysqli_connect("localhost","my_user","my_password","my_db");

$stmt = mysqli_prepare($con, "INSERT INTO usuarios(usuario, correo) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, "ss", $usuario, $correo);
$result = mysqli_stmt_execute($stmt);
TsaiKoga
  • 12,914
  • 2
  • 19
  • 28
  • *"You're missing a connection here."* - About that. They have one alright `mysqli_query($guardar, $conexion)`. They just mixed up the order. They used the connection as its 2nd parameter. Maybe they come from converting from mysql_. – Funk Forty Niner Jan 19 '20 at 05:12
  • @FunkFortyNiner yes, maybe u r right... He didn't post the `$conexion` value. I thought it's resultmode. However, its name look like` conncetion`... And I have fix the answer according to your comment. – TsaiKoga Jan 19 '20 at 05:19