Solved

Trying to post to a MySQL DB using PHP

Posted on 2002-04-14
4
207 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
ID: 6940972
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
ID: 6940994
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
ID: 6940996
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
ID: 6941014
No problem, glad I was able to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

948 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

20 Experts available now in Live!

Get 1:1 Help Now