John Davidson

mysql - Issues with PHP Slim or API Generation

0 comments
Message:


i am new to using API's. i am using the Slim framework for PHP.
i have a data base called devdev and a table called product in mySQL.
I created a webpage, connected the database using normal PHP. and it displays my Products.


then i installed slim into my project and a few strange things happened.


the slim1.php file when opened in my browser displays exactly whats in my index.php. I am not sure why this is happening. the links to my login.php and register.php does not work either. when i click on the the URL changes to http:.../PHP/login.php but it still displays my index.php.


when i ran my slim1.php in thunder client it failed but when i ran index.php in thunder client it returned my index.php code. i am not sure why this is happening. i was expecting a JSON data format of my product list from my database. What is causing this to happen or what am i not getting/missing?



slim1.php



   <?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require './vendor/autoload.php';

$app = new \Slim\App;

// Running the Slim framework, Get Method
$app->get('/product', function (Request $request, Response $response, array $args) {
require_once 'db.php';// Calling the database connection file

$query = "select * from devdev";// SQL query
$result = $conn->query($query);

while ($row = $result->fetch_assoc()){
$data[] = $row;// Store each field in an array
}

return json_encode($data);// Translate this array into JSON
});

$app->run(); //this ensures that the code runs in Slim


index.php



 <?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
include 'database.php';
include 'cartProcess.php';
include 'registerProcess.php';
include 'loginProcess.php';

// var_dump($_SESSION);


?>
<!DOCTYPE html>
<html>
<head>
<meta viewport="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<title>Home Page</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous">
<link href="../../FullStackOnlineStoreFinalCodespaceAcademy/CSS/styleHomePage.css" rel="stylesheet" type="text/css">
<link href="../../FullStackOnlineStoreFinalCodespaceAcademy/CSS/styleCart.css" rel="stylesheet" type="text/css">
</head>


index.php "body"



<section>
<div class="row">
<div class="col-md-12">
<div class="row">
<?php
$query = 'SELECT * FROM product ORDER BY id ASC';
$result = mysqli_query($conn,$query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo '
<div class="col-md-3">
<div class="card">
<div class="card-body">
<form method="post" action="index.php?action=add&id='.$row['id'].'">
<div class="product">
<img src="'.$row['productImage'].'" alt="">
<h4 class="text-info">'.$row['productName'].'</h4>
<h4 class="text-muted">'.$row['price'].' Coins</h4>
<p class="text-muted">'.$row['product description'].'</p>
<input type="number" name="quantity" class="form-control" value="1">
<input type="hidden" name="productName" value="'.$row['id'].'">
<input type="hidden" name="price" value="'.$row['price'].'">
<input type="submit" name="add_to_cart" class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3" value="Add to Cart">
</div>
</form>
</div>
</div>
</div>
';
}
}
?>
</div>
</div>
</div>
</section>


My Answer:
It sounds like there may be an issue with your Slim configuration or routing. Make sure that you have set up your Slim routes correctly to handle requests for your login.php and register.php files.

Additionally, make sure that you are returning the data from your database in the correct format in your Slim routes. You should be returning JSON data in response to API requests, not HTML like you would for a regular webpage.

Check your Slim configuration and routes to ensure that they are set up correctly to handle API requests and return the data in the correct format. If you continue to have issues, you may want to consult the Slim documentation or seek help from the Slim community for further assistance.

Rate this post

3 of 5 based on 4415 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us