Raf1k1 Posted February 21, 2010 Share Posted February 21, 2010 Hey, I'm having trouble getting information from my form and inserting it into a MySQL database. This is my form. (I wanted to do something really simple first to make sure it works before trying it on a more complicated form) <form method="post" action="process.php"> Name:<br> <input type="text" name="user_name"> <br> Email: <br> <input type="text" name="user_email"> <br><br> <input type="submit" name="Submit" value="Submit"> </form> my process.php code <?php require("./connection.php"); $user_name = check_input($_POST['user_name']); $user_email = check_input($_POST['user_email']); function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <html> <body> <?php $sql = "INSERT INTO user_info (user_name, user_email) VALUES ('{$user_name}', '{$user_email}')"; if($result = mysql_query($sql) { echo '<h1>Thank you</h1>Your information has been entered into our database'; } else { echo "ERROR: ".mysql_error(); } ?> </body> </html> Can anyone help with this? I'd much appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/192821-php-form-insert-into-mysql-db/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 21, 2010 Share Posted February 21, 2010 Since you did not provide any information as to what it does do when you tried it, no one can directly help you with the dozen possible things that could prevent it from working on your server. What result did you see in front of you when you submitted the form? Quote Link to comment https://forums.phpfreaks.com/topic/192821-php-form-insert-into-mysql-db/#findComment-1015690 Share on other sites More sharing options...
Raf1k1 Posted February 21, 2010 Author Share Posted February 21, 2010 Sorry, it was originally just giving me a blank page and nothing was going into the table in the database. Sorry for the late reply was kind of expecting an email so I knew when I got a response but it seems I forgot to tick the box for that. Anyway, I've got it working now with some googling and changing of code but I had to replace "require("./connection.php");" with some script that connects to the database in the page itself. Any idea as to why it would work without "require("./connection.php");"? It works on my other pages. Here's the code from the connection.php file: <?php // Connect to the database $conn = new MySQLi("localhost", "username", "pass", "dbname") or die('Could not connect: ' . mysql_error()); ?> edit: Here's what I', using now: <?php $username="username"; $password="pass"; $database="dbname"; //create shorthand for form data $game_id = $_REQUEST['game_id']; $review_title = $_REQUEST['review_title']; $review_text = $_REQUEST['review_text']; $graphics = $_REQUEST['graphics']; $gameplay = $_REQUEST['gameplay']; $sound = $_REQUEST['sound']; $replayability = $_REQUEST['replayability']; $pros = $_REQUEST['pros']; $cons = $_REQUEST['cons']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO xreview VALUES('', '$game_id','$review_title', '$review_text', '$graphics','$gameplay','$sound','$replayability','$pros','$cons')"; $result = MYSQL_QUERY($query); mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/192821-php-form-insert-into-mysql-db/#findComment-1015732 Share on other sites More sharing options...
PFMaBiSmAd Posted February 21, 2010 Share Posted February 21, 2010 The path to "./connection.php" must be correct so that require() can find it, otherwise require() halts program execution, resulting in a blank page from that point onward. Quote Link to comment https://forums.phpfreaks.com/topic/192821-php-form-insert-into-mysql-db/#findComment-1015738 Share on other sites More sharing options...
Raf1k1 Posted February 21, 2010 Author Share Posted February 21, 2010 I've made sure that it's in the directory that I've told it to look in but now I'm getting a page. However, now I'm getting the page but nothing is going into the database. I am able to echo out the information contained in the variables that should be going into the database so the information is getting into the variables it's just not going into the table I need it to go in. I don't really see much of a difference between the connection.php file: // Connect to the database $conn = new MySQLi("localhost", "kahmed14", "240985", "kahmed14") or die('Could not connect: ' . mysql_error()); and mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); They're both doing the same thing aren't they? Quote Link to comment https://forums.phpfreaks.com/topic/192821-php-form-insert-into-mysql-db/#findComment-1015740 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.