This blog site is not meant to be any information that is distributed to the general public. It is to serve as shared documentation between the DNSTC developers; however, if you find something useful and would like to use it, go right ahead.

Monday, February 14, 2011

Delete All Records in a MySQL Table – Quick Reference

Occasionally you may need to remove all records from a database table to reset the data. The following code is a quick reference to delete all of the records within a table. It can be copy/paste into a PHP page, then it will work after modify the reference to the database and/or tables.

This is just quick reference to remove all records from a database:

PHP Code:


$deleteSQL = "DELETE FROM inventory";
mysql_select_db($database_database, $databse);
$Result1 = mysql_query($deleteSQL, $database) or die(mysql_error());

Adding or Subtracting Dates in PHP

Working with dates within PHP sometimes come in handy, yet the syntax is not quite something that is easy to remember. You can add or subtract; days, months and years to the current date by using the mktime() function.


Here is some quick PHP code to add a month to the current date:
$next_month = mktime(0, 0, 0, date("m")+1, date("d"), date("y"));
$outDate = date("Y-m-d", $next_month);

For more on the mktime() function: http://php.net/manual/en/function.mktime.php


For more on the date() format function: http://php.net/manual/en/function.date.php

Turning on PHP errors on the Server

By default, if there is a PHP error, it is not displayed on the screen to the user. This is done for security reasons, but it does not aid the programmer in troubleshooting the issue. If you place some code within the .htaccess file, you can turn on the error display. The following code is used to turn on the error display for troubleshooting purposes:



php_flag display_errors on
php_value error_reporting 7


To add this code, perform the following:

  1. Use putty to log into server.
  2. cd to the appropriate /home/<users>/www directory
  3. nano .htaccess
  4. Copy and paste the code above to the file
  5. Save the .htaccess file… control/o

If you now refresh the page, you should see the error and you can be off to troublshoot!

Sunday, February 13, 2011

Submitting e-mail to pop-up window

Problem:

We had an issue with e-mail sending on an ASP Intranet and had to come up with a solution to redirect the sending of mail to a PHP page on the client's website.  The client's website did not have access to sendmail or any other program that could be used to send out the e-mail messages.

Solution:

To do this, we had to submit the data to a PHP page using a pop-up window, the only problem this caused was the form stayed on the screen with the entered data after it was submitted, since we used a pop-up window to submit the data.

I was able to solve this problem by adding the following code as an attribute to the form tag:
onsubmit="window.location='newpage.htm';"

This then changes the page that contains the form at the same time it is submitting the data to a pop-up page.

WYSIWYG Editor

I have purchased a WYSIWYG editor that can be used in any of the programming that DNSTC does and requires an editor.  A WYSIWYG editor is just that... What you see is what your get... this type of editor is used to replace textareas within a form.

The following is the documentation for the WYSIWYG editor that I purchased.

 http://www.wysiwygpro.com/index.php?id=56

Thursday, March 19, 2009

Quick and Dirty E-mail Form

There may be occasions where you need a quick form to send information to a client.  The form does not have to be anything special, just collect some information and send it off to somebody’s e-mail address.

The following is some code that will submit whatever fields you specify within the form to an e-mail address.

Place the following PHP code within the body tags, (Note: Your site must be PHP capable):
<?php

if(!empty($_POST)) { // If something is submitted to this page
$body = "";

// This loop will build the body of the message including every variable
while (list($key, $val) = each($HTTP_POST_VARS)) {
if ($key--> "submit") { // Don't include the sumit key value
$body .= "$key: $val \n"; //Add all fields to the message
}
}

$to = "your@emailaddress.com";
$subj = "The subject you want";
$from = "from@websiteaddress.com";

mail($to, $subj, $body, "FROM: $from");

// Display a thank you message after the message has been sent.
?>
<blockquote>
Thank You for your submission. The results were e-mailed...</blockquote>
<?php } else { ?>

<form method=post action=<?php echo $PHP_SELF; ?>>

... Add the form and fields here ...

</form>

<?php } // close the if ?>

Friday, April 18, 2008

Resetting the Auto Increment

Here is a little trick I have picked up in my travels… when you load information into a MySQL database that has an auto increment for the key, those keys can become quite large. Especially if you continue to delete/load records into the database.

You can use the following to reset the key (Or change it to whatever you want):

Perform this after a deletion…
$fixIt = "ALTER TABLE inventory AUTO_INCREMENT = 1";
mysql_select_db($database_experience, $experience);
$Result1 = mysql_query($fixIt, $experience) or die(mysql_error());