0

I have been trying a few exercises for revision and I am having issues with a piece of PHP code - a small form / submit into a database - when I submit, the execution is not working as intended (it should state "Success!" or atleast "Error":

    <?php  
    $servername = "localhost";  
    $username = "username";  
    $password = "password";  
    $dbname ="whatdo"; 
    
    $conn = new mysqli($servername, $username, $password, $dbname); 
     
    if ($conn->connect_error) {
        die("Connection failed: " .$conn->connect_error);
    }
    
    $disc = $_POST["disc"];
    
    $sql = "INSERT INTO whatdo (disc) VALUES ('$disc')";
    
    if($conn->query($sql) === TRUE) {
        echo "Success!";
    } 
    else {
      echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    $conn->close(); 
    
    ?>

When submitted, the page displays the following:

    connect_error) { die("Connection failed: " .$conn->connect_error); } $disc = $_POST["disc"]; $sql = "INSERT INTO whatdo (disc) VALUES ('$disc')"; if($conn->query($sql) === TRUE) { echo "Success!"; } else { echo "Error: " . $sql . "
    " . $conn->error; } $conn->close(); ?>

I thought I had all my bases covered but I have obviously missed something obvious.

sonique
  • 4,539
  • 2
  • 30
  • 39
  • Do you have PHP interpreter installed and enabled in your web server? – juzraai Jun 26 '20 at 22:30
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Jun 26 '20 at 22:42
  • Hi - I am only manually combing this because it is a small section of code, up till now I have been able to execute basic strings (emphasising that I am a newb) - I have rewritten this multiple times and returning the same result. – Dan Buck Jun 26 '20 at 23:00
  • Also to clarify - the files are local to my comp and the database is via XAMPP, I am only completing various tasks as a means to revise :) – Dan Buck Jun 26 '20 at 23:01
  • Is php installed on your server ? use this simple code to check : – detzu Jun 27 '20 at 02:22
  • Try this comments out -> The $disc = $_POST["disc"]; should be given before establishing the connection -> The $disc = $_POST["disc"]; must be coming from the php function Try this code out [![enter image description here](https://i.stack.imgur.com/9dCmh.png)](https://i.stack.imgur.com/9dCmh.png) – Maria Israel Sathyan Jun 27 '20 at 06:56
  • @detzu I ran the check and it returned an affirmative reply. But the issue persists, I went backwards with the level of detail on the code and just tried: and all I get is a blank page. If I add a little HTML into that "

    Howdy

    "; a part og the sring is visible "Howdy;>?
    – Dan Buck Jun 28 '20 at 15:42

0 Answers0