FreedomandDemocrazy Posted September 3, 2011 Share Posted September 3, 2011 Greetz, I am trying to write a simple newsletter sign up form using HTML FORM + PHP + MySQL. I seem to be having a problem getting it working and I suspect the issue is in my PHP code and was wondering if someone could take a look at it for me to see if they can spot any obvious mistakes. <?php $link = mysql_connect('localhost', 'testuser', 'pass1'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_select_db("testdb", $link); $sql="INSERT INTO newsletters(mens) VALUES ('$_POST[e-mail]')"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } echo "Done!"; mysql_close($link); ?> Note: My SQL Database is named "testdb", I have a table called "newsletters" and a field called "mens". Kind regards. Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/ Share on other sites More sharing options...
voip03 Posted September 3, 2011 Share Posted September 3, 2011 Is your DB Connected successfully? your code $sql="INSERT INTO newsletters(mens)VALUES('$_POST[e-mail]')"; change to $sql="INSERT INTO newsletters(mens) VALUES ('".$_POST[e-mail]."')"; PS: Please use the code tag next time. Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265067 Share on other sites More sharing options...
FreedomandDemocrazy Posted September 3, 2011 Author Share Posted September 3, 2011 That's a good point... I'm not sure if its connecting correctly. How can I check? I tried your line of code, but it didn't work.. but this could be because I am not connected. Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265070 Share on other sites More sharing options...
Pikachu2000 Posted September 3, 2011 Share Posted September 3, 2011 There was really nothing wrong with the way you had it to start with. When you start concatenating variables into strings when it isn't necessary, all you do is introduce a greater number of chances to make a typo. The more common way to write it would be to leave array indices that are strings quoted, and use complex notation. $sql="INSERT INTO newsletters(mens)VALUES('{$_POST['e-mail']}')"; Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265071 Share on other sites More sharing options...
FreedomandDemocrazy Posted September 3, 2011 Author Share Posted September 3, 2011 I see. I tried your line Pikachu, but it didn't work either. Perhaps the problem is else where - perhaps PHP is not connecting to the database.. How would I check this? Note: I'm not getting "Connected Successfully". Note: I'm new to PHP/SQL.. sorry. Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265072 Share on other sites More sharing options...
Pikachu2000 Posted September 3, 2011 Share Posted September 3, 2011 You should be getting the "Connected Successfully" message regardless of whether the connection is successful or not. What are you getting? Anything at all? Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265076 Share on other sites More sharing options...
FreedomandDemocrazy Posted September 3, 2011 Author Share Posted September 3, 2011 I wasn't getting anything. Error log showed this: PHP Fatal error: Call to undefined function mysql_connect() Google led me to someone who said you need the PHP MySQL module installed. Problem might be found. Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265077 Share on other sites More sharing options...
FreedomandDemocrazy Posted September 3, 2011 Author Share Posted September 3, 2011 Solved. Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265078 Share on other sites More sharing options...
Pikachu2000 Posted September 3, 2011 Share Posted September 3, 2011 Just noticed you have a hyphen (AKA subtraction operator) in the value of an array key. Although it will usually work, it can have some unexpected results depending on how it's quoted. You'd be better off to omit it or use an underscore instead. Quote Link to comment https://forums.phpfreaks.com/topic/246349-html-from-php-mysql-fault-suspected-fault-in-php/#findComment-1265081 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.