0

Im trying to get my database for my local host up and running using MYSQLI and i get the message "Failed to connect: A connection could not be established because the destination computer actively refused it"

My code is the following

the connect page called connect.php

<?php
require_once "user_db.php";

$mysqli = mysqli_connect($servername,$username,$password,$database);

if (mysqli_connect_errno()) {
  echo "Failed to connect:",mysqli_connect_error();
}

function performQuery($sql) {
  global $mysqli;
  $result = mysqli_query($mysqli,$sql);
  if ($result) {
    return $result;
  } else {
    echo "Something went wrong";
  }
}
 ?>

and in adition to that i have the user_db.php page with my database information

   <?php
$servername = "localhost";
$username = "root";
$password = "12345";
$database = "recovery"
 ?>

and then the page where i try to add stuff to my database

    <?php
require_once "DB_handling/connect.php";

if(isset($_POST["submit"])) {
  $duration = $_POST['TD'];
  $intensity  = $_POST['TI'];
  $RPE = $TI*$TD;


$New = performQuery("INSERT INTO rpe(duration, intensity, rpe) VALUES ('$duration', '$intensity', '$RPE')");
}
 ?>

with a simple form field to get the values

    <form class="" action="insert.php" method="post">
Training Intensity
<input type="number" placeholder="1-10" name="TI">
<br>
Training Duration
<input type="number" placeholder="in minutes" name="TD">
<br>
<input type="submit" name="submit" value="Submit">
</form>

Im using MAMP as my localhost server

Does know how to fix the problem. Thanks in advance

Ko1ind
  • 39
  • 8
  • Make sure your MySQL server is up and running and isn't configured to use any other port than the default 3306. – M. Eriksson Jul 30 '22 at 15:04
  • 1
    **Warning!** You're open to [SQL injection attacks](https://owasp.org/www-community/attacks/SQL_Injection)! Read [how to prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) by using prepared statements with bound parameters instead of injecting variables directly into your queries. It's not just about security. If your data contains, for example, a single quote `'`, your query will break. – M. Eriksson Jul 30 '22 at 15:04
  • _Side note:_ Here: `$RPE = $TI*$TD;`, where does `$TI` and `$TD` come from? – M. Eriksson Jul 30 '22 at 15:05
  • thanks i changed the name of those and waset aware of the $TI and $TD – Ko1ind Jul 30 '22 at 15:06
  • _Side note 2:_ Your `performQuery()`-function is completely pointless. It actually makes it worse since it returns a string on failure. – M. Eriksson Jul 30 '22 at 15:07
  • and mySQL is set to the default for mamp, but its at the 8889 – Ko1ind Jul 30 '22 at 15:07
  • 1
    The default MySQL port is 3306. If your MySQL instance is set to 8889, then you need to define that when you connect. You can change to `$servername = "localhost:8889"` and try (I don't use MAMP so I don't know how it's usually configured) – M. Eriksson Jul 30 '22 at 15:09

0 Answers0