Solved

Trying to post to a MySQL DB using PHP

Posted on 2002-04-14
4
206 Views
Last Modified: 2010-05-18
I have a form that calls a php file that has this segment in it.  I do not get an error at all. But my data is not posted to the DB.  I can, however, enter data directly into the DB from the PHP administrator tool.  I am using this from "hypermart.net" and have no control over how it was set up.  I don't know if the problem is my form or what. I am pretty new at PHP and am not sure how to test to see if my variables are being correctly passed from my other form.  I have several books but just haven't had time to read them completely - I have just skimmed pertinent chapters and haven't found an answer to my question yet.  This project is a survey (very simple) and after I get it working, I will have to really spend learning it.  Thanks for any help.

(fyi - I removed the blank lines and took out my real localhost, uname and password)

<?php
 @ $db = mysql_connect("localhost", "uname", "password");
 if (!$db)
     {
          echo "Error: Can not get to the database: Try again later.";
          }
  mysql_select_db("cynmain",$db);
  $sql = "INSERT INTO tblEval (Class, Email, Engage, Distance, Affirm, Confuse, Surprise) VALUES ('$Class','$Email','$Engage','$Distance','$Affirm','$Confuse','$Surprise','$Comment')";
  $result = mysql_db_query($sql);
echo mysql_affected_rows()."Thank you! Information entered.\n";
?>
0
Comment
Question by:cyndeec
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 300 total points
Comment Utility
You have two errors:

1) The first one is in the query, you specify 7 fields but you are supplying 8 values.

$sql = "INSERT INTO tblEval (Class, Email, Engage, Distance, Affirm, Confuse, Surprise) VALUES ('$Class','$Email','$Engage','$Distance','$Affirm','$Confuse','$Surprise','$Comment')";


You need to add 'Comment' to the field names:

$sql = "INSERT INTO tblEval (Class, Email, Engage, Distance, Affirm, Confuse, Surprise, Comment) VALUES

The second on is the the mysql_db_query($sql) statement, when you use this statement you must specify the 'db name':

You have:

 $result = mysql_db_query($sql);

Should be:

 $result = mysql_db_query("dbName",$sql);

Since you are using mysql_select_db you can use mysql_query instead which doesn't require the db name:

 $result = mysql_query($sql);

One last thing, to get an error message from the database to help you find the problems use the mysql_error() function.

if(!($result = mysql_query($sql)))
{
//If error in query print error message
echo mysql_error();

//Then terminate program
exit;
}
0
 

Author Comment

by:cyndeec
Comment Utility
Thank you so much.  I think the error was actually the second one because I never did put my db name on my result statement.  None of my examples showed me that (at least not the ones I looked at).  I did have my fields matching my values at one point in time (not this time obviously).  Also thank you for the hint on displaying errors. That will come in handy!
Again - you have saved my sanity! :)
0
 

Author Comment

by:cyndeec
Comment Utility
Thank you so much.  I think the error was actually the second one because I never did put my db name on my result statement.  None of my examples showed me that (at least not the ones I looked at).  I did have my fields matching my values at one point in time (not this time obviously).  Also thank you for the hint on displaying errors. That will come in handy!
Again - you have saved my sanity! :)
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
No problem, glad I was able to help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now