Php print format or print php value to html scope - php

hello i am new in php and im trying to print some values from my sql table to an article in html i use article because this is the structure i need . as you will see bellow i basically want to take the path and the username from my sql table and print them to my article any suggestions?
<html >
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Corporate 1</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Logo and responsive toggle -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
<span class="glyphicon glyphicon-fire"></span>
Logo
</a>
</div>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav">
<li class="active">
Home
</li>
<li>
About
</li>
<li>
Products
</li>
<li class="dropdown">
Services <span class="caret"></span>
<ul class="dropdown-menu" aria-labelledby="about-us">
<li>Engage</li>
<li>Pontificate</li>
<li>Synergize</li>
</ul>
</li>
</ul>
<!-- Search -->
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<div class="jumbotron feature">
<div class="container">
<h1><span class="glyphicon glyphicon-equalizer"></span><font color="#F0FFFF" style="Impact">Welcome to Aegean Community</font></h1>
<p><font color="#E9967A">Community for hope</font></p>
<p><a class="btn btn-default" href="LogIn.php">Engage Now</a></p>
</div>
</div>
<!-- Content -->
<div class="container">
<!-- Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Superior Collaboration
<small>Visualize Quality</small>
</h1>
<p>Proactively envisioned multimedia based expertise and cross-media growth strategies. Seamlessly visualize quality intellectual capital without superior collaboration and idea-sharing. Holistically pontificate installed base portals after maintainable products.</p>
</div>
</div>
<!-- /.row -->
<!-- Feature Row -->
<div class="row">
<!-- Feature Row -->
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "some";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM information order by Ranking desc LIMIT 3 ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<article class="col-md-4 article-intro">
<a href="#">
<img class="img-responsive img-rounded" src=" <?php echo $row['imgagePath'] ?>" alt="">
</a>
<h3>
<?php echo $row['username'] ?>
</h3>
</article>
<?php
}
?>
</div>
</div>
</body>
</html>

You need to use echo $row['var'] to tell PHP to insert that value on the page as a string.
Just change:
<?php $row['imgagePath'] ?>
to
<?php echo $row['imgagePath']; ?>
and
<?php $row['username'] ?>
to
<?php echo $row['username']; ?>
That should be about all you need to do

