Page 1 of 1

Confused newbie... MySQL, PHP, forms, email, sessions...

Posted: Sat Feb 15, 2003 3:00 pm
by Sinemacula
With the help of some of you folks here, some digging through php.net, and a bunch of trial and error, I've managed to get most of the way through a little project I'm trying to put together.

*********Description of current situation**********
Here's what I've got:

1. an html form (form.html)
2. a php page (display.php)
3. another php page (mailit.php)
  • In form.html the user inputs all sorts of data and then hits SUBMIT... the form uses the POST function to go to display.php --

    display.php, using an "if (submit) == "Submit Info"";" statement then connects to the MySQL database and INSERTs the data.

    display.php also displays the data on a webpage according to my html formatting by using <?PHP echo $Fieldname; ?> statements.

    display.php also has an include line "include ('mailit.php');"

    mailit.php is the script that will email the form data, formatted with the same html and echo statements
So far so good... but I want to make a change to how this works, as I know that some people won't want html formatted email.

**********Description of what I want to change**********
Instead of automatically sending out an email via an "include" statement in display.php, I want to add links to display.php, giving people the choice of:
  • - no email
    - plain text email
    - html email
My first try was to take the "include (mailit.php);" statement out, and put in a link to mailit.php... this didn't work as the form data did not make it through to another round (I know this because the mailit.php script includes a "cc" to me... and I got the formatted email with no form data).

***********My thoughts/questions***********
I'm guessing, from what I've read, that I probably need to use sessions, and/or reconnect to the database to retrieve the form data previously inserted.
Can I use one of the variables I collected through the form as my session ID? If so, how do I use this?
I've tried the following:

In display.php I've added:

Code: Select all

session_start(); 
$_SESSION&#1111;"sessCust_Email"] = $Cust_Email;
Then, in mailit.php I've added:

Code: Select all

// Open connection to the database 
$connection = mysql_connect("localhost", "root", "xxxx") or die("Failure to communicate with database"); 
mysql_select_db('trimergence_free', $connection) or die(mysql_error()); 

//set up the query 
$query = "SELECT * FROM trimerguserdata 
WHERE Cust_Email='$_SESSION' "; 


//run the query and get the number of affected rows 
$result = mysql_query($query, $connection) or die('error making query'); 
$affected_rows = mysql_num_rows($result); 
$Cust_Name =  $affected_rows&#1111;"Cust_Name"];
$Cust_Email = $affected_rows&#1111;"Cust_Email"];
$Q1 = $affected_rows&#1111;"Q1"];
$Q2 = $affected_rows&#1111;"Q2"];
$Q3 = $affected_rows&#1111;"Q3"];
$Q4 = $affected_rows&#1111;"Q4"];
$Q5 = $affected_rows&#1111;"Q5"];
$Q6 = $affected_rows&#1111;"Q6"];
$Q7 = $affected_rows&#1111;"Q7"];
$Q8 = $affected_rows&#1111;"Q8"];
$Q9 = $affected_rows&#1111;"Q9"];
$Q10 = $affected_rows&#1111;"Q10"];
$Q11 = $affected_rows&#1111;"Q11"];
$Q12 = $affected_rows&#1111;"Q12"];
Doesn't work... am I way off? :?

Thanks,
Scott

Posted: Mon Feb 17, 2003 2:03 am
by twigletmac
You're not way off, just slightly skew...

Try changing this:

Code: Select all

//set up the query 
$query = "SELECT * FROM trimerguserdata 
WHERE Cust_Email='$_SESSION' ";
to

Code: Select all

//set up the query 
$query = "SELECT * FROM trimerguserdata 
WHERE Cust_Email='{$_SESSION['sessCust_Email']}'";
and this

Code: Select all

