Jump to content

Display Content as Default from MySQLi


Recommended Posts

Hi folks,

I am in the middle of creating a CMS as a project. It's going pretty well so far but I am stuck and hoping to get some guidance.

When loading the main website, I want the contents from "Home" in the database to display unless a menu item is clicked.

Here is what I have so far:

<?php
include_once('includes/header.php');
require_once('admin/includes/config.php');
?>
    
    <div class="hero-wrap" style="background-image: url('images/uluru.jpg');" data-stellar-background-ratio="0.5">
      <div class="overlay"></div>
      <div class="container">
        <div class="row no-gutters slider-text justify-content-start align-items-center">
          <div class="col-lg-6 col-md-6 ftco-animate d-flex align-items-end">
          	<div class="text">
	            <h1 class="mb-4">Coaches For Hire <span>Book Now!</span></h1>
	            <p style="font-size: 18px;">The local Anangu, the Pitjantjatjara people, call the landmark Uluṟu (Pitjantjatjara [ʊlʊɻʊ]). This word is a proper noun, with no further particular meaning in the Pitjantjatjara dialect, although it is used as a local family name by the senior Traditional Owners of Uluru.</p>
	            <a href="https://www.youtube.com/watch?v=biuYA54nb7Y" class="icon-wrap popup-vimeo d-flex align-items-center mt-4">
	            	<div class="icon d-flex align-items-center justify-content-center">
	            		<span class="ion-ios-play"></span>
	            	</div>
	            	<div class="heading-title ml-5">
		            	<span>Learn more about Uluru</span>
	            	</div>
	            </a>
            </div>
          </div>
          <div class="col-lg-2 col"></div>
          <div class="col-lg-4 col-md-6 mt-0 mt-md-5 d-flex">
          	<form action="#" class="request-form ftco-animate">
          		<h2>Get A Quote</h2>
	    				<div id="searchBoxContainer" class="form-group">
	    					<label for="searchBox" class="label">Pick-Up Location</label>
	    					<input class="form-control" type="text" id="searchBox" placeholder="Start Typing..." />
	    				</div>
	    				<div id="searchBoxContainerAlt" class="form-group">
	    					<label for="searchBoxAlt" class="label">Drop-Off Location</label>
	    					<input type="text" class="form-control" id="searchBoxAlt" placeholder="Start Typing..." />
	    				</div>
	    				<div class="d-flex">
	    					<div class="form-group mr-2">
	                <label for="" class="label">Departure Date</label>
	                <input type="text" class="form-control" id="book_pick_date" placeholder="Date">
	              </div>
	              <div class="form-group ml-2">
	                <label for="" class="label">Return Date</label>
	                <input type="text" class="form-control" id="book_off_date" placeholder="Date">
	              </div>
              </div>
              <div class="d-flex">
              <div class="form-group mr-2">
                <label for="" class="label">Pick-Up Time</label>
                <input type="text" class="form-control" id="time_pick" placeholder="Time">
              </div>
              <div class="form-group ml-2">
                  <label for"" class="label">Passenger Numbers</label>
                  <input type="number" class="form-control" placeholder="Amount" />
              </div>
              </div>
	            <div class="form-group">
	              <input type="submit" value="Request Quote" class="btn btn-primary py-3 px-4">
	            </div>
	    			</form>
          </div>
        </div>
      </div>
    </div>
<script type="text/javascript" src="https://www.bing.com/api/maps/mapcontrol?key=AqIY0ivSCCdBIe3-EKGuox9cwBFw2wWRWIErZi1iy57EfD67PoiSra9wl_wu48de&callback=bingMapsReady" async defer></script>
<?php
$id = $_GET['id'];

$sql = "SELECT * FROM pages WHERE id = $id";
$result = $con->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
?>
    <!-- HOW IT WORKS -->
		<section class="ftco-section ftco-no-pt ftco-no-pb">
			<div class="container">
				<div class="row no-gutters">

					<div class="col-md-12 wrap-about py-md-5 ftco-animate">
	          <div class="heading-section mb-5 pl-md-5">
	          	<span class="subheading"><?php echo $row['description']; ?>
	          	</span>
	            <h2 class="heading"><?php echo $row['name']; ?></h2>

	            <?php echo $row['body']; ?>
	          </div>
					</div>
				</div>
			</div>
		</section>
<?php
}
}
?>

<!-- FOOTER -->
<?php
include_once('includes/footer.php');
?>

I hope you can help and that I am making sense.

Cheers,

Dan

Edited by DanEthical
Link to post
Share on other sites
1 minute ago, benanamen said:

Your code is vulnerable to an SQL Injection Attack. You need to use Prepared Statements.

NEVER EVER PUT VARIABLES IN YOUR QUERY

Thanks for pointing that out. Yes, I agree. It is only a project that will be fixed up as time goes on. For now I just want to get everything working and then I can modify MySQL code where required.

Link to post
Share on other sites

Got it to work by using an If statement as such:

if(mysqli_real_escape_string($con, $_GET['id']=="")){
    $sql = "SELECT * FROM pages WHERE name = 'Home'";
$result = $con->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {

Most likely not the most correct way to do it but it works.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.