-1

After using a form and submit button to take user input, I want my page to print out the values from a database, however, after hitting submit the new page has nothing at all. even the h is not printed.

<html>
<body>

<?php
echo "h";
$server = "localhost";
$username = "root";
$pass = "fghjkl";
$db = "world";
$conn = new mysqli($server, $username, $pass,$db) or die("Connect failed: %s\n". $conn -> error);
$select = "select Name,Continent,Region from country where name like 'a%'";

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$country =$_POST["country"]; 
$continent = $_POST["continent"];
$region =$_POST["region"]; 
$add = "insert into Country(name,continent,region) values (". $country.",".$continent.","$region.")"; 
$conn->query($add);
$result = $conn->query($select);

if ($result->num_rows > 0) {
echo "l";
    // output data of each row
    while($row = $result->fetch_assoc()) {
      echo "name: " . $row["Name"]. " - continent: " . $row["Continent"]. " - in - " . $row["Region"]. "<br>";
    }
  } else {
    echo "0 results";
  }
$conn->close();
?>
</body>
</html>

The form is :

<html>
<body>    
<form name = "test" action="hlo.php" method="POST">
country: <input type="text" name="country"><br>
continent: <input type="text" name="continent"><br>
region: <input type="text" name = "region"><br>
<input type="submit" name = "submit" value="submit">
</form>
</body>
</html>
  • Could you please also share the code of the form? – MF714 Jun 03 '22 at 09:13
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jun 03 '22 at 09:13
  • There should be an error in your error log. Did you check? – Dharman Jun 03 '22 at 09:14

1 Answers1

-2

I think the problem is right here:

$add = "insert into Country(name,continent,region) values (". $country.",".$continent.","$region.")"; 

You are missing one . before $region.

I think this is going to fix it:

$add = "insert into Country(name,continent,region) values (". $country.",".$continent.",".$region.")"; 
Thanos
  • 358
  • 1
  • 3
  • 6