//run the query and get the number of affected rows 
$result = mysql_query($query, $connection) or die('error making query'); 
$affected_rows = mysql_num_rows($result); 
$Cust_Name =  $affected_rows["Cust_Name"]; 
$Cust_Email = $affected_rows["Cust_Email"]; 
$Q1 = $affected_rows["Q1"]; 
$Q2 = $affected_rows["Q2"]; 
$Q3 = $affected_rows["Q3"]; 
$Q4 = $affected_rows["Q4"]; 
$Q5 = $affected_rows["Q5"]; 
$Q6 = $affected_rows["Q6"]; 
$Q7 = $affected_rows["Q7"]; 
$Q8 = $affected_rows["Q8"]; 
$Q9 = $affected_rows["Q9"]; 
$Q10 = $affected_rows["Q10"]; 
$Q11 = $affected_rows["Q11"]; 
$Q12 = $affected_rows["Q12"];
to this:

Code: Select all

//run the query and get the number of affected rows 
$result = mysql_query($query, $connection) or die('error making query'); 
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
	$row = mysql_fetch_assoc($result);
	$Cust_Name = $row['Cust_Name']; 
	$Cust_Email = $row['Cust_Email']; 
	$Q1 = $row['Q1']; 
	$Q2 = $row['Q2']; 
	$Q3 = $row['Q3']; 
	$Q4 = $row['Q4']; 
	$Q5 = $row['Q5']; 
	$Q6 = $row['Q6']; 
	$Q7 = $row['Q7']; 
	$Q8 = $row['Q8']; 
	$Q9 = $row['Q9']; 
	$Q10 = $row['Q10']; 
	$Q11 = $row['Q11']; 
	$Q12 = $row['Q12'];
} else {
	echo 'Customer does not exist';
}
You'll also need a call to session_start() at the top of mailit.php in order to use session variables within it.

Have a look at:
http://www.php.net/manual/en/ref.mysql.php
http://www.php.net/manual/en/function.m ... -assoc.php
http://www.php.net/manual/en/language.types.array.php

Mac

parse error...

Posted: Mon Feb 17, 2003 4:24 pm
by Sinemacula
Thanks Mac...

I think I understand a little better now... but I'm getting a parse error at the following line:

Code: Select all

$row = mysql_fetch_assoc($result);
I've looked at the relevant pages on php.net, but that didn't help.

Any suggestions?

Thanks,
Scott

Some changes... still a parse error

Posted: Mon Feb 17, 2003 5:02 pm
by Sinemacula
I've done a little more mucking about, based on what I've read on the php.net site... for example, trying:

Code: Select all

$row = mysql_fetch_array($result, MYSQL_ASSOC);
in place of

Code: Select all

$row = mysql_fetch_assoc($result);
in case my server needed what appears to be the "old" way based on what I read... but it still didn't work...

However, the parse error changed, in terms of which line was getting the error... and when I changed the code back to "mysql_fetch_assoc" the parse error stayed on the "new" line.

So, now I'm getting a parse error on the line:

Code: Select all

$Cust_Name = $row&#1111;'Cust_Name'];
which confuses me a little, because if there had been a problem connecting to the database, or finding the record, wouldn't I have received a different error message based on the code being used (e.g. a "failure to communicate..." message, or "Customer does not exist" message?

Scott

Posted: Tue Feb 18, 2003 2:03 am
by twigletmac
Parse errors occur when PHP can no longer go through the code because the syntax is incorrect. Often it is because there is a missing parenthesis, semi-colon or something like that. It also tends to be occuring on a line above where it reporting the error - this is because the line it reports is where it has to stop, it may be able to keep going for a few lines after the error depending on what it is.

Could you post your code - a few lines above and below where the error is occuring - so that we can help debug.

Mac

Got it...

Posted: Wed Feb 19, 2003 11:45 am
by Sinemacula
Thanks again for your help, Mac. I managed to figure it out... seems I had left out an underscore in the previous line :oops: (at least, I think that's what did it -- I did a fair bit of messing around with it - best way for me to learn I guess).

Scott