0

html form not inputting text inside mysqldatabase.

        <div id="form-div">
            <form method="post" id="form-space" action="manage_content.php">
                <h6 id="close-text">close</h6>
                <input type="text" name="webname" id="webname" placeholder="Title"
                required="required"/>
                <input type="text" name="url" id="addurl" placeholder="url" required="required"/>
                <input type="submit" name="submit" value="Add Link" id="addlink"/>
            </form>         
        </div>

manage_content.php

<?php
$serverName = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "registration";
$conn = mysqli_connect($serverName, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
    die("Connection fails: " . mysqli_connect_error());
}
if(isset($_POST["submit"])){
    $webname = $_POST["webname"];
    $url = $_POST["url"];
    $sql = "INSERT INTO post (webname, url) VALUES (?, ?)";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        die(mysqli_error($conn));
    }
    mysqli_stmt_bind_param($stmt, "ss", $webname, $url);
    mysqli_stmt_execute($stmt);
}
?>

both the html file and the php is in the same directory but, it is not working. Can't figure out the problem.

  • What error do you get? – MarcM May 04 '22 at 11:30
  • [What do you mean "It doesn't work"?](https://meta.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work). We don't know what "not working" actually means in reality. What kind of error or unexpected behaviour do you get, beyond it not inserting into the database? Do some [debugging](https://www.atatus.com/blog/debugging-in-php/) - trace the code line by line to find the point where it deviates from the behaviour you expected. At the moment you're only reporting the symptom at the end of the process, which suggests you haven't investigated anything else. – ADyson May 04 '22 at 11:35
  • And improve your error handling - you're only checking for failures on some of the mysqli operations, not all of them, so you could be missing something. Better to just have univeral error reporting rather than having repetitive checking code after every single statement. See [mysqli or die, does it have to die?](https://stackoverflow.com/questions/15318368/mysqli-or-die-does-it-have-to-die) or [How to get the actual mysqli error](https://stackoverflow.com/a/22662582/5947043) for details. – ADyson May 04 '22 at 11:37
  • Code is fine... – Ahmet Feyzi May 04 '22 at 12:19
  • @AhmetFeyzi Well we don't really know that, do we? Because the OP hasn't provided enough information. It could be a mistake in this code, or it could be elsewhere (e.g. a problem in the database setup). Better not to make any assumptions... – ADyson May 04 '22 at 12:26

0 Answers0