PHP: Difference between revisions

3,053 bytes added ,  4 years ago
no edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1:
[[Category: Scripting]]
__TOC__
<br />
 
= Basics =
 
{{UC}}
*Linking optional files:
<pre>
<?php include 'footer.php'; ?>
</pre>
 
*Linking Mandatory files:
<pre>
<?php require 'db_connect.php'; ?>
</pre>
 
= Code Snipets=
Line 54 ⟶ 66:
$ProductionDown = $_POST['ProductionDown'];
</pre>
 
* Accessing DB:
<pre>
//Set database access credentials
$name = 'codered';
$user = 'test';
$password = 'test@123';
$host = 'localhost';
 
//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));
</pre>
 
*Send input data to DB:
<pre>
$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')";
 
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);
</pre>
 
* Fetching Data from DB to Page:
<pre>
$sql = "SELECT * FROM MasterDB ORDER BY id DESC LIMIT 1";
 
$results = mysqli_query($link, $sql);
 
if (!$results) {
die('Invalid query: ' . mysqli_error($link));
}
 
echo '<h2>CodeRed Alert Generated with below Info:</h2>';
 
while($result = mysqli_fetch_array($results )){
echo '<div style="border: 1px solid #e4e4e4; padding: 15px; margin-bottom: 10px;">';
echo '<p>SR Number: ' . $result['sr'] . '</p>';
echo '<p>Customer Name: ' . $result['CustomerName'] . '</p>';
echo '<p>Country: ' . $result['Geo'] . '</p>';
echo '<p>Business Impact: ' . $result['BusinessImpact'] . '</p>';
echo '<p>Product Version: ' . $result['ProductVersion'] . '</p>';
echo '<p>Problem Description: ' . $result['ProblemDescription'] . '</p>';
echo '<p>Customer Temp: ' . $result['CustomerTemp'] . '</p>';
echo '<p>BugID: ' . $result['BugID'] . '</p>';
echo '<p>ProductionDown: ' . $result['ProductionDown'] . '</p>';
echo '<p>Last Update: ' . $result['LastUpdate'] . '</p>';
echo '<p>Engineers Email ID: ' . $result['email'] . '</p>';
echo '</div>';
}
</pre>
 
*Send Email:
<pre>
$to = "aman@test.com";
$subject = "Alert Mail";
 
$headers = "From: TestAlert<testalert@test.com>" . "\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 = "<p>Hi,</p>
<p>This is a Test Alert Mail.</p>
<p>Please find below the details about this Alert:<br /><br /></p>;
 
mail($to,$subject,$txt,$headers);
</pre>
 
 
<br />
;References
<references/>
<br />
<br />
<br />
 
 
{{DISQUS}}