Executing php code by clicking a link - php

Good people i am stuck with an issue that i know has been discussed here before. I am displaying mysql data as a link and want to load more data from the database when a user clicks the link, I looked at all the previous questions and answers but i can't sort this one out. Here is how far i have managed to drag myself:
index.php:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT topic FROM steps";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$topic = isset($_get['topic']);
$id = isset($_get['id']);
echo ''.$row['topic'].'<br/>';
}
}
$conn->close();
?>
moreinfo.php:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = isset($_get['id']);
$sql = "SELECT * FROM steps WHERE id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$topic = isset($_get['topic']);
echo "Description: " . $row['topic'] . "<br />";
}
}
else{
echo "123";
}
$conn->close();
?>
This doesn't give me any error, just that the hyperlinks on this line
echo ''.$row['topic'].'<br/>';
on index.php does not append an id ($id) to the hyperlink so that moreinfo.php can execute correctly.
I rily pray i'm making some sense here...

Use $_GET['id'] insted of $_get['id']
Correct usege of isset is looks like this
$id = isset($_GET['id'])?$_GET['id']:false
Always check if parameter which you will use next was really passed
$id = isset($_GET['id'])?$_GET['id']:false;
if(!$id)
die('id was not passed');
And last, you were overwriting variables from db with data from GET which should be on first page (probably copy and past )
$topic = isset($_get['topic']);
$id = isset($_get['id']);
here is your fixed code:
index.php
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
$sql = "SELECT * FROM steps";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo ''.$row['topic'].'<br/>';
}
}
$conn->close();
?>
moreinfo.php
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
$id = isset($_GET['id'])?$_GET['id']:false;
if(!$id)
die('id was not passed');
$sql = "SELECT * FROM steps WHERE id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$topic = isset($_get['topic']);
echo "Description: ".$row['topic']."<br />";
}
} else {
echo "123";
}
$conn->close();
?>
sql table create statement
CREATE TABLE `steps` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`topic` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

Ok first thing first you are overwriting your id in this line
$id = isset($_get['id']);
so when you try to use it it holds bool value (true or false) instead of actual $id. And you do it in both of your files.

Related

Get rows from database

I have a connection where i want to get some data from my database.
I have inserted some data but now i want to retreive it but i get NULL.
I have no idea why.
<?php
require "connect.php";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM tbltemperature ORDER BY time DESC LIMIT 1";
$result = $conn->query($sql);
var_dump($results);
$t = 0;
while($row = $result->fetch_assoc()) {
$weather[] = array(
$row["time"],
$row["inside_temperature"]
);
echo $row["time"];
echo $row["inside_temperature"];
}
$conn->close();
?>
check var_dump($results); , it looks like it should be var_dump($result);
otherwise you should check $result->num_rows() first to know if there is any row available.
Firstly you have to check your query in phpmyadmin query is working or not. If working you should try to var_dump($result); and after
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> time: ". $row["time"]. " - inside_temperature: ". $row["inside_temperature"]."<br>";
}
} else {
echo "0 results";
}

how can I display all the data from database using an array

I'm trying to connect $countries to an array that will display all the data from my database, it only displays the last data I entered.Is there anyway I can display all of them?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries = $tom;
}
} else {
echo "0 results";
}
$conn->close();
because you have assinged data to $countries with wrong manner:
$countries = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries[] = $tom; // use []
}
} else {
echo "0 results";
}
$countries[] = $tom;
With [] at the end of inicialization of variable you add a row to that array. If you do it without [], it's just inicialization every time in while loop.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
$countries[] = " ". $row["quantity"] . $row["product_name"];
} else {
echo "0 results";
}
$conn->close();
// create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
// get the data
$sql = "SELECT product_id, product_name, quantity FROM inventory";
$countries = $conn->query($sql)->fetch_all();
Although it's only available with mysqlnd installations, you need one, because without mysqlnd mysqli is unusable anyway.

PHP if SteamID is equal to the MYSQL Row

It will not work. I'm trying like below.
if (($row["steamid"]) == $steamprofile['steamid']){
Below is full code snippet.
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$eh = $steamprofile[steamid];
$sql = "SELECT steamid FROM Main";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (($row["steamid"]) == $steamprofile['steamid']){
echo "SteamID Is Equal and Created!";
}
}
} else {
echo "Nope?";
}
$conn->close();
?>
Ive tried various methods.
// Create connection
$conn = new mysqli("localhost", "root", "", "stackoverflow");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//assume 1
$steamprofile['steamid']=1;
// changes here single quote
$eh = $steamprofile['steamid'];
$sql = "SELECT steamid FROM Main";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
if (($row["steamid"]) == $steamprofile['steamid']){
echo "SteamID Is Equal and Created!";
}
}
} else {
echo "Nope?";
}
$conn->close();
?>
output is
SteamID Is Equal and Created!
Mysql Database------------
CREATE TABLE IF NOT EXISTS main (
steamid int(11) NOT NULL,
name varchar(500) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table main
INSERT INTO main (steamid, name) VALUES
(1, 'test1'),
(2, 'test2');
I think your
$eh = $steamprofile[steamid];
should be changed to
$eh = $steamprofile['steamid'];
The comment by:
try echo $row["steamid"]) ."==". $steamprofile['steamid']; so you can print and check that. – Hardy Mathew
Worked. Thanks.

PHP SELECT * FROM not working

I am trying to query data from a table using the following script:
//connect file
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//connect file included above - ECHO tested and it is connected as $conn
$sql = "SELECT * FROM userInfo";
$results = $conn->query($sql);
if (!$results) {
printf("Errormessage: %s\n", $conn->error);
exit;
} else {
echo $row['username'];
}
UPDATE --
It now no longer tries to throw an error and seems to go to the else section; however, no echo - and the spelling is correct this time and the column is filled.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["username"];
}
} else {
echo "0 results";
}
This now returns results. Thank you #Fred -ii- especially for your help on this.
Also thanks #jjczopek for the error checking advice!

update in MYSQL using php and login

I am doing an update of values inside a MySQL database using PHP
and here is my code to update
$id = $_REQUEST['uid'];
$name = $_REQUEST['name'];
$company = $_REQUEST['company'];
$contact = $_REQUEST['contact'];
$email = $_REQUEST['email'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
echo "$id "."$name". "$company" . "$contact" . "$email";
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else
{
$sql = "UPDATE `users` SET `userName`='$name',`userEmail`='$email',`userCompany`='$company',`userContact`='$contact' WHERE userID = $id";
if (mysqli_query($conn, $sql))
{
mysqli_commit($conn);
echo "success";
}
else
{
echo "error";
}
}
mysqli_close($conn);
it does the update and changes the value in the db.
But when I login using the previous username and password, it still accepts it
code for login
$uname= $_REQUEST['loginusername'];
$pword= $_REQUEST['loginpassword'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
{
$sql = "SELECT * FROM `users` WHERE userName = '$uname' AND userPassword = '$pword'";
$return = mysqli_query($conn, $sql);
if(mysqli_num_rows($return) > 0)
{
echo 'found';
}
else
{
echo 'not found';
}
}
$conn->close();
thanks in advance
You don't update your Password field in
UPDATE `users` SET `userName`='$name',`userEmail`='$email',`userCompany`='$company',`userContact`='$contact' WHERE userID = $id
and it isn't a good practice to save clear text passwords. Its better to hash it with an hash algorithm (for example sha256) and salt it.

Categories