0

I'm creating a bookstore database using phpMyAdmin/MySQL, and HTML. I have my PHP created along with the HTML form to input the data online, however I keep getting an "error querying database." error once I hit submit on my form. No detail to what the problem is. I'm a PHP newbie so I could've made a code mistake but I can't find anything obvious.

<title>Bookstore Database</title>
</head>
<body>
<h2>Below Is Your Confirmation Message </h2>

<?php
  $title = $_POST['title'];
  $author_last = $_POST['author_last'];
  $author_first= $_POST['author_first'];
  $format = $_POST['format'];
  $price= $_POST['price'];
  $isbncode= $_POST['isbncode'];

   $dbc = mysqli_connect('localhost', 'root', '','bookstore_jason')
   or die('Error connecting to MySQL server.');

  $query = "INSERT INTO books (title, author_last, author_first, format,    price, isbncode)" .
"VALUES ('$title', '$author_last', '$author_first', '$format', '$price', '$isbncode')";

echo $query;
echo '<br/>'; 

 $result = mysqli_query($dbc, $query)
  or die('Error querying database.');

  mysqli_close($dbc);

 echo 'The Book Title Is: ' .$title. '<br>';
 echo 'The Author Last Name Is: ' .$author_last. '<br>';
 echo 'The Author First Name Is Is: ' .$author_first. '<br>';
 echo 'The Book Format Is: ' .$format. '<br>';
 echo 'The Book Price Is: ' .$price. '<br>';
 echo 'The Book ISBN-10 Code Is: ' .$isbncode. '<br>';
  ?>

</body>
</html>
Francisco
  • 10,918
  • 6
  • 34
  • 45
Jason Quinn
  • 25
  • 1
  • 6
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – Jay Blanchard Feb 09 '16 at 21:45
  • Instead of `die('Error connecting to MySQL server.')` output the error. http://php.net/manual/en/mysqli.connect-error.php – chris85 Feb 09 '16 at 21:47
  • 3
    Get the *actual* error: add `or die(mysqli_error())` to your queries. Or you can find the issues in your current error logs. – Jay Blanchard Feb 09 '16 at 21:47

1 Answers1

1

Rather than just putting out a woops message output something useful, then likely as not you can solve your own issue

$result = mysqli_query($dbc, $query)
      or die('Error querying database. ' .  mysqli_error($dbc);

#TeachAManToFish

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149