PHP

= Basics =


 * Linking optional files:


 * Linking Mandatory files:

= Code Snipets= $sql = "SELECT * FROM MasterDB WHERE sr=$sr"; $results = mysqli_query($link, $sql);
 * Redirect to another page if query is empty:

if (!$results) { die('Invalid query: ' . mysqli_error($link)); }

if (!$result = mysqli_fetch_array($results )){ header('Location: index.php', TRUE, 303); exit; } else { header('Location: index2.php', TRUE, 303); exit; }

define('DB_NAME', 'codered'); define('DB_USER', 'test'); define('DB_PASSWORD', 'test@123'); define('DB_HOST', 'localhost');
 * DB connection:

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) { die('Could not connect: ' . mysqli_connect_error($link)); }

$db_selected = mysqli_select_db($link, DB_NAME);

if (!$db_selected) { die('Can\'t use ' . DB_NAME . ': ' . mysqli_error($link)); }

$sr = mysqli_real_escape_string($link, $_POST['sr']); $redirect = mysqli_real_escape_string($link, $_POST['redirect_to']); $CustomerName = mysqli_real_escape_string($link, $_POST['CustomerName']); $Geo = $_POST['Geo']; $BusinessImpact = mysqli_real_escape_string($link, $_POST['BusinessImpact']); $ProductVersion = mysqli_real_escape_string($link, $_POST['ProductVersion']); $ProblemDescription = mysqli_real_escape_string($link, $_POST['ProblemDescription']); $CustomerTemp = $_POST['CustomerTemp']; $BugID = mysqli_real_escape_string($link, $_POST['BugID']); $LastUpdate = mysqli_real_escape_string($link, $_POST['LastUpdate']); $email = mysqli_real_escape_string($link, $_POST['email']); $ProductionDown = $_POST['ProductionDown'];

//Set database access credentials $name = 'codered'; $user = 'test'; $password = 'test@123'; $host = 'localhost';
 * Accessing DB:

//Set table name $tname = 'MasterDB';

/*Open the connection to our database use the info from the config file.*/ $link = mysqli_connect($host, $user, $password);

if (!$link) { die('Could not connect: ' . mysqli_error($link)); } mysqli_select_db($link, "codered") or die(mysqli_error($link));

$sql = "INSERT INTO MasterDB (sr, CustomerName, Geo, BusinessImpact, ProductVersion, ProblemDescription, CustomerTemp, BugID, LastUpdate, email, ProductionDown ) VALUES ('$sr', '$CustomerName', '$Geo', '$BusinessImpact', '$ProductVersion', '$ProblemDescription', '$CustomerTemp', '$BugID', '$LastUpdate','$email', '$ProductionDown')";
 * Send input data to DB:

if (!mysqli_query($link, $sql)) { die('Error ' . mysqli_error($link)); } /*Redirect the user after a successful form submission*/ if ( !empty ( $redirect ) ) { header("Location: $redirect?msg=1"); } else { header("Location: $referred?msg=1"); }

mysqli_close($link);

$sql = "SELECT * FROM MasterDB ORDER BY id DESC LIMIT 1";
 * Fetching Data from DB to Page:

$results = mysqli_query($link, $sql);

if (!$results) { die('Invalid query: ' . mysqli_error($link)); }

echo ' CodeRed Alert Generated with below Info: ';

while($result = mysqli_fetch_array($results )){ echo ' '; echo ' SR Number: '. $result['sr']. ' ';       echo ' Customer Name: '. $result['CustomerName']. ' ';       echo ' Country: '. $result['Geo']. ' ';       echo ' Business Impact: '. $result['BusinessImpact']. ' ';       echo ' Product Version: '. $result['ProductVersion']. ' ';       echo ' Problem Description: '. $result['ProblemDescription']. ' ';       echo ' Customer Temp: '. $result['CustomerTemp']. ' ';       echo ' BugID: '. $result['BugID']. ' ';       echo ' ProductionDown: '. $result['ProductionDown']. ' ';       echo ' Last Update: '. $result['LastUpdate']. ' ';       echo ' Engineers Email ID: '. $result['email']. ' ';       echo ' '; }

$to = "aman@test.com"; $subject = "Alert Mail";
 * Send Email:

$headers = "From: TestAlert". "\r\n". $headers .= "Content-type: text/html; charset=\"UTF-8\"; format=flowed \r\n"; $headers .= "Mime-Version: 1.0 \r\n"; $headers .= "Content-Transfer-Encoding: quoted-printable \r\n"; $headers .= "Cc: $email". "\r\n";

$txt = " Hi, This is a Test Alert Mail.  Please find below the details about this Alert:



mail($to,$subject,$txt,$headers);