Forum Moderators: coopster

Message Too Old, No Replies

values not passing to mySQL db

connect, post to MySQL db

         

saint_ryan

10:43 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



I am trying to build a database for cancer trial patients and am having problems.
Granted, it's a long string ~26 values collected from the html form but I believe it's a syntax error that gets me stuck. I always get a "success" message (line 46) but when I check the db, no values appear for this table. I've played around with the INSERT string with and without the '$_POST [value]' format but no luck. Any help would be appreciated.
Here's my whole PHP script.

<?php
$user="user";
$pass="pass";
$db="database_name";
$first_name=$_POST['first_name'];
$last_name =$_POST['last_name'];
$MRN=$_POST['MRN'];
$sex=$_POST['sex'];
$height=$_POST['height'];
$weight=$_POST['weight'];
$BSA=$_POST['BSA'];
$race=$_POST['race'];
$age=$_POST['age'];
$hospital=$_POST['hospital'];
$AMO=$_POST['AMO'];
$dxdate=$_POST['dxdate'];
$prior_chemo=$_POST['prior_chemo'];
$chemoline=$_POST['chemoline'];
$chemotype=$_POST['chemotype'];
$mets=$_POST['mets'];
$crc_trial=$_POST['crc_trial'];
$total_cycles=$_POST['total_cycles'];
$chemo_response=$_POST['chemo_response'];
$survival=$_POST['survival'];
$ecog=$_POST['ecog'];
$progress_time=$_POST['progress_time'];
$myelo_tox=$_POST['myelo_tox'];
$gastro_tox=$_POST['gastro_tox'];
$neuro_tox=$_POST['neuro_tox'];

// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'username', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}

// Select the patient database
if (!@mysql_select_db('db_url')) {
exit('<p>Unable to locate the CRC ' .
'database at this time.</p>');
}

$query = "INSERT INTO patient_information (patient_id, first_name, last_name, MRN, sex, height, weight, BSA, race, age, hospital, AMO, dxdate, prior_chemo, chemoline, chemotype, mets, crc_trial, total_cycles, chemo_response, survival, ecog, progress_time, myelo_tox, gastro_tox, neuro_tox) VALUES ('','$_POST[last_name]','$_POST[last_name]','$_POST[MRN]','$_POST[sex]','$_POST[height]','$_POST[weight]', '$_POST[BSA]', '$_POST[race]','$_POST[age]','$_POST[hospital]', '$POST_[AMO]', '$POST_[dxdate]', '$_POST[prior_chemo]', '$_POST[chemoline]', '$_POST[chemotype]', '$_POST[mets]', '$_POST[crc_trial]', '$_POST[total_cycles]', '$_POST[chemo_response]', '$_POST[survival]', '$_POST[ecog]', '$_POST[progress_time]', '$_POST[myelo_tox]', '$_POST[gastro_tox]', '$_POST[neuro_tox]')";

echo "success in database entry.";

echo "<br />";
echo "<a href=\"add_patient.html\">Click here to return to the form page.</a>";
mysql_close();
?>

jatar_k

10:58 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld saint_ryan,

I don't see anywhere that the query is actually sent to mysql.

after your
$query = "INSERT INTO patient_information...

you need to do a mysql_query like so

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

the second part will give you the actual error from mysql if there is a problem.

While we are looking at inserting user input values into a database, you need to do a little more "cleaning" on that user input.

testing to make sure the values from the forms are of the proper type and escaping the values for insertion into mysql.

take a look at mysql_real_escape_string() [php.net] as well as ctype functions [php.net]

saint_ryan

12:29 am on Jan 18, 2006 (gmt 0)

10+ Year Member



Thank you! I shall try this now.

saint_ryan

12:58 am on Jan 18, 2006 (gmt 0)

10+ Year Member



That did the trick! But the records aren't getting updated as they don't automatically increment. The error I get is: "Duplicate entry '' for key 1" I assumed that unless the unique identifier was defined (in this case a row named "patient_id"), MySQL would automatically insert submitted data into the next row.

Googling where I went wrong now...but any help is greatly appreciated!

saint_ryan

3:01 am on Jan 18, 2006 (gmt 0)

10+ Year Member



D'Oh! Not in the script, but a value inherent to the table (or more exactly, the row). INT and auto_increment did the trick.

On to the next.

jatar_k

3:27 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



perfect, that would have been my guess ;)