I tailored this page mysql.php but for the time being it appears blank-white on the browser.
<html>
<head>
<title> MySQL Test </title>
</head>
<body>
<?php
$username = "username";
$password = "pass";
$hostname = "localhost";
$dbname = "StudentLessons";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password, $dbname)
or die("Unable to connect to MySQL");
echo "Connected to MySQL <br>";
$query = "SELECT * FROM Teacher GROUP BY bathmida";
$result = mysql_query($query);
echo "<table>"; // start table in html
while($row = mysql_fetch_array($result)) { //loop through results
echo "<tr><td>" . row['onoma'] . "</td><td>" . row['epitheto'] "</td><td>" . row['bathmida'] "</td></tr>"; //$row['index'] is a filed name
}
echo "</table>"; //close table
mysql_close(); //close the db connection
?>
</body>
</html>
Now I know for a fact that
The connection is legit.
The query is working because I tested them both.
I also advised another question here on [so] and it seems I am missing something important. The <tr><td> tags in HTML are set as they should be, so what might be the problem?
By the way, isn't there any other way to automate the process a bit? i.e. to develop a form that can support more queries etc.?
You forgot to add $ in below code
while($row = mysql_fetch_array($result)) { //loop through results
echo "<tr><td>" . row['onoma'] . "</td><td>" . row['epitheto'] "</td><td>" . row['bathmida'] "</td></tr>"; //$row['index'] is a filed name
}
correct code is:
while($row = mysql_fetch_array($result)) { //loop through results
echo "<tr><td>" . $row['onoma'] . "</td><td>" . $row['epitheto'] "</td><td>" . $row['bathmida'] "</td></tr>"; //$row['index'] is a filed name
}
You miss $, and you must use like this : $row->onoma
echo "<tr><td>" . $row->onoma . "</td><td>" . $row->epitheto"</td></tr>
Related
So I have a HTML table that grabs SQL data and shows the data in the HTML table. I have a button in the HTML table aswell witch reredicts to update.php. Here I want the row that the user pressed the button in updates the column "paid" to "Paid". Screenshot of the HTML table: https://emildeveloping.com/screenshots/outredden-pardonableness-amharic.png
The thing is that I can't get it working that it updates just that specific row, it updates all rows right now.
Ive tried searching around for same questions but haven't found any solutions.
This is the PHP code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emildeveloping2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE purchases SET paid='Paid'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
This is the PHP snippet of the HTML Table
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table id='purchases' class='purchases'><tr class='header'><th>Invoice ID</th><th>Customer ID</th><th>Product</th><th>Name</th><th>Email</th><th>Adress</th><th>Security Number</th><th>City</th><th>Zip Code</th><th>Country</th><th>Cost</th><th>Payment Plan</th><th>Status</th><th>Options</th></tr>";
// Visa datan
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["customerid"]. "</td><td>" . $row["product"]. "</td><td>" . $row["name"]. "</td><td>" . $row["email"]. "</td><td>" . $row["adress"]. "</td><td>" . $row["securitynumber"]. "</td><td>" . $row["city"]. "</td><td>" . $row["zipcode"]. "</td><td>" . $row["country"]. "</td><td>" . $row["cost"]. "</td><td>" . $row["paymentplan"]. "</td><td>" . $row["paid"]. "</td><td><a class='fas fa-check-square' href='update.php'></a></td></tr>";
}
echo "</table>";
} else {
echo "There is no active calls.";
}
I want just a specific row to update where id = id.
You need to update two things:
Add the id of the row you want to update as a GET argument in the link so it is passed to PHP script:
<a class='fas fa-check-square' href='update.php?row_id='.$row["id"].'>
Now if you check the links, each of them should be unique: update.php?row_id=1, update.php?row_id=2, etc.
Handle this added url parameter in the PHP script so it is used to select desired
row in database table:
$sql = "UPDATE purchases SET paid='Paid' WHERE id=".$_GET['row_id'];
If you get it working use http://php.net/manual/en/mysqli.prepare.php prepare method instead of query, so your code is not prone to sql injecton.
Something like this:
$mysqli->prepare("UPDATE purchases SET paid='Paid' WHERE id=?")) {
$stmt->bind_param("i", $_GET['row_id']);
$stmt->execute();
Another thing would be using POST requests instead of GET links to prevent CSRF vulnerability.
I'm learning PHP right now and connecting to MySQL. I'm able to connect to a table called "info" and display the "date", "time", and "data" of each row, along with 2 extra columns that will allow the user to download the respective "data" into a txt and csv. This is where I'm completely lost on how to achieve that.
My understanding is that since it'll be a clickable link, I will have to use "href=" and will link that to a separate php that I create that will download that row's data, right? But then how does that new php file know which row to download from? I'm guessing I need to pass the row number it is to the other file?
Greatly appreciate it if anyone can lead me in the right direction or have examples. Thanks!
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT date, time, data FROM info";
$result = $conn->query($sql);
echo "<table border='1'>";
echo "<tr><td> Date </td><td>Time</td><td>Data</td><td> Download text </td><td> Download csv</td>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["date"]. "</td><td>" . $row["time"]. "</td><td>" .$row["data"] ."</td><td><a href='NEEDTXTFILE.php'>".$row["date"].'.txt'. "</a></td>"."<td><a href='NEEDCSVFILE.php'>".$row["date"].'.csv'. "</a></td> ";
echo "</tr>";
}
echo "</tr>";
echo "</table>";
} else {
echo "0 results";
}
Assuming you select also the id value
$sql = "SELECT id, date, time, data FROM info";
....
you could add the id to your href eg:
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["date"]. "</td><td>" .
$row["time"]. "</td><td>" .
$row["data"] .
"</td><td><a href='NEEDTXTFILE.php?id=" . $row["id"] . "'>".
$row["date"].'.txt'. "</a></td>"."<td><a href='NEEDCSVFILE.php'>".
$row["date"].'.csv'. "</a></td> ";
echo "</tr>";
}
then in your NEEDTXTFILE.php you can obtain the id in $_GET['id']
I am new to PHP and MySQL and hope someone can help me with this.
I have a MySql db with a table "TranslationsMain" and the following PHP query.
This returns all the items from column "German" (incl. the corresponding ID) from the db table and works correctly so far.
Instead of echoing the whole list (I just did this here for testing), **how can I store the results in an array in a way that each item within the array is stored with its value and a unique ID + how I can I echo specific items from this array by referring to their ID ?
Example:
I would like to echo the item value for ID "xyz" from this array (without calling the db again since this will be needed for multiple items on the page).
My PHP:
<?php
require_once("includes/header.php");
$tblTranslations = "TranslationsMain";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM " . $tblTranslations;
$translations = $conn->query($sql);
// for testing only
if($translations->num_rows > 0){
echo "<table><tr><th>ID</th><th>Translation</th></tr>";
while($translation = $translations->fetch_assoc()){
echo "<tr><td>" . $translation["ID"] . "</td><td>" . $translation["German"] . "</td></tr>";
}
echo "</table>";
}else{
echo "0 results";
}
$conn->close();
?>
Update:
What I am looking for here is how to create a two dimensional array and how to fetch from it.
Many thanks in advance,
Mike
if($translations->num_rows > 0){
$result_arr = array();
echo "<table><tr><th>ID</th><th>Translation</th></tr>";
while($translation = $translations->fetch_assoc()){
echo "<tr><td>" . $translation["ID"] . "</td><td>" . $translation["German"] . "</td></tr>";
$result_arr[] = $translation;
}
echo "</table>";
}else{
echo "0 results";
}
// now you can iterate $result_arr
foreach($result_arr as $row){
echo "<tr><td>" . $row["ID"] . "</td><td>" . $row["German"] . "</td></tr>";
}
I've commented out the bottom part, and the SQL query works fine. Its the displaying of the query where the error is coming from i believe.
$host = "127.0.0.1";
$user = "root";
$pass = "Toom13371!";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
// 2. Selecting DB.
$dbname = "filters";
mysql_select_db($dbname);
// 3. Build/Test SQL Query
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
//echo $sql; //Comment/Uncomment to test sql query.
// 4. Retrieve info from MySQL.
$query = mysql_query($sql);
// 5. Display Query.
echo "<table border='1'>
<tr>
<th>Low Frequency</th>
<th>High Frequency</th>
</tr>";
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Any help would be appreciated, I'm sure it's going to be some small stupid error i've over looked.
Thanks in advance :)
I'm guessing, based on your query, that you need to change this
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
and
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
to
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['start_passband'] . "</td>";
echo "<td>" . $row['stop_passband'] . "</td>";
echo "</tr>";
}
Change line
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
Also before query check
$_POST['Lowfreq'] and $_POST['Highfreq']
If there is no value in these variables query will must return empty.
In your query there should not brackets for string.
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
should be:
$sql = "select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'";
I am using 2 pages. On one page it generates a table with the records and a delete button. After pressing delete it goes to the second page which should delete the record. But it doesn't. Below is the code that I am using.
PS: The code is adapted from a tutorial I found through Google a while ago.
delete_overzicht.php
<?php
// Load Joomla! configuration file
require_once('../../../configuration.php');
// Create a JConfig object
$config = new JConfig();
// Get the required codes from the configuration file
$server = $config->host;
$username = $config->user;
$password = $config->password;
$database = $config->db;
// Connect to db
$con = mysqli_connect($server,$username,$password,$database);
if (!$con){
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,$database);
// Get results
$result = mysqli_query($con,"SELECT * FROM cypg8_overzicht");
echo "<table border='1' id='example' class='tablesorter'><thead><tr><th>Formulier Id</th><th>Domeinnaam</th><th>Bedrijfsnaam</th><th>Datum</th><th>Periode</th><th>Subtotaal</th><th>Dealernaam</th><th>Verwijderen</th></tr></thead><tbody>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['formuliernummer'] . "</td>";
echo "<td>" . $row['domeinnaam'] . "</td>";
echo "<td>" . $row['bedrijfsnaam'] . "</td>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['periode'] . "</td>";
echo "<td> € " . $row['subtotaal'] . "</td>";
echo "<td>" . $row['dealercontactpersoon'] . "</td>";
echo "<td><a href='delete.php?id=" . $row['id'] . "'>Verwijderen </a></td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
delete.php
<?php
// Load Joomla! configuration file
require_once('../../../configuration.php');
// Create a JConfig object
$config = new JConfig();
// Get the required codes from the configuration file
$server = $config->host;
$username = $config->user;
$password = $config->password;
$database = $config->db;
// Connect to db
$con = mysqli_connect($server,$username,$password,$database);
if (!$con){
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,$database);
// Check whether the value for id is transmitted
if (isset($_GET['id'])) {
// Put the value in a separate variable
$id = $_GET['id'];
// Query the database for the details of the chosen id
$result = mysqli_query($con,"DELETE * FROM cypg8_overzicht WHERE id = $id");
} else {
die("No valid id specified!");
}
?>
Thanks to everyone who is willing to help!
The following answer comes from the user Andrewsi. This answer was posted in the comments.
The syntax for DELETE is DELETE FROM - you need to remove the *