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';
?>
Related
I am new to using database with php, using 000webhost I don't know what to write to get data from specific table
connection.php
<?php
$servername = "localhost";
$username = "*****************";
$password = "***********";
$database = "*****************";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
getalldata.php
<?php
include 'connection.php';
//get data from Database
?>
Let suppose you are fetching records from studentrecord
require_once 'connection.php';
$roll = $_POST['roll'];
$query = "SELECT * from studentrecord where roll = '$roll'";
try{
$statement = $conn->query($query);
echo "<table border='1'>
<tr>
<td>Roll</td>
<td>Stream</td>
<td>Session</td>
<td>Name</td>
<td>Mother Name</td>
<td>Father Name</td>
<td>Total Mark's</td>
<td>Grade</td>
<td>Result</td>
</tr>";
//if you want to fetch records as an array indexed
while ($result = $statement->fetch()){
echo "<tr>";
echo "<td>" . $result['id'] . "</td>";
echo "<td>" . $result['stream'] . "</td>";
echo "<td>" . $result['session'] . "</td>";
echo "<td>" . $result['name'] . "</td>";
echo "<td>" . $result['mother_name'] . "</td>";
echo "<td>" . $result['father_name'] . "</td>";
echo "<td>" . $result['total_marks'] . "</td>";
echo "<td>" . $result['grade'] . "</td>";
echo "<td>" . $result['result'] . "</td>";
echo "</tr>";
}
//if you want to fetch records as an anonymous object
while ($result = $statement->fetchObject()){
echo "<tr>";
echo "<td>" . $result->id . "</td>";
echo "<td>" . $result->stream . "</td>";
echo "<td>" . $result->session . "</td>";
echo "<td>" . $result->name . "</td>";
echo "<td>" . $result->mother_name . "</td>";
echo "<td>" . $result->father_name . "</td>";
echo "<td>" . $result->total_marks . "</td>";
echo "<td>" . $result->grade . "</td>";
echo "<td>" . $result->result . "</td>";
echo "</tr>";
}
echo "</table>";
}
catch (PDOException $ex) {
echo "Failed to retrieve record".$ex->getMessage();
`enter code here`}
I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down but I cannot seem to get all of the information to go into the table.
The formatting will only allow one row to be entered into the table and then the rest of the row go below in normal text format.
How would I go about getting all the information to sit in the table and automatically update when more information is added?
<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('******');
$query = mysql_query("SELECT * FROM tablename");
echo "<table border='1'>
<tr>
<td>Date</td>
<td>Region</td>
<td>Cameraman</td>
<td>Livestream?</td>
<td>Event Title</td>
<td>Lecturer</td>
<td>Time</td>
<td>Speaker</td>
<td>ICE Contact</td>
<td>Venue Address</td>
<td>Venue Contact</td>
<td>Additional Comments</td>
<td>On App?</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
$date = $rows['date'];
$region = $rows['region'];
$cameraman = $rows['cameraman'];
$livestream = $rows['livestream'];
$eventtitle = $rows['eventitle'];
$lecturer = $rows['lecturer'];
$time = $rows['time'];
$speaker = $rows['speaker'];
$icecontact = $rows['icecontact'];
$venuecontact = $rows['venueaddress'];
$venuecontact = $rows['venuecontact'];
$additionalcomments = $rows['additionalcomments'];
$onapp = $rows['onapp'];
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
echo "</table>";
endwhile;
?>
http://cpdonline.tv/spreadsheet/spreadsheet.php
You made two boo boo's in your code:
1: You are not using these variables, delete them
$date = $rows['date'];
$region = $rows['region'];
$cameraman = $rows['cameraman'];
$livestream = $rows['livestream'];
$eventtitle = $rows['eventitle'];
$lecturer = $rows['lecturer'];
$time = $rows['time'];
$speaker = $rows['speaker'];
$icecontact = $rows['icecontact'];
$venuecontact = $rows['venueaddress'];
$venuecontact = $rows['venuecontact'];
$additionalcomments = $rows['additionalcomments'];
$onapp = $rows['onapp'];
2: You are closing the table inside of the while loop
echo "</table>";
endwhile;
Should be:
endwhile;
echo "</table>";
Complete newbie to PHP/MySQL and HTML so please bear with me if I dont explain myself properly. At present I have an order form which stores the order in my database and shows it on a table in my admin area. I would like to be able to delete the row from the table and my database when I have completed the order.
At present my code looks like this:
<?php
//87229feely.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//deleterow.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: 87229feely.php");
?>
Any help? All greatly appreciated. Thanks
This answer does not meet security standards but should do the job:
<?php
//index.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//delete.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: index.php");
?>
Many ways to do this. Since you're a beginner, this would probably be the most straight-forward (albeit not how I would do it)
Create a form around the table
Add a button on the delete column for each row and assign an id to it (the ID should be the ID from your database) <input type="submit" name="submit" value"/*YOUR ID*/">
Add some processing script before the table is being parsed
if (isset($_POST['submit'])) {
$sql = "DELETE FROM table WHERE id='/*YOUR ID*/'";
mysql_query($sql);
}
First of all ,you need to send ID of completed order to next form. You cam do this by adding:
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");
That will create a hidden field with value of ID.
If your form uses POST:
then:
if(isset($_POST['submit'])){
$orderID = $_POST['orderID'];
mysql_query("DELETE FROM table WHERE id=$oderID");
}
If you are using GET method:
<form method="GET">
You could either use hidden field as mentioned above or you could parse ID of order in GET url:
<form action='action.php?id=".$row['id']."'>
and after submitting:
if(isset($_GET['submit']) && isset($_GET['id')){
$orderID = $_GET['id'];
mysql_query("DELETE FROM table WHERE id=$orderID");
}
Maybe something like this with PDO
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </th>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$row = $conn->query('SELECT * FROM orderform');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"id[]\" id=\"checkAll\" value=\"".$row['id']."\" /></td>"
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "</tr>";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
</table>
<input type="submit" name="submit" value="submit" />
</form>
delete.php
<?php
$id
if(isset($_POST['submit']) {
include('connection.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="DELETE FROM orderform WHERE id IN (".implode(',',$conn->quote($id)).")";
$stmt->exec($sql);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
I am working on this web application that needs to send a certain report. The report is dynamic however and it is in a table format. The thing i hope to do now is to email the whole table via email to someone. Is there any way I can do so?
php script
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$input = $_POST['id'];
$query = mysql_query("SELECT * FROM table WHERE ID = '$input'");
echo "<table border='5' align=center>
<tr>
<th>ID</th>
<th>Type</th>
<th>Setting</th>
<th>Value</th>
<th>Actual</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['Setting'] . "</td>";
echo "<td>" . $row['Value'] . "</td>";
echo "<td>" . $row['Actual'] . "</td>";
echo "</tr>";
if($row['Value'] != $row['Actual'])
{
echo "<td bgcolor='#FD2911'>" . "X" . "</td>";
}
else
{
echo "<td bgcolor='#1CDC15'>" . "O" . "</td>";
}
}
echo "<br>";
mysql_close($con);
?>
html code
<form action="query1.php" method="POST">
Enter the choice: <input type="varchar" name="id" /><br />
<input type="submit" value="Audit" />
</form>
You concatenate the whole table:
$table = '';
$table .= "<table border='5' align=center>
<tr>
<th>ID</th>
<th>Type</th>
<th>Setting</th>
<th>Value</th>
<th>Actual</th>
</tr>";
while($row = mysql_fetch_array($query))
{
$table .= "<tr>";
$table .= "<td>" . $row['ID'] . "</td>";
$table .= "<td>" . $row['Type'] . "</td>";
$table .= "<td>" . $row['Setting'] . "</td>";
$table .= "<td>" . $row['Value'] . "</td>";
$table .= "<td>" . $row['Actual'] . "</td>";
$table .= "</tr>";
if($row['Value'] != $row['Actual'])
{
$table .= "<td bgcolor='#FD2911'>" . "X" . "</td>";
}
else
{
$table .= "<td bgcolor='#1CDC15'>" . "O" . "</td>";
}
}
$table .= "</table><br>";
mail('to#example.com', 'My Subject', $table);
if you are wanting to do more than the table, you could use output buffering;
<?php
ob_start();
# all your code/page/table(s) with echos and all
$toBeSent = ob_get_contents();
ob_end_clean();
?>
then send the entire contents between ob_start and ob_end_clean via email by replacing $table in the mail() line of Mihai's post with $toBeSent.
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".