Codey Posted May 8, 2014 Share Posted May 8, 2014 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. 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 Attached is a screenshot of the PHPMyAdmin table. 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 = "Fafa1669"; $db = "CodigoFacilito"; ?> Hope some one can guide me in the right direction, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/288354-php-code-help/ Share on other sites More sharing options...
ginerjm Posted May 9, 2014 Share Posted May 9, 2014 Turn on php error checking and also check the results of your call to query and you would have answers. Basically - you left off the quotes in your query statement values, which btw are not secure. Quote Link to comment https://forums.phpfreaks.com/topic/288354-php-code-help/#findComment-1478824 Share on other sites More sharing options...
Codey Posted May 9, 2014 Author Share Posted May 9, 2014 Thank you for the reply, I have php error checking "on", but still code does not work, have checked the quotes you mentioned but same problem persist, no error message is shown everything ok except that data is not saved in the db. Quote Link to comment https://forums.phpfreaks.com/topic/288354-php-code-help/#findComment-1478836 Share on other sites More sharing options...
richei Posted May 9, 2014 Share Posted May 9, 2014 change mysql_query("INSERT INTO CodigoF (NOMBRE,PW) VALUES ('$_POST[nombre]','$_POST[pw]')",$con);echo "datos insertados"; to if(is_numeric($_POST['nombre'])) { $nombre = mysql_real_escape_string($_POST['nombre'); } else { $err[] = "Must be a number"; } if(is_numeric($_POST['pw'])) { $pw = mysql_real_escape_string($_POST['pw'); } else { $err[] = "Password must be a number"; } if(empty($err)) { $ins = mysql_query("INSERT INTO CodigoF (NOMBRE,PW) VALUES ($nombre, $pw)") or die(mysql_error()); if($ins) { echo "datos insertados"; } else { echo "problemas al insertar datos"; } } else { foreach($err as $code) { echo $code."<br>"; } } His input doesn't need the quotes in the query, they're integers in the database table. Quote Link to comment https://forums.phpfreaks.com/topic/288354-php-code-help/#findComment-1478841 Share on other sites More sharing options...
cyberRobot Posted May 9, 2014 Share Posted May 9, 2014 Note that is_numeric() will consider the follow as numbers: 0x539 1337e0 9.1 To make sure the values only contain numbers, you can use ctype_digit(). http://www.php.net/manual/en/function.ctype-digit.php Also, it's worth pointing out that richei added a call to mysql_error() to the following line: $ins = mysql_query("INSERT INTO CodigoF (NOMBRE,PW) VALUES ($nombre, $pw)") or die(mysql_error()); This function should help determine if there's anything wrong with the query. More information about the function can be found here: http://www.php.net/manual/en/function.mysql-error.php Quote Link to comment https://forums.phpfreaks.com/topic/288354-php-code-help/#findComment-1478867 Share on other sites More sharing options...
Codey Posted May 13, 2014 Author Share Posted May 13, 2014 Thank you very much for the help ! Quote Link to comment https://forums.phpfreaks.com/topic/288354-php-code-help/#findComment-1479358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.