-1

PHP trying to connect to MySQL database! but the code doesn't work.

Here is the code, when I put the code in a PHP file, the display shows actually the code I wrote.

I am stuck, please help!

<?php // sqltest.php
  require_once '../../htdocs/login.php';
  $conn = new mysqli($hn, $un, $pw, $db);
  if ($conn->connect_error) die($conn->connect_error);

  if (isset($_POST['delete']) && isset($_POST['isbn']))
  {
    $isbn   = get_post($conn, 'isbn');
    $query  = "DELETE FROM classics WHERE isbn='$isbn'";
    $result = $conn->query($query);
    if (!$result) echo "DELETE failed: $query<br>" .
      $conn->error . "<br><br>";
  }

  if (isset($_POST['author'])   &&
      isset($_POST['title'])    &&
      isset($_POST['category']) &&
      isset($_POST['year'])     &&
      isset($_POST['isbn']))
  {
    $author   = get_post($conn, 'author');
    $title    = get_post($conn, 'title');
    $category = get_post($conn, 'category');
    $year     = get_post($conn, 'year');
    $isbn     = get_post($conn, 'isbn');
    $query    = "INSERT INTO classics VALUES" .
      "('$author', '$title', '$category', '$year', '$isbn')";
    $result   = $conn->query($query);

    if (!$result) echo "INSERT failed: $query<br>" .
      $conn->error . "<br><br>";
  }

  echo <<<_END
  <form action="sqltest.php" method="post"><pre>
    Author <input type="text" name="author">
     Title <input type="text" name="title">
  Category <input type="text" name="category">
      Year <input type="text" name="year">
      ISBN <input type="text" name="isbn">
           <input type="submit" value="ADD RECORD">
  </pre></form>
_END;

  $query  = "SELECT * FROM classics";
  $result = $conn->query($query);
  if (!$result) die ("Database access failed: " . $conn->error);

  $rows = $result->num_rows;

  for ($j = 0 ; $j < $rows ; ++$j)
  {
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);

    echo <<<_END
  <pre>
    Author $row[0]
     Title $row[1]
  Category $row[2]
      Year $row[3]
      ISBN $row[4]
  </pre>
  <form action="sqltest.php" method="post">
  <input type="hidden" name="delete" value="yes">
  <input type="hidden" name="isbn" value="$row[4]">
  <input type="submit" value="DELETE RECORD"></form>
_END;
  }

  $result->close();
  $conn->close();

  function get_post($conn, $var)
  {
    return $conn->real_escape_string($_POST[$var]);
  }
?>

enter image description here I am reading this book called Learning PHP, MySQL, & JavaScript 4th Edition By Robin Nixon, but when I write the exact code, it doesn't show like in the book but I get that like in the photo. I am trying to connect MySQL with PHP, using xampp. I created also the php.login file. I wrote my username and password, and saved it in the same directory with this code! please help

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    Do you have PHP installed on your machine/server? Btw, that's a `.html` file you're viewing, might want to rename it to `.php` – brombeer Mar 14 '18 at 10:00
  • 7
    After looking to your picture I saw that you saved your file with `index.html` . change it to `index.php` and it will start working – Alive to die - Anant Mar 14 '18 at 10:01
  • [This answer](https://stackoverflow.com/a/11312349/3441504) to another question will explain your problem. As others before me mentioned here: it's because your file type `.html` – kscherrer Mar 14 '18 at 10:05
  • i saved it as .php, but when i run with dreamweaver in chrome live preview, i get that, i want to see this code working! – ardi hajrizaj Mar 14 '18 at 10:26
  • If above solution doesnot help then there may be something wrong with your configuration, check [this answer](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) to another question similar to your question. – Sushank Pokharel Mar 14 '18 at 10:40
  • You need to use the `.php` extension (not `.html`) for the file name and make sure your webserver knows how to handle it. – axiac Mar 14 '18 at 11:40
  • you need to run it via Apache server with PHP installed so that the PHP engine can execute the code, Dreamweaver cannot do that. In fact I'd have thought Dreamweaver was generally not the best IDE to use for actual programming rather than web design. Visual Studio Code or a dedicated PHP IDE would be much more useful. – ADyson Mar 14 '18 at 11:42
  • You can check the hostname,username, password and database coming or not. – itsme Mar 14 '18 at 11:45
  • @itsme look at the screenshot, we're stuck a long way before that. The code is not even executing. – ADyson Mar 14 '18 at 12:14
  • Please be warned that your queries are widely open for SQL injection. Have a look at prepared statements to avoid getting hacked – Nico Haase May 24 '23 at 14:21

1 Answers1

-2

Perhaps try it with less code and more emphasis on establishing a connection to the database as shown below `

    $servername = "localhost";
$username = "username";
$password = "password";

try {
  $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}

after that you can start inserting data into the database, getting the last id and many more

Helper123
  • 15
  • 6
  • The issue was the OP was connecting PHP code using an HTML page – Martin May 24 '23 at 14:25
  • Please, don't answer a question with "perhaps", "may be" or "guess". Only provide an answer if you clearly understood the problem and has a concrete solution related to the question. – Your Common Sense May 24 '23 at 14:36