Page 1 of 1

Need help with code

Posted: Mon Feb 22, 2010 8:18 am
by gjcluttrell
Can someone take a look at this code & tell me why it’s not working? I’m VERY new to php & I know there are mistakes so if you find something in there that don’t belong please let me know or if something is needed to be added please let me know.
I need this code to do the following; a user goes to this page & chooses their office and name from the drop down boxes then clicks the submit button. Then the code will look at the mysql database & send the all the entries in the table column named hours in an email, display it in a browser, or even export it to an excel spreadsheet to view….it doesn’t matter to me, I just want the users to see all the data in the hours table column.
Thanks I've added the code below:



<?php

$hostname = "localhost";
$db_user = "root";
$db_password = "password";
$database = "mydatabase";
$db_table = "mytable";

# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>
<html>
<body bgcolor="ruble">
<head>
<title>Training Database</title>
</head>
<body>

<?php

#####################################################################

switch($_REQUEST['supervisor']) {

case "John Doe":
$email = "johndoe@yahoo.com";
break;
case "Jane Doe":
$email = "janedoe@yahoo.com";
break;
default:
echo "Error, no supervisor selected!!!";
break;
}

$message = "office : {$_REQUEST['office']}\nsupervisor : {$_REQUEST['supervisor']}\nname : {$_REQUEST['name']}]}";


if (mail($email, "Subject", $message, "Message")) {

echo "some message here";

} else {

echo "This system is not working properly.";

}

##########################################################################

echo '<h1>Thank you</h1>Your information has been entered into our database<br><br> Please remember support staff are required to have a minimum of 20 hours per.';
echo '<p>To check your training hours <a href="http://TNEP-G-NETADMIN/FREPORT.PHP">click here</a>.<br><br> To enter more training <a href="http://tnep-g-netadmin/greg.php">click here</a>. </p>';
echo '<p> To return to our home page <a href="http://156.125.44.115/tnep">click here</a>.</p>';


} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<h1>Training Database</h1>By <a href="http://www.google.com">Training Database</a><hr>
<form method="post" action="">
<b>Your Office:<br>
<select name="office">
<option value=""></option>
<option value="Office A">Office A</option>
<option value="Office B">Office B</option>
<option value="Office C">Office C</option>
</select>
</br>
</br>
</br>

<b>Your Name:<br> </b>
<select name="name">
<option value=""></option>
<option value="John Doe">John Doe</option>
<option value="Jane Doe">Jane Doe</option>
</select>
</br>
</br>
</br>

<input type="submit" name="Submit" value="Submit">

</form>

<?php
}
?>
</body>
</html>

Re: Need help with code

Posted: Mon Feb 22, 2010 9:32 am
by mikosiko
please allow me to give you a couple advices:

- If I were in your position I'll apply the old say "divide to conquer".. I will write the code in this way:
* first only the form that capture the data
* the submit of that form will call a "processthis.php" (or whatever name you want).
* In the processthis.php I will validate and sanitize the user input... after that if all the inputs are valid then:
* login in database, select the data and display it on screen (for simplicity)

- Having everything in the same php make the code more difficult to maintain and for you to understand what is going wrong.

- In your code check for this lines:
} else {
echo "ERROR: ".mysql_error();
}
} else {


those lines are orphans and causing your code to fail. After you comment or remove those lines you will see how your code is behaving :wink: (Not that good isn't?) ... don't worry... you can make it better.

Miko