Solved

Trying to post to a MySQL DB using PHP

Posted on 2002-04-14
4
211 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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…

734 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