So am doing my project, and i am literally stuck at this part where i want to display values from a database which are foreign keys, and adding them via selection from a drop down menu. the values are to be added to the new table as selections of the values of the other (that sounds a bit mixed).So i managed to get the values displayed in a drop down menu on my form (the foreign contraints) and i can select them, but once i submit the form i get an error.
Book Insertion Query Failed.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''category', $'bookclub', '2015-04-19 10:31:09')' at line 1
i use both php and html in one page, makes it easier for me i guess, so here is my code:
code for fetching and selecting the foreign constraints:
<div class='container'>
<label for='</select>' >Book Club: </label><br/>
<?php
include('include/databaseconnection.php');
$sql = "SELECT name FROM bookclubs";
$result = mysql_query($sql);
echo "<select name='bookclub'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";
?>
</div>
code for value insertion to the database:
<?php
//Php for record inserted text
//db connection
include('include/databaseconnection.php');
// Initializing variables
$title = '';
$author = '';
$category = '';
$bookclub = '';
$submit = 'submit';
if(isset($_POST['submit'])){
// if submit button is pressed
$title = $_POST['title'];
$author =$_POST ['author'];
$category =$_POST ['category'];
$bookclub = $_POST ['bookclub'];
$date = date('Y-m-d H:i:s');
if ($db_con == TRUE) {
$insertValue = "INSERT INTO books VALUES ('$title','$author', $'category', $'bookclub', '$date')";
$queryInsert = mysql_query($insertValue) or die ('Book Insertion Query Failed.'.mysql_error());
if (mysql_affected_rows() == 1) { echo "Record was successfully inserted";}}
else { exit;} }
?>
I believe the error is with the selection and input of the foreign keys to the new table. Currently using xammp and database is mysql server. P.s am a beginner at php, know how to play with codes but cant write them off head
Aucun commentaire:
Enregistrer un commentaire