Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
PHP Error: Error Querying Database? How do I fix it?
My full PHP document is below. I keep geting an "error querying database error when I attempt to submit my for online to MyPHPdmin, for an assignment. My Xampp is running fine.
<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>
5 Answers
- ChrisLv 75 years ago
Both Ben and JR are wrong regarding the query composition, and JR is also wrong when he claims that "title" is a keyword. It's not.
How you are creating the query is completely fine, and you ARE in fact enclosing the String in double quotes, which means you CAN insert variable names directly. The fact that your String also contains single quotes does not matter one bit.
The problem lies elsewhere, but like Jeff says, we need to see the actual error message.
- Jeff PLv 75 years ago
There is something wrong with your query. If you were to put this right before mysqli_close():
echo mysqli_error($dbc);
It will echo out the exact error message from MySQL.
But, back to the point, there is something wrong with your query. What I would do is echo $query and see what it looks like. You may be missing a value or something. But, mysqli_error() will give you all the information you need to know about your error.
- just "JR"Lv 75 years ago
For the query:
I agree with Ben, but will add:
- Remember that Php does NOT parse variables in single quotes! => '$name' will pass $name, and NOT the VALUE of $name! => ' " . $name . " ' passes "myname"...
- "title" is also, I believe, like "user", "date", "time" etc, a keyword in mysqli, and needs to be enclosed in reverse single quotes.
Take the habit of enclosing table and field names with single reverse quotes: `tablename`, `fieldname`.
- abc_to_xyzLv 65 years ago
aren't you supposed to enter this line
"INSERT INTO books (title, author_last, author_first, format, price, isbncode)" .
"VALUES ('$title', '$author_last', '$author_first', '$format', '$price', '$isbncode')";
like this?
"INSERT INTO books (title, author_last, author_first, format, price, isbncode)" .
"VALUES (".$title.", ".$author_last.", ".$author_first.", ".$format.", ".$price.", ".$isbncode.")";
- How do you think about the answers? You can sign in to vote the answer.
- BenLv 45 years ago
I don't think you're concatenating the variables into the query correctly. Try the following:
$query = "INSERT INTO books (title, author_last, author_first, format, price, isbncode)" .
"VALUES ('" . $title . '", '" . $author_last . "', '" . $author_first . "', '" . $format . "', '" . $price . "', '" . $isbncode . "')";