By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,037 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,037 IT Pros & Developers. It's quick & easy.

Not able to insert to db from form in production

P: 97
I'm trying to get a web page working so that visitors to the website can leave reviews of the business, or read reviews others have left. But I'm having trouble getting it working on the production server, even though it worked in development.

Here's the code I'm using to inert the reviews into the database;

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbc = mysql_connect('localhost', 'communi3_root', 'NotTheRealPassword');
  3. mysql_select_db('communi3_cfds',$dbc);
  4. $monicker = $_POST['monicker'];
  5. review = $_POST['review'];
  6.  
  7. $query = "INSERT INTO reviews 
  8. (monicker, review) 
  9. VALUES ('$monicker', '$review' );";
  10.  
  11. if (mysql_query($query, $dbc))
  12. {
  13. print '<p>The review has been submitted!</p>';
  14. } else 
  15. {
  16. print '<p style="color: red;">Could not add the review because:<br />' .mysql_error($dbc) . '</p>';
  17. }
  18. ?>
  19.  
I know that this is an old version of MySQL but the admin at the hosting company has already changed the version on the server and I don't mind using it as long as it works (even though it doesn't). I also know that I should be using an error log rather than printing error messages to the screen, but I'm not sure how to access the php.ini file to set that up.

Basically, I haven't got much experience in this kind of thing (should be only too obvious from the amateurish code) but I'd really like to get this working. I had it working in production but it just won't work in development. I got it to the point where it looked ok, but when I tested it, the reviews were not being stored in the database. I also have code to display the reviews when a user clicks a button, but that seems to be fine.
May 14 '15 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,035
You should check if your connection is setup correctly.

You should to this by checking the value of $dbc, after line#2

see the stuff about return values in the docs: MySQL_connect
May 14 '15 #2

P: 97
I think I've finally got it going. The connection was ok but I didn't have the id set in the INSERT statement, also I hadn't used an ISSET statement, so when I got the first problem fixed I had to add the ISSET so that a blank entry wouldn't be added to the database on page load. Anyway final code is:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbc = mysql_connect('localhost', 'DB_NAME', 'NotRealPassWord');
  3. mysql_select_db('communi3_cfds',$dbc);
  4. if(isset($_POST['submit'])) {
  5. $monicker = mysql_real_escape_string ($_POST['monicker']);
  6. $review = mysql_real_escape_string ($_POST['review']);
  7.  
  8. $query = "INSERT INTO reviews 
  9. (id, monicker, review, date) 
  10. VALUES (DEFAULT,'$monicker', '$review', 'CURDATE()');";
  11. }
  12. ?>
  13.  
May 15 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.