-1

I have been experimenting with this code:

    <html>
<head>
    <title>Test</title>
    <h3>Test</h3>
</head>
<body>
    <?php
        $name = $_POST["name"];
        $age = $_POST["age"];
        $con = new mysqli("localhost","root","","server");

        if ($con->connect_error) {
            die("Connection failed: " . $con->connect_error);
        }



        $sql = "SELECT * FROM `joinedplayers`";
        $result = $con->query($sql);

        if ($result->num_rows > 0) {
        // output data of each row
            while($row = $result->fetch_assoc()) {
                echo $row["num"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. " <button type = 'button'> Get Info </button>  <br><br>";
            }
        } else {
            echo "0 results";
        }

        $con->close();
    ?>
</body>
</html>

Btw: I'm using XAMPP and phpMyAdmin

For some reason, I can't send requests to the script. I have a table that holds 3 values: the current number (first-person = 1, second-person = 2 and so on...) the name (varchar) and an age value which holds an int. If I run this, without the two lines with $_POST, it works fine and lists all the objects in the table as I want it too. Also, the MySQLI is connecting to the localhost just fine. Is there any way to get the values sent as a post and add them to the database?

Also, here is the code I used inside of Fiddler 4: name=test&age=5

yivi
  • 42,438
  • 18
  • 116
  • 138
sen_trix
  • 19
  • 2
  • Since you state that you started learning PHP yesterday, welcome aboard. Things you should be aware of, in case you don't know, is learning how to debug. This can be found on the web and may very well lead you (back) to either PHP.net or here on Stack Overflow or other well reputed sites. Error reporting is a good start https://www.php.net/manual/en/function.error-reporting.php – Funk Forty Niner Dec 02 '19 at 00:56

1 Answers1

0

I'm guessing your values aren't coming through from your HTML.

You need to specifically tell the browser your form, is submitting post data. Please have a read of PHP forms for some good reading: https://www.w3schools.com/php/php_forms.asp

otherwise: it looks like your missing this part:

<form action="welcome.php" method="post">

</form>

You are not actually submitting data to show up in $_POST. Once you add a button and submit the form then in your PHP the values will come through

Sweet Chilly Philly
  • 3,014
  • 2
  • 27
  • 37
  • What if this is coming from an external website? So for example let's say example.com sends a post request to this server (assuming that it can access it), will it be able to do so? – sen_trix Dec 02 '19 at 00:49
  • From him saying hes a beginner, I safely assume its not, and its just a miss understanding of how PHP fundamentally works. – Sweet Chilly Philly Dec 02 '19 at 01:21