Sunday, December 19, 2010

write to a file php


$file_handle = fopen('filename.xml','a');
$content=('

data

'); fwrite($file_handle,$content);
fclose($file_handle);

Update data php

"; ?>
include_once("include/db_connection.php");

$thisCustomer_id = $_REQUEST['customer_idField'];

$sql = "SELECT * FROM customers WHERE customer_id = '$thisCustomer_id'";

$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUMROWS($result);
if ($numberOfRows==0) {
?>

Sorry. No records found !!

}
else if ($numberOfRows>0) {

$i=0;
$thisCustomer_id = MYSQL_RESULT($result,$i,"customer_id");
$thisFname = MYSQL_RESULT($result,$i,"fname");
$thisMname = MYSQL_RESULT($result,$i,"mname");
$thisLname = MYSQL_RESULT($result,$i,"lname");
$thisCompany = MYSQL_RESULT($result,$i,"company");
$thisTitle = MYSQL_RESULT($result,$i,"title");
$thisAddress1 = MYSQL_RESULT($result,$i,"address1");
$thisAddress2 = MYSQL_RESULT($result,$i,"address2");
$thisAddress3 = MYSQL_RESULT($result,$i,"address3");
$thisCity = MYSQL_RESULT($result,$i,"city");
$thisState_province = MYSQL_RESULT($result,$i,"state_province");
$thisCountry = MYSQL_RESULT($result,$i,"country");
$thisPostal_code = MYSQL_RESULT($result,$i,"postal_code");
$thisPhone = MYSQL_RESULT($result,$i,"phone");
$thisFax = MYSQL_RESULT($result,$i,"fax");

}
?>

Insert data php

enter_new.php
"; ?>
include_once("include/db_connection.php");
?>

Enter Customers


form name="customersEnterForm" method="POST" action="insert_new.php"
input type="text" name="thisCustomer_idField" size="20" value=""
======================================================================
insert_new.php

"; ?>
include_once("include/db_connection.php");
?>
// Retreiving Form Elements from Form
$thisCustomer_id = addslashes($_REQUEST['thisCustomer_idField']);
$thisFname = addslashes($_REQUEST['thisFnameField']);
$thisMname = addslashes($_REQUEST['thisMnameField']);
$thisLname = addslashes($_REQUEST['thisLnameField']);
$thisCompany = addslashes($_REQUEST['thisCompanyField']);
$thisTitle = addslashes($_REQUEST['thisTitleField']);
$thisAddress1 = addslashes($_REQUEST['thisAddress1Field']);
$thisAddress2 = addslashes($_REQUEST['thisAddress2Field']);
$thisAddress3 = addslashes($_REQUEST['thisAddress3Field']);
$thisCity = addslashes($_REQUEST['thisCityField']);
$thisState_province = addslashes($_REQUEST['thisState_provinceField']);
$thisCountry = addslashes($_REQUEST['thisCountryField']);
$thisPostal_code = addslashes($_REQUEST['thisPostal_codeField']);
$thisPhone = addslashes($_REQUEST['thisPhoneField']);
$thisFax = addslashes($_REQUEST['thisFaxField']);

$sqlQuery = "INSERT INTO customers (customer_id , fname , mname , lname , company , title , address1 , address2 , address3 , city , state_province , country , postal_code , phone , fax )
VALUES ('$thisCustomer_id' , '$thisFname' , '$thisMname' , '$thisLname' , '$thisCompany' , '$thisTitle' , '$thisAddress1' , '$thisAddress2' , '$thisAddress3' , '$thisCity' , '$thisState_province' , '$thisCountry' , '$thisPostal_code' , '$thisPhone' , '$thisFax' )";
$result = MYSQL_QUERY($sqlQuery);

retrieving_data.php

"; ?>
echo "Intro: Let's do a little generic database work!

";
$user = "root";
$passwd = "";

// Connecting, selecting database
$connect = mysql_connect('localhost', 'root', '')
or die('Could not connect: ' . mysql_error());
echo 'Step 1: Connected successfully!
';

$db = 'samples';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
echo 'Step 2: Connected to ('.$db.') successful!
';

// Performing SQL query
$query = 'SELECT * FROM tbl_users';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());


// Printing results in HTML
echo "

Let's fetch data...
";
echo "
\n";

$count = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t \n";
foreach ($line as $col_value) {
echo "\t\t \n";
}
$count++;
echo "\t \n";
}

echo "
$col_value
\n";

if ($count < 1) {
echo "

No rows were found in this table.

";
} else {
echo "

".$count." rows were found in this table.

";
}

// We will free the resultset...
mysql_free_result($result);

// Now we close the connection...
mysql_close($connect);
?>

connecting_to_data.php

"; ?>
echo "Intro: Let's do a little generic database work!

";
$user = "root";
$passwd = "";

// Connecting to and selecting a database
$connect = mysql_connect('localhost', $user, $passwd)
or die('Could not connect: ' . mysql_error());
echo 'Step 1: Connected successfully!
';

$db = 'samples';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
echo 'Step 2: Connected to ('.$db.') successful!
';

// Now we close the connection...
mysql_close($connect);
?>