Solved

Trying to post to a MySQL DB using PHP

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…

679 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