J
J
Jimi Hendrix2016-02-21 20:14:41
PHP
Jimi Hendrix, 2016-02-21 20:14:41

Database access failed (Table does not exist)?

Faced the problem of working with databases using php. Everything seems to be correct in the code. Checked 5 times.
Calling the page with the form gives an error that it cannot find the table, although it exists. If you use other databases (not test), it gives an error that it cannot connect to the database.
9be80ad5c5754a8884d183a6f6ab18b3.PNGc9b0c10c6f264bbfac9af235015898ce.PNG
login.php file

<?php
  $db_hostname = 'localhost';
  $db_database = 'test';
  $db_username = 'root';
  $db_password = '';
?>

sqltest.php file
<?php
  require_once 'login.php';
  $db_server = mysql_connect($db_hostname, $db_username, $db_password);

  if (!$db_server) die ("Невозможно подключиться к MYSQL: " . mysql_error());

  mysql_select_db ($db_database, $db_server)
    or die("Невозможно выбрать БД: " . mysql_error());

  if (isset ($_POST['delete']) && isset($_POST['isbn']))
  {
    $isbn = get_post('isbn');
    $query = "DELETE FROM classics WHERE isbn = '$isbn'";

    if (!mysql_query($query, $db_server))
      echo "Сбой при удалении данных: $query<br>" . 
      mysql_error() . "<br><br>";
  }

  if (isset ($_POST['author']) &&
      isset ($_POST['title']) &&
      isset ($_POST['category']) &&
      isset ($_POST['year']) &&
      isset ($_POST['isbn']))
  {
    $author = get_post('author');
    $title = get_post('title');
    $category = get_post('category');
    $year = get_post('year');
    $isbn = get_post('isbn');

    $query = "INSERT INTO classics VALUES" .
    "('$author', '$title', '$category', '$year', '$isbn')";

    if (!mysql_query($query, $db_server))
      echo "Сбой при вставке данных: $query<br>" .
      mysql_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 = mysql_query($query);

  if(!$result) die ("Сбой при доступе к БД: " . mysql_error());
  $rows = mysql_num_rows($result);

  for($j = 0; $j < $rows; ++$j)
  {
    $row = mysql_fetch_row($result);
    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;
  }

  mysql_close($db_server);

  function get_post($var)
  {
    return mysql_real_escape_string($_POST[$var]);
  }
?>

Please help me understand

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Igor, 2016-02-21
@LuckydutchRM

Are you sure you have root without a password? And do you have all the necessary rights?

J
Jimi Hendrix, 2016-02-21
@LuckydutchRM

Users
d67b820f72b148e2b47828722b88c523.PNG

A
AntonMZ, 2016-02-21
@AntonMZ

Hah! Your user does not have rights to work with this database!!!

A
Alexander, 2014-11-11
@kostya__wolf

bbf = abf[1]['attachments']
You have a list in abf. Which element with index 1 is a dictionary with the necessary data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question