The major error here is not using an outputting the collected data per $row using echo and you're also missing the semicolon ;.
So changing <?php $row['username'] ?> to <?php echo $row['username'] ?> and <?php $row['imgagePath'] ?> to <?php echo $row['imgagePath']; ?> should do the trick, I believe that's major error you're encountering.
Also instead of using while ($row = $result->fetch_assoc()) { you're better off using while ($row = $result->fetch_assoc()): then you can simply replace the closing bracket } with endwhile;. The same goes for if (expr): and endif;.
Altogether it should look something like this:
<!-- Feature Row -->
<div class="row">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "some";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM information order by Ranking desc LIMIT 3 ";
$result = $conn->query($sql);
if ($result->num_rows > 0):
// output data of each row
while ($row = $result->fetch_assoc()):
?>
</article>
<article class="col-md-4 article-intro">
<a href="#">
<img class="img-responsive img-rounded" src=" <?php echo $row['imgagePath']; ?>" alt="">
</a>
<h3>
<?php echo $row['username']; ?>
</h3>
</article>
<?php endwhile; ?>
</div>
...
<?php endif; ?>

You should use echo before the variable that you want to print to HTML.
src=" <?php $row['imgagePath'] ?>"
should become
src=" <?php echo $row['imgagePath']; ?>"

Related

How to have a mysql query print out in bootstrap cards alongside each other

I am trying to get a mysql query of results to print out alongside each other, say 3 in a row and not one below the other.
I have tried stack overlow answers and I cannot find one that helps
<?php
include('db.php');
$results = mysqli_query($conn, "SELECT * FROM contacts");
if(isset($_GET['edit'])){
$id = $_GET['edit'];
$edit_state = true;
$rec =mysqli_query($db_connection, "SELECT * FROM contacts WHERE id=$id ");
$record = mysqli_fetch_array($rec);
$name = $record['name'];
$contact = $record['contact'];
$email = $record['email'];
$id =
$record['id'];
}?>
<div class="container">
<h1 class="my-4">Welcome to Modern Business</h1>
<!-- Marketing Icons Section -->
<?php while ($row = mysqli_fetch_array($results)){ ?>
<div class="row">
<div class="col-lg-4 mb-4">
<div class="card h-100">
<h4 class="card-header"><?php echo $row['name']; ?></h4>
<img class="img_size" src="images/dog2.jpg" alt="Card image top">
<div class="card-body">
<p class="card-text"><?php echo $row['contact']; ?></p>
</div>
<div class="card-footer">
Learn More
</div>
</div>
</div>
</div>
<?php } ?>
right now, the cards are printing out but not inline, one below the other. I would like them to print horizontally.
any help at all would be appreciated, I am new to php.
This is the wrong outcome, I want them beside each other
PHP code:
<?php
include('dbconn.php'); // include database connection
$output="";
$selQuery = "select * from tbl_name";
$exeQuery = mysqli_query($conn,$selQuery);
$output="<h2>Welcome to Modern Business
<div class='container'>
<div class='row'>";
while($row = mysqli_fetch_array($exeQuery)) {
$name = $row['name'];
$desciption = $row['desciption'];
$ephoto = $row['ephoto'];
$output.="<div class='col-sm-4'>
<h4 class='card-header'>$name</h4>
<img class='img_size' src='$ephoto' alt='Card image top'>
<p>$desciption</p>
<a href='#' class='btn btn-primary'>Learn More</a>
</div>";
}
$output.="</div></div></h2>";
echo $output;
?>
HTML markup including bootstrap links:
<html>
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
</html>
You need to put the while loop before the following div and end it when div ends.
<?php while($row=mysqli_fetch_array($results)){ ?>
<div class="card h-100">
<h4 class="card-header">
<?php echo $row['name']; ?></h4>
<img class="img_size" src="images/dog2.jpg" alt="Card image top">
<div class="card-body">
<p class="card-text"><?php echo $row['contact']; ?></p> </div>
<div class="card-footer">
<a href="#" class="btn btn-primary">
Learn More
</a>
</div>
</div>
<?php } ?>
Also replace col-lg-4 with col-lg-12
Try this and update me.

How can I use header() function to redirect to index if login is successful?

I have created a website and am implementing login functionality. I have created separate php file for main menu and am including it as required. When I try to login it says header has already been sent. I am new to php and wanted to implement master page and child pages but session seeems to create a problem.
Tried storing Request_URI in session and redirect using header for index to login if not logged in and from login to index on successfull login.
index page
Login page
<?php include('includes/session/mysession.php') ?>
<?php include('includes/navigation/navigation.php') ?>
<?php include('DbConnections/config.php') ?>
<section id="login-page-wrap" class="section-padding">
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-8 m-auto">
<div class="login-page-content">
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$uname = $_POST['username'];
$pwd = $_POST['password'];
if (empty($uname) || empty($pwd)) {
$output = "Please enter all fields.";
echo 'output : ' . $output;
} else {
$uname = $mysqli->real_escape_string($uname);
$pwd = $mysqli->real_escape_string($pwd);
echo $uname;
echo $pwd;
$query = $mysqli->query("SELECT id FROM users
WHERE username = '$uname'
AND password = md5('$pwd')");
echo 'query done';
if ($query->num_rows == 0) {
$output = "Invalid username/password";
// echo $output;
} else {
echo 'Success';
$_SESSION['loggedin'] = TRUE;
$_SESSION['user'] = $uname;
$output = "WELCOME " . $_SESSION['user'];
// On successful login
$redirect = $_SESSION['redirect_to'];
// unset the session var
unset($_SESSION['redirect_to']);
header("Location: $redirect");
exit();
// header('location: index.php');
// echo $output;
}
}
}
//echo 'output : ' . $output;
?>
<?php
if (!isset($_SESSION['loggedin'])) { ?>
<div class="login-form">
<h3>Welcome !</h3>
<form method="POST">
<div class="username">
<input name="username" type="text" placeholder="Email or Username">
</div>
<div class="password">
<input name="password" type="password" placeholder="Password">
</div>
<div class="log-btn">
<button type="submit"><i class="fa fa-sign-in"></i> Log In</button>
</div>
</form>
</div>
<?php
} else {
echo 'Done.';
}
?>
</div>
</div>
</div>
</div>
</section>
config.php
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
//pass variables for values
$dbserver = 'localhost';
$dbusername = 'root';
$dbpassword = '';
$dbname = 'cs';
define('DB_SERVER', $dbserver);
define('DB_USERNAME', $dbusername);
define('DB_PASSWORD', $dbpassword);
define('DB_NAME', $dbname);
/* Attempt to connect to MySQL database */
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if ($mysqli === false) {
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
?>
mysession.php
<?php session_start();?>
mainmenu.php
<?php
$HomeActive = '';
$ServiceActive = '';
$CarsActive = '';
$AboutActive = '';
$BlogActive = '';
$PagesActive = '';
$ContactsActive = '';
?>
<div class="col-lg-8 d-none d-xl-block">
<nav class="mainmenu alignright">
<ul>
<li class="<?php echo $HomeActive; ?>">Home
<ul>
<li>Home 1</li>
<li>Home 2</li>
<li>Home 3</li>
</ul>
</li>
<li class="<?php echo $AboutActive; ?>">About</li>
<li class="<?php echo $ServiceActive; ?>">services</li>
<li class="<?php echo $CarsActive; ?>">Cars
<ul>
<li>Car Left Sidebar</li>
<li>Car Right Sidebar</li>
<li>Car Without Sidebar</li>
<li>Car Details</li>
</ul>
</li>
<li class="<?php echo $PagesActive; ?>">Pages
<ul>
<li>Pricing</li>
<li>Driver</li>
<li>FAQ</li>
<li>Gallery</li>
<li>Help Desk</li>
<li>Log In</li>
<li>Register</li>
<li>404</li>
</ul>
</li>
<li class="<?php echo $BlogActive; ?>">Blog
<ul>
<li>Blog Page</li>
<li>Blog Details</li>
</ul>
</li>
<li class="<?php echo $ContactsActive; ?>">Contact</li>
</ul>
</nav>
</div>```
navigation.php
<!DOCTYPE html>
<html cla s s="no-js" la n g="zx x">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--=== Favicon ===-->
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<title>Cardoor - Car Rental HTML Template</title>
<!--=== Bootstrap CSS ===-->
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<!--=== Slicknav CSS ===-->
<link href="assets/css/plugins/slicknav.min.css" rel="stylesheet">
<!--=== Magnific Popup CSS ===-->
<link href="assets/css/plugins/magnific-popup.css" rel="stylesheet">
<!--=== Owl Carousel CSS ===-->
<link href="assets/css/plugins/owl.carousel.min.css" rel="stylesheet">
<!--=== Gijgo CSS ===-->
<link href="assets/css/plugins/gijgo.css" rel="stylesheet">
<!--=== FontAwesome CSS ===-->
<link href="assets/css/font-awesome.css" rel="stylesheet">
<!--=== Theme Reset CSS ===-->
<link href="assets/css/reset.css" rel="stylesheet">
<!--=== Main Style CSS ===-->
<link href="style.css" rel="stylesheet">
<!--=== Responsive CSS ===-->
<link href="assets/css/responsive.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body class="loader-active">
<!--== Preloader Area Start ==-->
<!-- <div class="preloader">
<div class="preloader-spinner">
<div class="loader-content">
<img src="assets/img/preloader.gif" alt="JSOFT">
</div>
</div>
</div> -->
<!--== Preloader Area End ==-->
<!--== Header Area Start ==-->
<header id="header-area" class="fixed-top">
<!--== Header Top Start ==-->
<div id="header-top" class="d-none d-xl-block">
<div class="container">
<div class="row">
<!--== Single HeaderTop Start ==-->
<div class="col-lg-3 text-left">
<i class="fa fa-map-marker"></i> 802/2, Mirpur, Dhaka
</div>
<!--== Single HeaderTop End ==-->
<!--== Single HeaderTop Start ==-->
<div class="col-lg-3 text-center">
<i class="fa fa-mobile"></i> +1 800 345 678
</div>
<!--== Single HeaderTop End ==-->
<!--== Single HeaderTop Start ==-->
<div class="col-lg-3 text-center">
<i class="fa fa-clock-o"></i> Mon-Fri 09.00 - 17.00
</div>
<!--== Single HeaderTop End ==-->
<!--== Social Icons Start ==-->
<div class="col-lg-3 text-right">
<div class="header-social-icons">
<i class="fa fa-behance"></i>
<i class="fa fa-pinterest"></i>
<i class="fa fa-facebook"></i>
<i class="fa fa-linkedin"></i>
</div>
</div>
<!--== Social Icons End ==-->
</div>
</div>
</div>
<!--== Header Top End ==-->
<!--== Header Bottom Start ==-->
<div id="header-bottom">
<div class="container">
<div class="row">
<!--== Logo Start ==-->
<div class="col-lg-4">
<a href="index.php" class="logo">
<img src="assets/img/logo.png" alt="JSOFT">
</a>
</div>
<!--== Logo End ==-->
<!--== Main Menu Start ==-->
<?php include('includes/navigation/mainmenu.php') ?>
<!--== Main Menu End ==-->
</div>
</div>
</div>
<!--== Header Bottom End ==-->
</header>
<!--== Header Area End ==-->```
You echoed, before the redirect header, that's why you're getting that message. What you want to achieve looks like a client side job. Try replacing the php redirect with js.
Replace this:
header("Location: $redirect");
with this
echo "<script>window.location = '$redirect';</script>";

update database field based on what the user selects via dropdown

so on my site, the admin can assign a job to a user and that job will automatically have the status: waiting to be accepted. I have now created a drop down box on the job page so that the user can either decline or accept the job with that drop down box. however im unsure of the coding needed to update the database field based on what the user selects.
what im aiming for is: the user selects a status, then clicks the change button to update the status of the job.
current code:
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<title>Tyre Hire</title>
<link rel="stylesheet" type="text/css" href="css/mystyle.css"/>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<header>
<img class="img-responsive" src ="images/Logo.png" alt ="logo"/>
</header>
</div>
</div>
<?php
require_once ("config.inc.php");
try
{
$conn = new PDO(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD);
}
catch(PDOException $exception)
{
echo "Oh no, there was a problem" . $exception->getMessage();
}
if(!isset($_SESSION["username"]))
{
//user tried to access the page without logging in
header( "Location: add.php" );
}
$login = $_SESSION['user_ID'];
$query = "SELECT * FROM login WHERE user_ID = :user_ID";
$term = $conn->prepare($query);
$term->bindValue(':user_ID', $login);
$term->execute();
$login = $term->fetch(PDO::FETCH_OBJ);
$status = "Waiting to be accepted";
$status2 = "Accept Job";
$status3 = "Decline Job";
if(isset($_SESSION["username"]))
{
echo "Welcome, you are now logged in as <b>".$_SESSION['username']."</b> <img class='clientView' src='images/loginIcon.png' alt='client'>"; }
else {
echo "You are currently not logged in";
};
?>
<div class="row">
<div class="col-xs-12">
<br>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="main.php">Tyre Hire</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li> Home <span class="glyphicon glyphicon-print"></span></li>
<li> Search <span class="glyphicon glyphicon-print"></span></li>
<li><?php echo "<a href='all-jobs.php?user_id=" . $login->user_ID . "&occupation=". $login->occupation ."''> Current Jobs <span class='glyphicon glyphicon-print'></span></a>";?> </li>
<li> Register Interest <span class="glyphicon glyphicon-print"></span></li>
<li> Logout <span class="glyphicon glyphicon-print"></span></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="right">
<h2>Current Jobs</h2>
<p>One of the best sites to find the best qualified and skilled drivers in the UK.</p>
<form class="form-horizontal formApply" action="" method="POST">
<h2><u>Job Details </u></h2>
<div class="form-group">
<?php
echo "<h3>Job role :".$worker->jobTitle."</h3>";
echo "<ul>";
echo "<h3> About the role </h3>";
echo "<li><b>Company Name:</b>".$worker->company."</li>";
echo "<li><b>Job Description:</b>".$worker->jobDescription."</li>";
echo "<h3>shift pattern</h3>";
echo "<li><b>Start Time: </b>".$worker-> startTime."</li>";
echo "<li><b>End Time: </b>".$worker-> endTime."</li>";
echo "<br />";
echo "<li><b>start Date:</b>".$worker->startDate."</li>";
echo "<li><b>Job Expiry Date:</b>".$worker->expiry."</li>";
echo "<b><h4>Current Status: </b>".$worker->status."</h4>";
echo "</ul>";
?>
<div class="form-group">
<label class="control-label col-sm-2" for="search"> Status</label>
<div class="col-sm-3">
<select class="form-control" name="status" >
<option value="<? echo $status;?>"><?php echo $status;?></option>
<option value="<? echo $status2;?>"><?php echo $status2;?></option>
<option value="<? echo $status3;?>"><?php echo $status3;?></option>
</select>
<input type="hidden" value="<?php echo $status; ?>" name="status">
</div>
<div class="form-group">
<!--<label class="control-label col-sm-2" for="search"></label> -->
<div class="col-sm-3">
<!-- <select class="form-control" name="status" >
<option value="<? echo $job;?>"><?php echo $job;?></option>
</select> -->
<input type="hidden" value="<?php echo $job; ?>" name="job_id">
</div>
</div>
</div>
<input class = "buttonA" type="submit" name="Apply" value="Change status"/>
</div>
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div id="BottomArea">
<h4> Recruitment Agencies </h4>
<div class="row">
<div class="col-xs-3">
<img class="FxPic img-responsive" src="images/fxpic.png" alt="fx-logo">
</div>
<div class="col-xs-3">
<img class="driverhire img-responsive" src="images/driverhire.jpg" alt="driverhire">
</div>
<div class="col-xs-3">
<img class="logi img-responsive" src="images/logi.png" alt="logi-logo">
</div>
<div class="col-xs-3">
<img class="pebble img-responsive" src="images/noel.png" alt="pebbles">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<footer>
<img id="logoFoot" src ="images/Logo.png" alt ="logo"/>
<p><b>Address: 5A twickenham road, london SJ5 7AE</b></p>
</footer>
</div>
</div>
<script src=myjs.js></script>
</div>
</body>
</html>
attempted it after reading the update manual, no errors but upon clicking the button it gives a blank page.:
<?php
include ("db_fncs.php");
try{
$conn = new PDO(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD);
}
catch (PDOException $exception)
{
echo "Oh no, there was a problem" . $exception->getMessage();
}
$status = $_POST["status"];
$job = $_POST['job_id'];
print_r($_POST);
$query = "UPDATE jobs SET status = :status WHERE job_id = :job_id AND status = :status";
// Prepare statement
$stmt = $conn->prepare($query);
$stmt->bindValue(':status', $status);
$stmt->bindValue(':job_id', $job);
// execute the query
$stmt->execute();
//$stmt->commit();
echo $stmt->rowCount() . " records UPDATED successfully";
?>
The basic syntax for UPDATE is:
UPDATE <my_table>
SET <update_column_name> = <some_value>
WHERE <identifying_column_name> = <some_value>
In your case, it would probably look something like:
UPDATE jobs
SET status = <new_job_status>
WHERE userid = <user_id>
AND jobid = <job_id>

HTML table not populating from mysql db

I am trying to get a simple HTML table of the data in a table of my mysql database, but it is just showing up as a jumbled mess of the text and code in a paragraph on the page. Any ideas what may be causing this? Below is the code I am running:
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
Title
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="./swipe_da_code_files/bootstrap.min.css" rel="stylesheet">
<link rel="icon" type="image/ico" href="http://lndapp.wpi.edu/img/favicon.ico">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://url:Port">AdminPortal</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Create User</li>
<li>Database</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<h1>Database Landing Page</h1>
</div>
<?php
$server = mysql_connect("server", "user", "password");
$db = mysql_select_db("database", $server);
$query = mysql_query("SELECT * FROM Table");
?>
<table>
<tr>
<td>username</td>
<td>password</td>
<td>physicianID</td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[password]."</td>";
echo "<td>".$row[physicianID]."</td>";
echo "</tr>";
}
?>
</table>
</body></html>
I have tried a variety of different ways to display the data, this was the most simple form I could think of to just display a few rows in table form, however it is not running. I have double checked my connection details I entered, as well as the query for the database, and am certain that is not what's causing it.
Replace
echo "<td>".$row[username]."</td>";
echo "<td>".$row[password]."</td>";
echo "<td>".$row[physicianID]."</td>";
By
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['physicianID']."</td>";
You are missing quotes!
Please use mysqli or PDO since mysl method is now deprecated.
<?php
$server=mysqli_connect("server", "user", "password" , "my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($server, "SELECT * FROM Table")
?>
then
<?php
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['physicianID']."</td>";
echo "</tr>";
}
?>
Akintunde007 is correct in his/her answer that you are parsing a constant.

updating information Mysqli_php

i'm trying to make admin panel for simple website
now i'm trying to update the information
but i don't know why it don't update
on ControlPage
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("../guest.php");
require_once("../db.php");
global $tf_handle;
$gb = new guest();
$id = 0;
//get id from url
if(isset($_GET['id']))
{
$id = (int)$_GET['id'];
}
$message = $gb->getMessage($id);
if(isset($_POST['submit']))
{
$uid = $_POST['id'];
echo $uid;
$name = $_POST['name'];
echo $name;
$msg = $_POST['message'];
echo $msg;
$update = $gb->Update($id,$name,$msg);
if($update)
{
echo('updated');
}
else
{
echo("not updated");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Guestbook control panel</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="css/plugins/morris.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">GuestBook Admin</a>
</div>
<!-- Top Menu Items -->
<ul class="nav navbar-right top-nav">
<li class="dropdown">
<i class="fa fa-user"></i> Ambaleh <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<i class="fa fa-fw fa-gear"></i> Settings
</li>
<li class="divider"></li>
<li>
<i class="fa fa-fw fa-power-off"></i> Log Out
</li>
</ul>
</li>
</ul>
<!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active">
<i class="fa fa-fw fa-dashboard"></i> Dashboard
</li>
<li>
<i class="fa fa-fw fa-bar-chart-o"></i>Messages
</li>
<li>
<i class="fa fa-fw fa-bar-chart-o"></i>Logout
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Admin Panel
</h1>
<ol class="breadcrumb">
<li>
<i class="fa fa-dashboard"></i> Dashboard
</li>
<li class="active">
<i class="fa fa-table"></i> Messages
</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<h2>Update Messages</h2>
<form role="form" action="update.php" method = "post">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value = '<?php echo $message['name'];?>' class="form-control">
</div>
<div class="form-group">
<label>Message</label>
<textarea name="message" class="form-control" rows="3"><?php echo $message['message'];?></textarea>
</div>
<input type="hidden" name="id" value='<?php echo $id;?>' >
<input type="submit" name="submit" value="save!">
</form>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Morris Charts JavaScript -->
<script src="js/plugins/morris/raphael.min.js"></script>
<script src="js/plugins/morris/morris.min.js"></script>
<script src="js/plugins/morris/morris-data.js"></script>
</body>
</html>
i tried to echo the variables to check
but it already echo & takes the information from the POST request so the problem from another thing
update.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('db.php');
class guest
{
//get all the messages
public function getMessage($id)
{
global $tf_handle;
$querycheck = mysqli_query($tf_handle,"SELECT * FROM `messages` WHERE `id` = $id");
if($querycheck)
{
$message = mysqli_fetch_assoc($querycheck);
return $message;
}
else
{
die('Failed');
}
tinyf_db_close() ;
}
//".$name."
public function Update($id,$name,$message)
{
global $tf_handle;
$query = mysqli_query($tf_handle,"UPDATE `guest`.`messages` SET `name` = ".$name.", `message` = ".$message." WHERE `messages`.`id` = ".$id);
if($query)
{
echo "query Works";
return TRUE;
}
else
{
tinyf_db_close() ;
return False;
}
tinyf_db_close() ;
}
}
?>
i think the problem in this line
$query = mysqli_query($tf_handle,"UPDATE `guest`.`messages` SET `name` = ".$name.", `message` = ".$message." WHERE `messages`.`id` = ".$id);
There may well be other issues but this is the first I see.
text fields in any query need to be wrapped in quotes, single quotes is my preference, so this update wont compile.
$query = mysqli_query($tf_handle,
"UPDATE `guest`.`messages`
SET `name` = ".$name.", `message` = ".$message."
WHERE `messages`.`id` = ".$id);
You can also simplify the building of the query if you remember that double quoted string literals will expand variables automatically
SO Change to
$query = mysqli_query($tf_handle,
"UPDATE `guest`.`messages`
SET `name` = '$name', `message` = '$message' WHERE
`messages`.`id` = $id" );
Also after any mysql calls you should really do something with the actual error message rather than just return false.
if($query) {
echo "query Works";
return true;
} else {
$this->LastError = mysqli_error($tf_handle);
tinyf_db_close() ;
return false;
}
Then in the calling code do
if(isset($_POST['submit']))
{
$uid = $_POST['id']; <-- also change this to
$id = $_POST['id']; <-- this so it matches Update() params
echo $uid;
$name = $_POST['name'];
echo $name;
$msg = $_POST['message'];
echo $msg;
$update = $gb->Update($id,$name,$msg);
if($update) {
echo('updated');
} else {
echo $gb->LastError;
}

Categories