Re: Database/Email Script
- From: "Jerim79" <mylek@xxxxxxxxxxx>
- Date: 8 Nov 2006 11:21:09 -0800
Jerim79 wrote:
Jerim79 wrote:
Carl wrote:
Jerim79 wrote:
I am no PHP programmer. At my current job I made it known that I was no
PHP programmer during the interview. Still they have given me a script
to write with the understanding that it will take me a while (This
information is just for general knowledge as I don't want anyone
thinking I am trying to be dishonest with my intentions. Also, I do not
portray myself as something I am not. I am a beginner.)
Anyway, what the script needs to do is to take variables passed from an
HTML form and do two things. One is read it into a database. The other
is to send me an email with all of the customer's information.
//Name of the script is test.php
<?php
//I am using this echo command to make sure that the variables where
passed correctly.
echo $_Post['FName']; //Never displays
//Here is where the script for the database connections starts
$username='username';
$password='password';
$hostname='localhost';
$databasename='database';
//Here is where the database connection is actually made
$conection = mysql_connect($hostname, $username, $password);
mysql_select_db($databasename) or die ("Cannot connect to
database");
//With the database connection open, I start to insert the data from
the HTML form.
$result = mysql_query("INSERT INTO table() VALUES($FName,
$LName, $Company, $Title, $Address, $Apt, $City, $State, $Zip, $Phone,
$Fax, $Email, $Var1, $Var2, $Var3, $Var4, $Var5)");
//After reading the information into the table, we close the database
connection
mysql_close();
8 Message Cut >8
Hi Jerim,
A couple of things;
- $_Post is not the same as $_POST.
- When a function returns a value (as is the case with mysql_query()),
It is usually wise to check the value returned and make sure it is what
you expected.
- The mysql_error() function (http://php.net/mysql_error) is quite
useful when debugging these sorts of problems.
Hope that helps,
Carl.
I tried out POST instead of Post. I even set the method to POST. It
still won't even display the top echo $_POST['FName'] so I don't think
the variables are getting passed.
However, I had a question about the mail script. I keep getting this
error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING in /(website)/test.php
on line 270
Line 270 is this:
$message = "First Name: $_POST['FName']";
I believe the problem is the way in which I am adding the variable
$_POST['FName] to the line. I saw this method used somewhere: "First
Name: \"$_POST['FName']\""; but it didn't seem to work for me. I
haven't been able to find anything on point.
I was able to figure out the POST issue. If you use " in the name on
the HTML form, you have to use " in the PHP script. So $_POST['FName']
didn't work but $_POST["FName"] does. (I haven't seen this mentioned
anywhere.)
The other issue I am having, besides the email issue is the database
INSERT. Here is the code:
$result = mysql_query("INSERT INTO table() VALUES($FName, $LName,
$Company, $Title, $Address, $Apt, $City, $State, $Zip, $Phone, $Fax,
$Email, $Var1, $Var2, $Var3, $Var4, $Var5)")
I know that $FName isn't the proper way to do it. However, when I set
it to $_POST["FName"] I get this error:
Parse error: syntax error, unexpected '"', expecting T_STRING or
T_VARIABLE or T_NUM_STRING in /website/test.php on line 264
I did insert this command to show any database errors, but it doesn't
show any:
echo mysql_error($connection)
Okay, I found something that works. It probably isn't the best way, but
it works. At the beginning of the script I set each variable to a local
variable. Such as:
$FName=$_POST["FName"];
That may not be the best way to do it, but it works. The email script
is working great now. Which just leaves the MySQL connection. Here is
the code:
$username='username';
$password='password';
$hostname='localhost';
$databasename='database';
//Here is where the database connection is actually made
$conection = mysql_connect($hostname, $username, $password);
mysql_select_db($databasename) or die ("Cannot connect to
database" . msyql_error());
//This sets the query to a variable for easy calling
$query='INSERT INTO table() VALUES($FName,
$LName, $Company, $Title, $Address, $Apt, $City, $State, $Zip,
$Phone,
$Fax, $Email, $Var1, $Var2, $Var3, $Var4, $Var5)'
//With the database connection open, I insert the data using
$query
$result = mysql_query($query) or die ('Query failed: ' .
mysql_error());
//After reading the information into the table, we close the
database connection
mysql_close();
The error message I get:
Query failed: Unknown column '$FName' in 'field list'
If I enclose the variables inside the VALUES() part with quotations,
such as "$FName", "$LName","$Title" that data does get put into the
table with no error. Which is to say that $FName gets written to the
table, and not the data that $FName represents. So I know the database
connection is working and it is able to write. I tried defining the
columns in table() such as table(FNAME, LNAME, TITLE) with the same
error as above. I tried using $_POST[FName] in the VALUES() function
but it just returns a syntax error and tells me to check the manual for
my version of MySQL for the correct version. I am running 4.0.1 by the
way.
.
- Follow-Ups:
- Re: Database/Email Script
- From: Carl
- Re: Database/Email Script
- References:
- Database/Email Script
- From: Jerim79
- Re: Database/Email Script
- From: Carl
- Re: Database/Email Script
- From: Jerim79
- Re: Database/Email Script
- From: Jerim79
- Database/Email Script
- Prev by Date: Re: |OT| GG user ratings for c.l.php regulars
- Next by Date: Re: Database/Email Script
- Previous by thread: Re: Database/Email Script
- Next by thread: Re: Database/Email Script
- Index(es):
Relevant Pages
|