View Full Version : Help a beginner
Dirty Harry
09-20-2012, 10:10 AM
Howzit
I have been browsing the site and found quite a few helpful scripts which I played around with, however I have not been able to find what I need.
I am trying to write a report which I think includes more than just PHP.
I need to build a report where a user can select two dates,so one of those fancy calendar pop up thingys, and once done, will need to reference a mysql database which will then show those records between those dates.
Eg, I choose between 10-08-2012 and 20-09-2012,when I click submit or search it will then search the respective 'datecolumn' in table a,match up 'id_column' in table a to 'id_column' in table b and show the 'account_number' from table b which corresponds to the date/s selected from the date prompts mentioned earlier.
If I am not making sense please say so.
If I need to build more than one page I will do so,I have been struggling with this for a week now,might be easy for others but being inexperienced I would know where to start.
I would imagine I need to use more than one language here.
Any help will be greatly appreciated.
Cheers
Harry
keyboard
09-20-2012, 10:13 AM
So, what are you actually asking for help with?
Dirty Harry
09-20-2012, 10:19 AM
I need help to get the scripting correct for it to work and to get it together. All I have at the moment is a script that references mysql and that gives me all the data from the fields selected which is what I mentioned in my original post. But I highly doubt that that script will be at all usefull.
I have an idea of how it should work but dont know how to put it together and how to get each one to link up and reference the right inf
bernie1227
09-20-2012, 10:26 AM
I'd assume that you would just get javascript calendar, and then the content of each thing would be whatever in the database had the corresponding date.
Dirty Harry
09-20-2012, 10:34 AM
Yeah that's how I thought it to work, but I don't know how to put it together,that's what I'm struggling with. There are javascript calendars on google which work,but I need two to choose from,so a 'start date' and an 'end date'.Then from those values choose respective information from the database.
CLIENT (user):
1. pick a start date and end date, using a pop-up calendar (as bernie said, javascript).
2. send the selected dates to the server (hinthint, you need a <form> now).
SERVER (website):
1. check for form submission.
2. read submitted values.
3. validate that they *are* dates, and in the proper format.
4. connect to database.
5. query database.
6. retrieve resultset.
7. parse resultset and write html to display results.
8. send html result to client.
CLIENT:
1. read results; possibly send another request.
beyond that,
Your question is unclear.
Please provide more information, and be as specific as possible.
What do you want to accomplish? What have you already tried? What problems did you encounter?
Also, please be sure to share all relevant code.
Dirty Harry
09-21-2012, 07:04 AM
Yeas thats it traq.
ok to answer the questions,
What I want to accomplish is for a user to put in selected dates from calendar to query the database and whatever fields or information that is between those two dates are what needs to be showed on the client's side. So if a new client is added to the database and is within the dates selected, then that is what should show up.
I have tried to get forms etc but as I said,I dont know how to put it all together.
I would like someone to help me and say,eg,you need a form which does this,in the form this links to this part of the query db script,as said in original post,I am still very much a beginner. This is exciting for me but I'm getting stuck. All I have been able to do from a php page I put together was show all the field relating to eachother. Now i would like some assistance in being able to do as mentioned above.
The code I have at the moment is as follows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"
src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart','table','piechart']}]}">
</script>
<script type="text/javascript">
google.setOnLoadCallback(pieChart);
function pieChart() {
var startdate = "";
var enddate = "";
if ($("#datepicker").hasClass('hasDatepicker')) {
startdate = $("#datepicker").datepicker('getDate');
}
if ($("#datepicker2").hasClass('hasDatepicker')) {
enddate = $("#datepicker2").datepicker('getDate');
}
var pieJsonData = $.ajax({
url: "overall_ban_pos_pie_date.php?startdate=" + startdate + "&enddate=" + enddate,
dataType:"json",
async: false
}).responseText;
var pieData = new google.visualization.DataTable(pieJsonData);
var pieChartWrapper = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'pie_div',
'dataTable':pieData,
'options': {
chartArea:{left:10,top:40,height:200,width:360},
width:300,
height:260,
title: "Neutral Percentage",
titleTextStyle:{fontSize:12},
tooltip:{showColorCode:true},
legend:{textStyle:{fontSize: 10},position:'left'},
pieSliceTextStyle:{fontSize: 10}
}
});
pieChartWrapper.draw();
}
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#datepicker").datepicker({dateFormat: "yy-mm-dd"});
});
$(document).ready(function() {
$("#datepicker2").datepicker({dateFormat: "yy-mm-dd"});
});
$("#pieChart").click(function(e) {
e.preventDefault();
pieChart();
});
</script>
</head>
<body style="font-size:62.5%;">
<form action="overall_ban_pos_pie_date.php" method="post">
Start Date: <input type="text" name="startdate" id="datepicker"/>
End Date: <input type="text" name="enddate" id="datepicker2"/>
<input type="submit" id="pieChart"/>
</form>
<div id="pie_div"></div>
</body>
<?php
$con = mysql_connect("localhost:33307","USER","PASS");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("leonetcrm", $con);
$result = mysql_query("SELECT vtiger_account.accountid, vtiger_crmentity.crmid, vtiger_account.account_no, vtiger_crmentity.createdtime
FROM leonetcrm.vtiger_account
INNER JOIN leonetcrm.vtiger_crmentity
ON vtiger_account.accountid=vtiger_crmentity.crmid");
echo "<table border='5'>
<tr>
<th>Account ID</th>
<th>Account Number</th>
<th>CRMID</th>
<th>Createdtime</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['accountid'] . "</td>";
echo "<td>" . $row['account_no'] . "</td>";
echo "<td>" . $row['crmid'] . "</td>";
echo "<td>" . $row['createdtime'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</html>
I copied this from the web,I need to take the piechart out,just want normal table format. Once I can get that sorted I think it will work.
Thanks for taking the time to assist.
Cheers
Harry
If you haven't gotten too far along, I would suggest making some basic changes to how you organize things - it will help in the long run:
1. DO NOT use the mysql_* functions. They are outdated and not recommended for new projects.
Use the mysqli extension (http://php.net/mysqli) instead. If you're interested, here's more information on choosing which extension to use (http://php.net/manual/en/mysqlinfo.api.choosing.php).
2. AVOID (at nearly any cost) switching back and forth between php code and regular HTML. Likewise, AVOID using echo or other functions that send output to the browser until your script is complete. Things will be a lot less confusing in the long run if you keep them separated.
Instead, put _all_ of your php code a the top of your script (storing any generated HTML markup in a variable), and output it when you are done. For example:
<?php /* BAD example */
$DB = new mysqli( host,user,pass,name );
if( $query = $DB->query( "SELECT `col1`,`col2` FROM `table` WHERE `col1`='something'" ) ){
echo "<table>
<tr><th>Column One</th><th>Column Two</th></tr>";
while( $row = $result->fetch_assoc() ){
echo "<tr><td>{$result['col1']}</td><td>{$result['col2']}</td></tr>";
}
echo "</table>";
}?>
<?php
/* GOOD example */
// this results in the same output, but the script is separate from the output.
// makes it easier to debug, maintain, and make changes in the future.
$DB = new mysqli( host,user,pass,name );
if( $query = $DB->query( "SELECT `col1`,`col2` FROM `table` WHERE `col1`='something'" ) ){
while( $row = $result->fetch_assoc() ){
$tablerow = "<tr><td>{$result['col1']}</td><td>{$result['col2']}</td></tr>";
}
$table = "<table>
<tr><th>Column One</th><th>Column Two</th></tr>";
$table .= implode( "\n",$tablerow );
$table .= "</table>";
}
// all done
echo $table;
?>
Dirty Harry
09-25-2012, 06:58 AM
Thanks traq
I see what you saying. I'm not that far with the coding. I will change it to what you suggested and see what it does.
I still however am not too sure on how to get my report to work properly, firstly the coding as well as the layout, I dont know how to plan it so not sure where to start.
Ok
I have split it in two, the form one on page and the sql script on another,only problem is with the sql select script it just shows the whole range and not my start date to end date...
Any help with that please.
How about sharing your database/table structure, along with the SQL you're using?
Dirty Harry
09-26-2012, 06:38 AM
Im not sure if I understand what you mean by that, but this is the info I have so far4771 using the following script
<?php
$con = mysql_connect("localhost:33307","USER","PASS");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("leonetcrm", $con);
$result = mysql_query("SELECT vtiger_account.accountid, vtiger_crmentity.crmid, vtiger_account.account_no, vtiger_crmentity.createdtime
FROM leonetcrm.vtiger_account
INNER JOIN leonetcrm.vtiger_crmentity
ON vtiger_account.accountid=vtiger_crmentity.crmid");
echo "<table border='5'>
<tr>
<th>Account ID</th>
<th>Account Number</th>
<th>CRMID</th>
<th>Createdtime</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['accountid'] . "</td>";
echo "<td>" . $row['account_no'] . "</td>";
echo "<td>" . $row['crmid'] . "</td>";
echo "<td>" . $row['createdtime'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
That pic is showing out of 24xxx from the account ID column. What I need from my pop up datepicker is that I select the dates in which I want to see new accounts added, the sql query then shows data according to the two dates selected from the datepicker, thats where I am getting stuck.
If there other info needed please ask.
try something like so:
SELECT
vtiger_account.accountid
,vtiger_crmentity.crmid
,vtiger_account.account_no
,vtiger_crmentity.createdtime
FROM
leonetcrm.vtiger_account
INNER JOIN
leonetcrm.vtiger_crmentity
ON
vtiger_account.accountid=vtiger_crmentity.crmid
WHERE
vtiger_crmentity.createdtime >= STR_TO_DATE($startDateTime, '%Y-%m-%d %H:%i:%s')
AND
vtiger_crmentity.createdtime <= STR_TO_DATE($endDateTime, '%Y-%m-%d %H:%i:%s')
ORDER BY
vtiger_crmentity.createdtimewhere $startDateTime and $endDateTime are determined from the values submitted by the form.
Dirty Harry
09-28-2012, 06:14 AM
Thanks traq
That doesnt seem to work though.
Is there some sort of tutorial that I can go through on how to setup a form and then how to get the data to use that as the parameters?
I find alot on the web about it but only being half scripts that people post up I dont know what to put where and how it links up?
That doesnt seem to work though.What do you mean?
what did you try? what happened? what problems did you encounter? what did you try to do to solve them (or, at least, what do you think is causing the problem)?
give us something to work with. That query was almost guaranteed not to work as a "copy-paste" - I don't know what your form looks like, or how you plan to process the submitted data, or anything.
Is there some sort of tutorial that I can go through on how to setup a form and then how to get the data to use that as the parameters?I'm sure there are. But it's a very simple concept:
<form action="send form to this page" method="post">
<label>Start Date: <input type="datetime" name="start"></label>
<label>End Date: <input type="datetime" name="end"></label>
<input type="submit" name="searchsubmit" value="Submit Search">
</form>
<?php
// if form was submitted
if( isset( $_GET['searchsubmit'] ) ){
// the start time (if any) is in `$_GET['start']`
// check that $start is a valid date/time (strtotime() will work for now, but has a limited date range)
// (using the DateTime class would be better)
// if so, sanitize it (if you're using mysql, the function is mysql_real_escape_string())
// (but remember my earlier caution AGAINST ext_mysql)
// and put it into your query.
// same for end time.
}
I find alot on the web about it but only being half scripts that people post up I dont know what to put where and how it links up?
For the most part, webdev forums -like this one- have members who are excited and knowledgeable about web stuff. But it's not their *job*. They do it because they enjoy it, and because they enjoy helping others learn. Because of this, most advice you will get will be examples, and not finished, "cut-n-paste" code. You're expected to do some work on your own.
Now, you may be doing the work, but if you don't share that with us, we have no way of knowing. So, share with us! Be part of the process! Just saying "that didn't work" and posting your code doesn't really encourage any further advice. See my list at the top of this post.
If you want to learn how to do it, then this is the way to go about it. I'll help you every step of the way, as long as I can, and I know others here will as well - as long as they don't feel like they're doing all the work for you. My advice is that, if you want someone to give you a finished product, then you should hire them to give you a finished product. :)
Dirty Harry
10-01-2012, 06:40 AM
Sorry about that, you right, I should have otherwise you cant see what I did wrong.
I dont want someone to do it for me no, I want to learn as I go along and I really appreciate the time you taking to help me.
What I meant by not knowing how to put it together is that I'm not sure what values to put and where to put it, but let me post my 2 pages of script and let me learn!
Right, I started off with this one script I copied which is a pie chart which I dont want, I only used it as it was the closest code to what I needed(this page is called test2.php):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"
src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart','table','piechart']}]}">
</script>
<script type="text/javascript">
google.setOnLoadCallback(pieChart);
function pieChart() {
var startdate = "";
var enddate = "";
if ($("#datepicker").hasClass('hasDatepicker')) {
startdate = $("#datepicker").datepicker('getDate');
}
if ($("#datepicker2").hasClass('hasDatepicker')) {
enddate = $("#datepicker2").datepicker('getDate');
}
var pieJsonData = $.ajax({
url: "overall_ban_pos_pie_date.php?startdate=" + startdate + "&enddate=" + enddate,
dataType:"json",
async: false
}).responseText;
}
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#datepicker").datepicker({dateFormat: "yy-mm-dd"});
});
$(document).ready(function() {
$("#datepicker2").datepicker({dateFormat: "yy-mm-dd"});
});
$("#pieChart").click(function(e) {
e.preventDefault();
});
</script>
</head>
<body style="font-size:62.5%;">
<form action="test_connectdb.php" method="post">
Start Date: <input type="text" name="startdate" id="datepicker"/>
End Date: <input type="text" name="enddate" id="datepicker2"/>
<input type="submit" id="date"/>
</form>
<div id="pie_div"></div>
</body>
</html>
So either I start a complete new thing, or we go through it and trim out whats not needed. Please I dont want anything done for me, I just want to understand what and why.
That code once date is picked then goes to the following page which is the test_connectdb.php page.
<html>
<body>
Data selected between <?php echo $_POST["startdate"]; ?><br />
and <?php echo $_POST["enddate"]; ?> is as follows:
<?php
$con = mysql_connect("localhost:33307","USER","PASS");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("leonetcrm", $con);
$result = mysql_query("SELECT vtiger_account.accountid, vtiger_crmentity.crmid, vtiger_account.account_no, vtiger_crmentity.createdtime
FROM leonetcrm.vtiger_account
INNER JOIN leonetcrm.vtiger_crmentity
ON vtiger_account.accountid=vtiger_crmentity.crmid
WHERE vtiger_crmentity.createdtime >= STR_TO_DATE($startDate, '%Y-%m-%d %H:%i:%s')
AND vtiger_crmentity.createdtime <= STR_TO_DATE($endDate, '%Y-%m-%d %H:%i:%s')
ORDER BY vtiger_crmentity.createdtime");
echo "<table border='5'>
<tr>
<th>Account ID</th>
<th>Account Number</th>
<th>CRMID</th>
<th>Createdtime</th>
</tr>";
while($row = mysql_fetch($result))
{
echo "<tr>";
echo "<td>" . $row['accountid'] . "</td>";
echo "<td>" . $row['account_no'] . "</td>";
echo "<td>" . $row['crmid'] . "</td>";
echo "<td>" . $row['createdtime'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
All it shows is the dates I selected and the headings for the columns, there is no data.
I am actually so excited and amped to get this to work as this is my first proper 'report' and I am enjoying it, just hit a dead-end and cannot complete it. So I will work with you as much as I can and give you whatever info I can so we can get this to work. I have learnt so much already and cannot wait to learn even more.
Thank you once again for your time and effort.
Harry
So either I start a complete new thing, or we go through it and trim out whats not needed.I'd recommend the "starting fresh" route.
The pop-up date picker can be added next without much trouble; for now, it's not strictly necessary.
Let's start with just the essentials of submitting the form, processing it, querying the database, and displaying the results:
(untested; I don't have your DB)
<?php
// let's rearrange the general flow of your script:
// do all the PHP first; output the HTML when you're done
// (and the function definitions are at the end, out-of-the-way).
// I'm also using the mysqli class instead of the (outdated) mysql extension
// in this example, we'll handle everything from the same script.
## Basics ##
// where we are.
const this_script_URL = '/URL/to/this/page.php';
// database credentials.
const DBhost = 'your-database-hostname';
const DBuser = 'your-database-username';
const DBpass = 'your-database-password';
const DBname = 'database-we-should-use';
## Logic ##
// this is where we decide what to do and tell PHP to do it.
if( empty( $_POST['submit'] ) ){
// the form was not submitted. Display the form.
$OUTPUT = showForm();
}else{
// the form was submitted. get the dates requested.
if( ($dates = processForm()) && $dates !== false ){
// we got valid dates from the form submission.
// query the database (make sure we've connected okay first)
if( ($DB = connectToDB()) && !mysqli_connect_error() ){
$result = queryDatabase( $DB,$dates );
// and format the results as HTML to be output.
$OUTPUT = showResults( $result,$dates );
// close the database connection
$DB->close();
}else{
// could not connect to the database.
$OUTPUT = showError( '...could not connect to the database.<br>'.mysqli_connect_error() );
}
}else{
// the form submission was not valid.
$OUTPUT = showError( '...the form submission was not valid.' );
}
}
## All Done! ##
// not all servers specify UTF-8 by default (punk servers!)
// the implication here is that *you* should be taking it upon yourself to use UTF-8 *everywhere* :)
header( "Content-Type: text/html; charset=utf-8" );
// send the HTML page and end PHP execution
exit( showPage( $OUTPUT ) );
## Function Definitions ##
// $error is any error message you wish to show (leave it empty if there are no errors).
// returns the HTML markup for your form.
function showForm( $error=false ){
// the form submits to itself.
$action = this_script_URL;
// this is your form.
$_form = <<< HTML
<form action="$action" method="post">
<p>Start Date: <input type="datetime" name="startdate"></p>
<p>End Date: <input type="datetime" name="enddate"></p>
<p><input type="submit" value="Search Dates"></p>
</form>
HTML
;
// if there's an error message, add it.
if( $error ){
$_form = '<p class="error">'.$error.'</p>'.$_form;
}
// return it.
return $_form;
}
// validates and sanitizes the form submission.
// returns timestamps for start and end date/times if successful; false means that something is wrong.
// *NOTE: using strtotime() is quick and easy to demonstrate, but because of the way the function works,
// date/times *before* Jan 1., 1970 *might not work* (depending on your server),
// and date/times *before* Fri, 13 Dec 1901 20:45:54 OR *after* Tue, 19 Jan 2038 03:14:07 *DEFINITELY won't work.*
// Probably not a dealbreaker right now, but other methods (e.g., the DateTime class) are more [past|future]proof.
function processForm(){
// both startdate and enddate are required
if( !empty( $_POST['startdate'] ) && !empty( $_POST['enddate'] ) ){
// convert start and end to unix timestamps (this is just a quick way to validate them as dates)
$start = strtotime( $_POST['startdate'] );
$end = strtotime( $_POST['enddate'] );
// if $start or $end are false, that means they were not valid dates* (see note above!!!)
if( $start !== false && $end !== false ){
return array( $start,$end );
}
}
// if we get this far, something went wrong
return false;
}
// returns a connection to your DB, using mysqli
function connectToDB(){
return new mysqli( DBhost,DBuser,DBpass,DBname );
}
// $DB is the database connection object (we already made sure it was successful in the Logic portion of the code)
// $dates is an array holding the start and end dates
// returns the result of the database query
function queryDatabase( $DB,$dates ){
// get the start & end dates
list( $start,$end ) = $dates;
// prepare SQL query
// note I'm now using the mysql function FROM_UNIXTIME() because we're now passing a unix timestamp
// (they don't need to be escaped (sanitized) because unix timestamps have numeric characters only)
$SQL = "SELECT vtiger_account.accountid, vtiger_crmentity.crmid, vtiger_account.account_no, vtiger_crmentity.createdtime
FROM leonetcrm.vtiger_account
INNER JOIN leonetcrm.vtiger_crmentity
ON vtiger_account.accountid=vtiger_crmentity.crmid
WHERE vtiger_crmentity.createdtime >= FROM_UNIXTIME('$start', '%Y-%m-%d %H:%i:%s')
AND vtiger_crmentity.createdtime <= FROM_UNIXTIME('$end', '%Y-%m-%d %H:%i:%s')
ORDER BY vtiger_crmentity.createdtime";
// query the database
$result = $DB->query( $SQL );
// return the result
return $result;
}
// $result is the results of the database query
// returns the results formatted as HTML
function showResults( $result,$dates ){
// get the start and end dates
list( $start,$end ) = $dates;
// what if there was an error?
if( $result === false ){
return showError( '...the database query failed.<br>'.$DB->error() );
}
// what if there were no results?
if( $result->num_rows === 0 ){
return showError( '...no records found between '.date( 'Y-m-d H:i:s',$start ).' and '.date( 'Y-m-d H:i:s',$end ) );
}
// loop through results and build rows for your table.
while( $r = $result->fetch_row() ){
// get each field
list( $accountID,$accountNo,$crmID,$created ) = $r;
// use htmlspecialchars to make sure results are displayed as text
$accountID = htmlspecialchars( $accountID );
$accountNo = htmlspecialchars( $accountNo );
$crmID = htmlspecialchars( $crmID );
$created = htmlspecialchars( $created );
// assign the new table row to an array of rows
$row[] = "<tr><td>$accountID</td><td>$accountNo</td><td>$crmID</td><td>$created</td></tr>";
}
// make the array of rows into one long string
$rows = implode( "\n",$row );
// this is your table.
$table = <<< HTML
<table><tbody>
<tr><th>Account ID</th><th>Account Number</th><th>CRMID</th><th>Time Created</th></tr>
$rows
</tbody></table>
HTML
;
// return the table.
return $table;
}
// $message is the error message to show
// returns the error message, along with the form so the user can try again.
function showError( $message='' ){
// was there a message?
if( empty( $message ) || !is_string( $message ) ){
$message = '...something weird happened.';
}else{
// make safe for display
$message = htmlspecialchars( $message );
}
// return error message with form
return showForm( $message );
}
function showPage( $content ){
// this is your whole page.
$_html = <<< HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Search By Dates</title>
</head>
<body>
<h1>Search by Dates</h1>
<div id="output">$content </div>
</body>
</html>
HTML
;
// all done
return $_html;
}
?>
bernie1227
10-02-2012, 05:06 AM
<?php
$con = mysql_connect("localhost:*****","****","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
Just as a matter of practise, I wouldn't post the password of your databases online, even if they are local host.
also, sorry; I overlooked establishing the database connection!
I've added this function to the function definitions in my post above:
// returns a connection to your DB, using mysqli
function connectToDB(){
return new mysqli( DBhost,DBuser,DBpass,DBname );
}
Dirty Harry
10-02-2012, 02:25 PM
Wow, that makes a hell of a lot more sense. Now atleast I sort of see how it works together. However, I have put in my db details, when I load the page, it is blank, nothing happens. What do I fix?
Dirty Harry
10-02-2012, 02:26 PM
Why so bernie? I will keep it in mind for next time though.
Wow, that makes a hell of a lot more sense. Now atleast I sort of see how it works together. However, I have put in my db details, when I load the page, it is blank, nothing happens. What do I fix?
Since the script produces output no-matter-what, I'd have to say that there's an error. As I said, I haven't tested it yet. I did update it this morning, since I discovered there was a missing function in the first post. You might try it again; make sure your copy of the script has the function connectToDB() defined.
However, if you didn't see any errors, you need to make sure error reporting is turned on. (Don't display errors on your live site, obviously, but during development it's essential to see every last error.)
<?php
// very top of script
error_reporting( -1 );
ini_set('display_errors', '1');
// . . .
djr33
10-02-2012, 07:51 PM
Hackers could see the passwords. If it's just localhost it might make no difference, but if you ever use the same password for another account or if someone tries to access your personal computer remotely, it could be a problem. And there's never a reason for any of us here to see your passwords-- as long as you don't have a typo, they're never related to the code. (We can test with them anyway, so it's not important for us to have that part included-- good to see that you have the right code there, but not the username or password or even database name.) I suppose it's up to you; but as a habit we don't want to have passwords here for anyone to try to use against our members.
bernie1227
10-02-2012, 10:59 PM
Why so bernie? I will keep it in mind for next time though.
As Daniel explained, if you out that same site/database on the web, and at some point provide a url to your site to fix a problem, then you are in massive trouble, you would have given far and away enough information to access your database. <Hypothetical>For example, you make a website online, you have a table called user_info, using the same password, you post a link to your site on this forum in order to ask for help, now any hacker can access the personal info of all your clients.</hypothetical> so just as a matter of practise, I wouldn't post information nine that could lead to your website being hacked :)
@traq, why did you blank the password in my quote but nowhere else in the page?
djr33
10-03-2012, 12:22 AM
We intended to catch them all, but it looks like we missed a couple. I just fixed them. There's a point where it's up to Dirty Harry to keep an eye on it anyway, but I think they're all gone for now.
@traq, why did you blank the password in my quote but nowhere else in the page?
because the OP can't edit your post (all the other posts that show the credentials are his own; he can edit them out if he so desires).
My main concern was that he'd be able to remove them all if he thought it was prudent. I didn't mean to single you out.
bernie1227
10-03-2012, 01:32 AM
Don't worry, I wasn't feeling singled out, I was just wondering whether you had just forgotten to remove the rest of the database information.
It appears the rest of the information has been changed to USER and PASS
yeah, Daniel went ahead and did that. no prob.
Dirty Harry:
I've done my testing. (I made all of these corrections to the script in post #16 (http://www.dynamicdrive.com/forums/showthread.php?70932-Help-a-beginner&p=283585#post283585) as well, so it's now error-free.)
1. Forgot to include definition of the function connectToDB()
2. Forgot a semicolon on line 202 (last line!)
3. Misspelled htmlspecialchars() on lines 150,151,152, and 153
.....(actually, only misspelled it the first time: the others were copy+paste)
4. Forgot to add name="submit" to the submit button on the form
After these fixes, the script worked fine.
I changed the SQL, of course, since I was testing on my own database, so your original query still may-or-may-not work.
Let me know how it goes.
Dirty Harry
10-03-2012, 06:44 AM
Thanks for that info guys, and thanks for blanking my credentials out, this is a huge learning game for me.
@traq, I think I am cocking something up from my side, I corrected the script as you mentioned, also with the function you added. The way I am testing it is typing localhost:8888/test2.php in my browser. My other pages work, is this configured slightly differently? I also put that error line right at the top of the page, doesn't even show the error. I am using apache as my web server...any other info needed?
Dirty Harry
10-03-2012, 06:45 AM
Oh and of course, I am adding my db data for the connection in the.
well, all I can figure is that you're throwing a fatal error somehow (a fatal error stops the script before it's actually run, so our calls for error checking would never be made). Do you have a php.ini file in your web root directory? You could edit the following values (make a backup copy first):
error_reporting = E_ALL | E_STRICT
display_errors = Onif there is no php.ini file, you can add it.
Are you sure your copy of the script includes all the corrections I made? Make sure you're using a copy of the script that you downloaded *after* I posted my post #27.
Dirty Harry
10-04-2012, 07:24 AM
I will check for that.
Yeah a made sure the copy of the script is is after post #27 and I saw the changes you had made.
Here's a great example php.ini (http://www.reallylinux.com/docs/php.ini) file. The two options I posted earlier are the ones that are relevant to our problem (not seeing any error messages!), but you might want to look at it anyway. All of the options are very well-commented.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.