Click to See Complete Forum and Search --> : Inserting data using php/mySQL


rbragg
07-21-2006, 04:29 PM
I am revisiting mySQL after a few years and it looks as though I'm a bit rusty. My php variables carry over fine to a confirmation page. Upon the next submission, a new record is added to the database but it is completely empty.

<?php
include 'db_connect.php';

$query_insert = "INSERT INTO healthed_workshop (date, userid, fname, lname, phone, email, org, subject, other, dlMonth,
dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)".
"VALUES (null, '".$_SESSION['id']."', '".$fname."', '".$lname."', '".$phone."', '".$email."', '".$org."', '".$subject."', '".$other."', '".$dlMonth."', '".$dlYear."', '".$dlTime1."', '".$dlTime2."', '".$location."', '".$attend."', '".$cbItems."', '".$taAdd."')";

mysql_query($query_insert) or die(mysql_error());

mysql_close($link);
?>

Am I missing something simple or is there any more information that you need to help with my dilemma? Thanks in advance!

chazzy
07-21-2006, 05:09 PM
well let's see what your query looks like..


?php
include 'db_connect.php';

$query_insert = "INSERT INTO healthed_workshop (date, userid, fname, lname, phone, email, org, subject, other, dlMonth,
dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)".
"VALUES (null, '".$_SESSION['id']."', '".$fname."', '".$lname."', '".$phone."', '".$email."', '".$org."', '".$subject."', '".$other."', '".$dlMonth."', '".$dlYear."', '".$dlTime1."', '".$dlTime2."', '".$location."', '".$attend."', '".$cbItems."', '".$taAdd."')";
echo $query_insert."<br>\n";
mysql_query($query_insert) or die(mysql_error());

mysql_close($link);
?>

NogDog
07-21-2006, 05:09 PM
My best guess with the limited information is that register_globals is (correctly) turned off, so you need to reference your form variables from the $_POST array, e.g. $_POST['fname'] instead of $fname.

rbragg
07-24-2006, 09:22 AM
Sorry it's taken so long to reply. I used $_POST['fname'] before to no avail. Just to be sure I did this again and got the same result. If it will help, here is a little about my database structure:

I am trying to enter the variables into just one table called healthed_workshop using the latin1 collation. Of course, it connects fine because I get a new record even though it is empty.

ws_num - int (11) - auto increment - INDEX
date - datetime - null
userid - text - null
fname - tinytext
lname - tinytext
phone - varchar (12)
email - tinytext
org - tinytext
subject - tinytext
other - text
dlMonth - tinytext
dlDay - tinytext
dlYear - year (4)
dlTime1 - var (5)
dlTime2 - tinytext
location - tinytext
attend - int (11)
cbItems - text
taAdd - text

rbragg
07-24-2006, 09:27 AM
Thanks for your reply Chazzy. The echo on my last page produces this:

Thank you for submitting a presentation request. Connected successfully! INSERT INTO healthed_workshop (date, userid, fname, lname, phone, email, org, subject, other, dlMonth, dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)VALUES (null, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')

chazzy
07-24-2006, 01:26 PM
so then that means that none of the values you are listing there are actually being set. where are you actually defining them?

rbragg
07-24-2006, 01:43 PM
I noticed that they were not being sent and so I thought that NogDog's solution of including the $_POST array could be the solution, right?

I define them on the previous page (confirmation page), where I am echoing what the user has entered. However, I am not using session variables so do I need to also define the variables on this last page?

chazzy
07-24-2006, 04:13 PM
yes, if you put them in the session you must reference them via session.
but it doesn't look like your sessions are working either (based on that field being empty too)

rbragg
07-24-2006, 04:20 PM
I think I was confusing myself and you ;) I'm not using sessions at all - I took out the reference attached to the userid. Please consider the following without sessions:

------------
include 'db_connect.php';

$query_insert = "INSERT INTO healthed_workshop (date, fname, lname, phone, email, org, subject, other, dlMonth, dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)".
"VALUES (null, '".$fname."', '".$lname."', '".$phone."', '".$email."', '".$org."',
'".$subject."', '".$other."', '".$dlMonth."', '".$dlYear."', '".$dlTime1."', '".$dlTime2."', '".$location."', '".$attend."', '".$cbItems."', '".$taAdd."')";

echo $query_insert."<br>\n";

mysql_query($query_insert) or die(mysql_error());

mysql_close;
--------------

The echoed query still gives me this:

Connected successfully! INSERT INTO healthed_workshop (date, fname, lname, phone, email, org, subject, other, dlMonth, dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)VALUES (null, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')

chazzy
07-24-2006, 05:48 PM
and again i ask you, where is $fname and all that being set?

rbragg
07-24-2006, 07:38 PM
I touched on that above ... that they are being set on the previous page (confirmation page).

This is the order of my project: the user enters data on a form. Upon submit, that data is successfully passed to a confirmation page via php. When the "submit" button is pushed on this confirmation page, the user is sent to a results page where a thank you message is displayed. This results page contains the mySQL code to connect and insert. So, the variables are set in the confirmation page and not on my results (last) page where the connection to the db is made. However, I have tried to define them on this page before, to no avail. :confused:

NogDog
07-24-2006, 07:43 PM
Variables are not "remembered" from one script to the next. You'll either need to use sessions, or else add them to form fields (possibly "hidden" types) that are submitted when you click the confirmation button so that you can access them via the $_POST array.

rbragg
07-25-2006, 09:44 AM
I got my data to pass and insert.

On my confirmation page (2nd page of 3) I put this:

-----------------------
<!-- CONTACT -->
<tr><td align="left" valign="top">
<span class="style3">Contact</span><table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td width="25%" height="20" class="style1">
<label>First Name:</label></td>
<td width="75%" height="20" class="style11">
<?php
$fname = $_POST['fname'];
echo "$fname";
?>
<input type='hidden' name='fname' value='<?php echo $_POST['fname'];?>'>
</td>
</tr>
----------------------

and so on. This 2nd form is submitted with its hidden fields to the results page (3rd page) where I have this:

----------------------
Thank you for submitting a presentation request.

<?php
include 'db_connect.php';

$query_insert = "INSERT INTO healthed_workshop (date, fname, lname, phone, email, org, dlSubject,
other, dlMonth, dlDay, dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)".
"VALUES (null, '".$_POST['fname']."', '".$_POST['lname']."', '".$_POST['phone']."', '".$_POST['email']."',
'".$_POST['org']."', '".$_POST['dlSubject']."', '".$_POST['other']."', '".$_POST['dlMonth']."',
'".$_POST['dlDay']."', '".$_POST['dlYear']."', '".$_POST['dlTime1']."', '".$_POST['dlTime2']."', '".$_POST['location']."',
'".$_POST['attend']."', '".$_POST['cbItems']."', '".$_POST['taAdd']."')";

echo "<br>\n".$query_insert;

mysql_query($query_insert) or die(mysql_error());

mysql_close;
?>
----------------------

Thanks to you both b/c your help led me in the right direction. I hope you will be available if I have more question concerning this project. :D