?
Solved

Trying to post to a MySQL DB using PHP

Posted on 2002-04-14
4
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 1200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 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