PHP: Difference between revisions

4,224 bytes added ,  4 years ago
no edit summary
(Created page with " = Basics = {{UC}} = Code Snipets= *Redirect to another page if query is empty: <pre> $sql = "SELECT * FROM MasterDB WHERE sr=$sr"; $results = mysqli_query($link, $sql); if...")
 
No edit summary
 
(6 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 21 ⟶ 33:
}
</pre>
 
* DB connection:
<pre>
define('DB_NAME', 'codered');
define('DB_USER', 'test');
define('DB_PASSWORD', 'test@123');
define('DB_HOST', 'localhost');
 
$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'];
</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}}