469,071 Members | 1,988 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,071 developers. It's quick & easy.

Not able to insert to db from form in production

97 64KB
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'];
  7. $query = "INSERT INTO reviews 
  8. (monicker, review) 
  9. VALUES ('$monicker', '$review' );";
  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. ?>
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
2 1448
1,043 Expert 1GB
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
97 64KB
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']);
  8. $query = "INSERT INTO reviews 
  9. (id, monicker, review, date) 
  10. VALUES (DEFAULT,'$monicker', '$review', 'CURDATE()');";
  11. }
  12. ?>
May 15 '15 #3

Post your reply

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

Similar topics

2 posts views Thread by EricRobineau | last post: by
6 posts views Thread by Sean | last post: by
1 post views Thread by wizardry | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.