Connect HTML files to WAMP database on another machine - php

So I'm developing a Raspberry Pi project that needs access to a database, however the database is stored locally on my laptop through WAMP. I have the Pi as a hotspot so that i can exchange files over and back through SSH however I'm struggling to get it to connect to the WAMP database to display the information that I want it to, what is the best way to do this? I've added my PHP and HTML file below.
HTML & PHP:
<?php
//Step 1
$db = mysqli_connect('localhost', 'root', '', 'hospital') or die('Error connecting to MySQL server.');
?>
<html>
<head>
<meta http-equiv="refresh" content="20">
</head>
<body>
<h1>
PHP connect to MySQL
</h1>
<table>
<?php
//Step 2
$query = "SELECT * FROM patients";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
echo "<table border = '1'>
<tr>
<th>id</th>
<th>patient_name</th>
<th>check_in_date</th>
<th>room_number</th>
<th>bed_number</th>
<th>notes</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['patient_name'] . "</td>";
echo "<td>" . $row['check_in_date'] . "</td>";
echo "<td>" . $row['room_number'] . "</td>";
echo "<td>" . $row['bed_number'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
//while($row = mysqli_fetch_array($result)){
// echo $row['id'] . ' ' .</tr> $row['patient_name']. ' ' . $row['check_in_date'] . ' ' . $row['room_number'] . ' ' . $row['bed_number'] . ' ' . $row['notes'] . '<br />';
//}
?>
</table>
</body>
</html>

Related

How introduce anchor links when fetching a url from SQL database using PHP?

This is the PHP script and I can load my data in database but elements in the Link row seems not clickable .so that I redirect the user.
My attempt was to add anchor() tags similar to table definition but its still inactive.
<?php
$con=mysqli_connect($server,$user,$password,$dbname);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM accident_log");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Link</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<a><td>" . $row['Link'] . "</td></a>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Learn here if you do not know https://www.w3schools.com
echo '<td>' . $row['link'] . '</td>';
echo '<td>' . $row['link'] . '</td>';

Email each line of sql as email

This script looks fine but when i run it, the entire db is just echo'd all over the page. I didn't get any errors so not sure yet but i uploaded a spreadsheet to sql full of tasks and then each line i want to email to myself and if possible delete it after but i can manually do that anyway. The goal is just to array the email across each row.
<?php
$con = mysql_connect("localhost","root","toor");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM tasks");
$to = "someone#somewhere.com";
$subject = "Results from query";
$body =
"<table border='1'>
<tr>
<th>CustomerID:</th>
<th>Name:</th>
<th>CompanyCode:</th>
<th>Country:</th>
<th>SalesRep:</th>
<th>City:</th>
<th>PostalCode:</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['CompanyCode'] . "</td>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td>" . $row['SalesRep'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['PostalCode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
$headers = 'From: someone#example.com';
mail($to,$subject,$body,$headers);
echo 'Mail sent to $to';
?>

Table ID in PHP

I'm trying to assign anID to the table I'm generating with PHP but it keeps returning an error. Here is the full code that works fine so far. All I want is to add an 'id' to the table so I can apply css styles to it in the relevant sheet
<?php
$con=mysqli_connect("localhost","<un>","<pw>","monitor");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
}
echo "</table>";
mysqli_close($con);
Any help? maybe I'm going about it the wrong way?
Please try below block of code . I have added table id and also close </tr> inside while loop
<?php
$con = mysqli_connect("localhost", "<un>", "<pw>", "monitor");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1' id='table-id'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
echo "</tr>"; // you forget close tr
}
echo "</table>";
mysqli_close($con);

Tables not showing in HTML database

I am creating a database and when creating the HTML script the table is coming back with no boarders = Firstname Lastname ";
while($row = mysqli_fetch_array($result)) {
echo "";
echo "" . $row['Firstname'] . "";
echo "" . $row['Lastname'] . "";
echo "";
}
echo "";
mysqli_close($con); ?>
Here is the code I created, could someone look at it and tell me what I have done wrong?
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caller_info");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
th>Franchise</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Lastname'] . "</td>";
echo "<td>" . $row['Franchise'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Look at the tag
<th>Franchise</th>
Please change your file extension to .php it is not rendering your script. Html file not render your php, and also correct your markup as suggested by #suhail.

How can I solve the undefined index

I have a mistake in my database .. It is written that there is undefined index from line 41 until 50 in the code .. In my website I tried to insert data from a form to the PhpMyAdmin database and everything is working fine except this...
The error is :
Notice: Undefined index: Services in C:\xampp\htdocs\ers\Database.php on line 41 - 50
Database.php :
<html>
<body>
<?php
$con = mysql_connect("localhost","root","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
<table border="1">
<tr >
<td>Finding</td>
<td>ServiceType</td>
<td>Title</td>
<td>RootCause</td>
<td>RiskRating</td>
<td>Impact</td>
<td>Efforts</td>
<td>Likelihood</td>
<td>Finding</td>
<td>Implication</td>
<td>Recommendation</td>
<td>Report</td>
</tr>
<?php
mysql_select_db ( "ers_1", $con);
$sql="INSERT INTO findings (ServiceType_ID, Title, RootCause_ID, RiskRating_ID, Impact_ID, Efforts_ID, Likelihood_ID, Finding,Implication, Recommendation, Report_ID) VALUES (
'$_POST[Services]',
'$_POST[title]',
'$_POST[RootCause]',
'$_POST[RiskRating]',
'$_POST[impact]',
'$_POST[Efforts]',
'$_POST[likelihood]',
'$_POST[Finding]',
'$_POST[Implication]',
'$_POST[Recommendation]',
'1'
)";
$result = mysql_query("SELECT * FROM findings");
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['Finding_ID'] . "</td>";
echo "<td>" . $row['ServiceType_ID'] . "</td>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['RootCause_ID'] . "</td>";
echo "<td>" . $row['RiskRating_ID'] . "</td>";
echo "<td>" . $row['Impact_ID'] . "</td>";
echo "<td>" . $row['Efforts_ID'] . "</td>";
echo "<td>" . $row['Likelihood_ID'] . "</td>";
echo "<td>" . $row['Finding'] . "</td>";
echo "<td>" . $row['Implication'] . "</td>";
echo "<td>" . $row['Recommendation'] . "</td>";
echo "<td>" . $row['Report_ID'] . "</td>";
//echo "<td><a href='edit.php'>[EDIT]</a> <a href='delete_risk.php?risk_no=" . $row['risk_no'] . "'>[DELETE]</a></td>";
echo "</tr>";
}
mysql_close($con);
?>
<input type="button" value="Back" onclick="window.location.href='option_Frame.php'" />
</body>
</html
>
It sounds like $_POST['Services'] was not posted rather than this being a database issue.
Check your HTML; there is probably no input element with name="Services".

Categories