Hello

I am new to PHP and am working on an exercise but is not working, the problem is that is not inserting the data in to mysql database.

When enter the user and password in the html page runs and give no problems, get no errors.

php error checking is "on"

In MySQL one table created with: ID, NOMBRE, PW

Linux OS OpenSuse 13.1 / localhost
Apache 2.4.6
Database Server version 5.5.33-MariaDB
Database client version mysqlnd 5.0.10
PHPMyAdmin 4.1.14

This is my index dot php

<!DOCTYPE>
 <html> 
  <head> 
  <title> Insertar Nombre Password </title> 
  </head> 
  <body>


<form action="insertar.php" method="post" name="form">


       <input type="text" name="nombre" /><br /><br />
       <input type="password" name="pw" /><br /><br />
       <input type="submit" value="insertar datos" />


</form>
</body> 
</html> 

insertar dot php

<?php


 //Include another script in this one.


 include("conexion.php");


 if(isset($_POST['nombre']) && !empty($_POST['nombre']) &&
 isset($_POST['pw']) && !empty($_POST['pw'])) 
 {


//Connect to the database


$con = mysql_connect($host,$user,$pw)or die("problemas al conectar");


mysql_select_db($db,$con)or die("problemas al conectar la bd");


mysql_query("INSERT INTO CodigoF (NOMBRE,PW) VALUES ('$_POST[nombre]','$_POST[pw]')",$con);
echo "datos insertados";


 }


 else {
  echo "problemas al insertar datos";
  }


?>

conexion dot php

<?php


$host = "localhost";
$user = "root";
$pw = "elided";
$db = "dbname";


?>

Hope some one can guide me in the right direction, been working on this for hours now, thank you.

    First, the [man]MySQL[/man] extension is deprecated and should not be used. Use [man]PDO[/man] or [man]MySQLi[/man] instead.

    Second, you should never put unsanitised user data directly into an SQL statement (see the previous point for effective ways to avoid this).

    Third, if the insert statement fails for whatever reason, [man]mysql_query[/man] will return false and error information will be available in [man]mysql_error[/man].

    Fourth, using [man]isset[/man] and [man]empty[/man] together is usually redundant, since [man]empty[/man] already uses [man]isset[/man].
    Finally, I elided some of the sensitive information in your post.

      Hello, Codery, and welcome to PHPBuilder!

      Both Weedpacket and I have "Debugging 101" linked in our signatures. This might be a helpful place to start.

      Specifically, you should consider testing any function or operation that is "fail-able" ... that is, if there any possibility, however, remote, that it might fail, in a conditional statement that will advise you of what went wrong. This is the essence of debugging --- also known as "error handling".

      Some rudimentary examples:

      //a large loop for error handling/debugging
      if ($connection = mysqli_connect($host,$user,$pass,$dbname)) {
         //work goes here
      } else {
         echo "Unable to connect database.";
      }
      
      //a conditional check after the operation
      $connection = mysqli_connect($host,$user,$pass,$dbname);
      
      if (!$connection) { //this works because the function called returns a boolean value
         echo "Unable to connect database.";
      }
      
      //test a characteristic that needs to be present in a variable resulting from an operation
      
      $data = file_get_contents("/tmp/some_text.txt");
      if (!strlen($data)) {
         echo "Data variable is empty.";
         die(); // optional, for debugging only ... you don't want this in a 'live' application.
             //For that matter, you probably don't want "echo" either, except when debugging.  See "custom_error()" below ...
      }
      
      $foo = some_function_that_returns_an_array();
      
      if (!is_array($foo) || empty($foo)) {
         custom_error("Foo array is empty.");  //custom error is your own personal error-handling function/routine.
                                      //Perhaps it writes to a log on the server, or sends you email.
      }
        Write a Reply...