spoco Posted November 17, 2008 Share Posted November 17, 2008 New to PHP/My SQL and trying to make an evaluation form that writes to a database. Everything seems to be working fine, except for one major thing. I am only able to post numbers. Anytime that I enter text, nothing happens. I don't even get an error. All of the fields in the MySQL database are set to text or varchar, and the HTML form is set to accept text. Here is the PHP if anyone sees something that stands out. <?php require_once('databaseconn.php'); ?> <?php $_1a = $_POST['1a']; $_1b = $_POST['1b']; $_1c = $_POST['1c']; $_2a = $_POST['2a']; $_2b = $_POST['2b']; $_2c = $_POST['2c']; $_3a = $_POST['3a']; $_4a = $_POST['4a']; $_5a = $_POST['5a']; $_6a = $_POST['6a']; $_7a = $_POST['7a']; $_8a = $_POST['8a']; mysql_select_db('training', $databaseconn); $insertSQL = sprintf"(INSERT INTO evaluation (1a, 1b, 1c, 2a, 2b, 2c, 3a, 4a, 5a, 6a, 7a, 8a) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", $_1a, $_1b, $_1c, $_2a, $_2b, $_2c, $_3a, $_4a, $_5a, $_6a, $_7a, $_8a); mysql_query($insertSQL, $databaseconn) or die(mysql_error()); /* redirect back to registration page */ header("Location: index.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133091-having-problems-posting-non-numeric-data/ Share on other sites More sharing options...
rhodesa Posted November 17, 2008 Share Posted November 17, 2008 couple things...if you aren't seeing any errors, then you need to turn on error reporting... next, you need to put quotes around your values when inserting also, you should send all your values through mysql_real_escape_string() to prevent SQL Injection: <?php require_once('databaseconn.php'); ?> <?php mysql_select_db('training', $databaseconn); $_1a = mysql_real_escape_string($_POST['1a']); $_1b = mysql_real_escape_string($_POST['1b']); $_1c = mysql_real_escape_string($_POST['1c']); $_2a = mysql_real_escape_string($_POST['2a']); $_2b = mysql_real_escape_string($_POST['2b']); $_2c = mysql_real_escape_string($_POST['2c']); $_3a = mysql_real_escape_string($_POST['3a']); $_4a = mysql_real_escape_string($_POST['4a']); $_5a = mysql_real_escape_string($_POST['5a']); $_6a = mysql_real_escape_string($_POST['6a']); $_7a = mysql_real_escape_string($_POST['7a']); $_8a = mysql_real_escape_string($_POST['8a']); $insertSQL = sprintf"(INSERT INTO evaluation (1a, 1b, 1c, 2a, 2b, 2c, 3a, 4a, 5a, 6a, 7a, 8a) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $_1a, $_1b, $_1c, $_2a, $_2b, $_2c, $_3a, $_4a, $_5a, $_6a, $_7a, $_8a); mysql_query($insertSQL, $databaseconn) or die(mysql_error()); /* redirect back to registration page */ header("Location: index.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133091-having-problems-posting-non-numeric-data/#findComment-692159 Share on other sites More sharing options...
spoco Posted November 17, 2008 Author Share Posted November 17, 2008 Thanks for your help. When I make those changes, nothing will write to the database now. And I'm very new - how and where do I turn on error reporting as you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/133091-having-problems-posting-non-numeric-data/#findComment-692181 Share on other sites More sharing options...
spoco Posted November 17, 2008 Author Share Posted November 17, 2008 So - now I have the text part working, but its not redirecting to index.php like it should. Quote Link to comment https://forums.phpfreaks.com/topic/133091-having-problems-posting-non-numeric-data/#findComment-692185 Share on other sites More sharing options...
rhodesa Posted November 17, 2008 Share Posted November 17, 2008 probably because whitespace has already been sent...try this: <?php //Turn on error reporting...you should remove this once the script is working error_reporting(E_ALL ^ E_NOTICE); ini_set('display_errors',1); //Connect to the DB require_once('databaseconn.php'); mysql_select_db('training', $databaseconn); $_1a = mysql_real_escape_string($_POST['1a']); $_1b = mysql_real_escape_string($_POST['1b']); $_1c = mysql_real_escape_string($_POST['1c']); $_2a = mysql_real_escape_string($_POST['2a']); $_2b = mysql_real_escape_string($_POST['2b']); $_2c = mysql_real_escape_string($_POST['2c']); $_3a = mysql_real_escape_string($_POST['3a']); $_4a = mysql_real_escape_string($_POST['4a']); $_5a = mysql_real_escape_string($_POST['5a']); $_6a = mysql_real_escape_string($_POST['6a']); $_7a = mysql_real_escape_string($_POST['7a']); $_8a = mysql_real_escape_string($_POST['8a']); $insertSQL = sprintf"(INSERT INTO evaluation (1a, 1b, 1c, 2a, 2b, 2c, 3a, 4a, 5a, 6a, 7a, 8a) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $_1a, $_1b, $_1c, $_2a, $_2b, $_2c, $_3a, $_4a, $_5a, $_6a, $_7a, $_8a); mysql_query($insertSQL, $databaseconn) or die(mysql_error()); /* redirect back to registration page */ header("Location: index.php"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/133091-having-problems-posting-non-numeric-data/#findComment-692188 Share on other sites More sharing options...
spoco Posted November 17, 2008 Author Share Posted November 17, 2008 I can't get your suggested code to work at all. here's my updated code that works right, sans the redirect. <?php require_once('databaseconn.php'); ?> <html> <head> <title>Class Register</title> <style type="text/css"> <?php $_1a = $_POST['1a']; $_1b = $_POST['1b']; $_1c = $_POST['1c']; $_2a = $_POST['2a']; $_2b = $_POST['2b']; $_2c = $_POST['2c']; $_3a = $_POST['3a']; $_4a = $_POST['4a']; $_5a = $_POST['5a']; $_6a = $_POST['6a']; $_7a = $_POST['7a']; $_8a = $_POST['8a']; mysql_select_db('training', $databaseconn); $insertSQL = sprintf("INSERT INTO evaluation (1a, 1b, 1c, 2a, 2b, 2c, 3a, 4a, 5a, 6a, 7a, 8a) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $_1a, $_1b, $_1c, $_2a, $_2b, $_2c, $_3a, $_4a, $_5a, $_6a, $_7a, $_8a); mysql_query($insertSQL, $databaseconn) or die(mysql_error()); /* redirect back to registration page */ $conf = "redirect.php"; header("Location: $conf"); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/133091-having-problems-posting-non-numeric-data/#findComment-692192 Share on other sites More sharing options...
rhodesa Posted November 17, 2008 Share Posted November 17, 2008 You can't do a Location redirect have you have sent stuff to the screen. And, if you are redirecting, there shouldn't be any HTML on the page anyways. and you didn't add the error reporting, which will tell you EXACTLY what the problem is: <?php //Turn on error reporting...you should remove this once the script is working error_reporting(E_ALL ^ E_NOTICE); ini_set('display_errors',1); require_once('databaseconn.php'); $_1a = $_POST['1a']; $_1b = $_POST['1b']; $_1c = $_POST['1c']; $_2a = $_POST['2a']; $_2b = $_POST['2b']; $_2c = $_POST['2c']; $_3a = $_POST['3a']; $_4a = $_POST['4a']; $_5a = $_POST['5a']; $_6a = $_POST['6a']; $_7a = $_POST['7a']; $_8a = $_POST['8a']; mysql_select_db('training', $databaseconn); $insertSQL = sprintf("INSERT INTO evaluation (1a, 1b, 1c, 2a, 2b, 2c, 3a, 4a, 5a, 6a, 7a, 8a) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $_1a, $_1b, $_1c, $_2a, $_2b, $_2c, $_3a, $_4a, $_5a, $_6a, $_7a, $_8a); mysql_query($insertSQL, $databaseconn) or die(mysql_error()); /* redirect back to registration page */ $conf = "redirect.php"; header("Location: $conf"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133091-having-problems-posting-non-numeric-data/#findComment-692197 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.