0

I'm trying to insert data from a php page into my mySQL databse. when I refresh the page parts of my php code is being displayed on the page rather than being processed below is my code(sensitive info blocked):

<?php
  $servername = "localhost";
  $username = "*******";
  $password = "*******";
  $dbname = "accounts";

  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }

  $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  VALUES ('John', 'Doe', '[email protected]')";

  if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
  } else {
     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  }

  mysqli_close($conn);
?>

and this is a screenshot of the "view source", as you can see it sort of stops doing it's PHP and displaying into my page. thoughts?

view source

4
  • I don’t see there’s any issue with your code. You sure your server is on and the php lib is installed? Commented Sep 14, 2017 at 1:30
  • may be bad configuration in your vhosts, htaccess, bad rights file/folders, or you apache stop for some reason ... did you got some errors in your logs? Commented Sep 14, 2017 at 1:31
  • 2
    File is .php? Accessing with http://? Commented Sep 14, 2017 at 1:40
  • 1
    Which web server are you using? Does it support PHP at all ? Commented Sep 14, 2017 at 1:44

2 Answers 2

1

Hello bro your code is alright i test it on my local server. i think you are not saving file with correct extension. or your mysql table fields are incorrect if data is not inserting. Correct extension for php file name.php

Sign up to request clarification or add additional context in comments.

Comments

1

I think there is issue with either your web server or your file extension, Make sure your web server is active and file extension is .php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.