Jump to content

johnnys

Members
  • Posts

    42
  • Joined

  • Last visited

johnnys's Achievements

Member

Member (2/5)

0

Reputation

  1. Got it working. I have to rename my model file from adminarea_model.php to simply admin_model.php Not sure why this made a difference though but it's worked
  2. I am quite new to php and the mvc setup, I am developing a library app however starting at the very basics so as not to become overwhelmed! I am trying to do a basic insert to my book table, this is the code I have so far alsong with the error I am presented with. Model (models > adminarea_model.php) adminarea_model.php public function create($title_text) { $title_text = strip_tags($title_text); $sql = "INSERT INTO book (title) VALUES (:title)"; $query = $this->db->prepare($sql); $query->execute(array(':title' => $title_text)); $count = $query->rowCount(); if ($count == 1) { return true; } else { $_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED; } return false; } View (views > admin > addBook.php) addBook.php <form method="post" action="<?php echo URL;?>admin/create"> <label>Text of new note: </label><input type="text" name="title" /> <input type="submit" value='Create this note' autocomplete="off" /> </form> Controller (controllers > admin.php) admin.php public function create() { if (isset($_POST['title']) AND !empty($_POST['title'])) { $book_model = $this->loadModel('Admin'); $book_model->create($_POST['title']); } header('location: ' . URL . 'admin/addBook'); } When I am on admin/addBook and I try to submit the form I receive the following error; Fatal error: Call to a member function create() on a non-object in C:\xampp\htdocs\logintest\application\controllers\admin.php on line 43 Any ideas where I am going wrong? Line 43 contains the following $book_model->create($_POST['title']); Thanks J
  3. Good advice @Barand I was hoping for an easier solution however your suggestion makes sense for future amendments. Thanks
  4. I have two tables book and category, book id title author category isbn 1 fun times Joe 1 16161514 (new record) 2 fishing trip Jim Juvenile Science 88771615 (old record) 3 beach hunt Sam 3 81009991 (new record) 4 day out John 3 81009991 (new record) 5 farm fun Jim Classic Kids 88771615 (old record) category id cat_name 1 horror 2 science 3 kids I use the join query below to display a list of all book info, works fine. SELECT b.id, b.title, b.category, b.author, b.isbn, b.publicationYear c.cat_name FROM book AS b INNER JOIN category AS c ON b.category = c.id The problem I am facing is that all some of the books (1000's actually) in the db have been imported from a previous db, and instead of containing an integer for the category they contain a string such as 'General Fiction' or 'Popular Classics' etc. Must I import all of these categories into the 'category' table and assign an integer to them, or is there a way of simply displaying the string that is contained within the 'book' table as it stands? I use the code below to display the html table of results; while($row = $res->fetch_array()) { echo '<tr>'; echo '<td>' . $row['id'] . '</td>'; echo '<td>' . $row['title'] . '</td>'; echo '<td>' . $row['author'] . '</td>'; echo '<td>' . $row['cat_name'] . '</td>'; echo '<td>' . $row['isbn'] . '</td>'; echo '<td>' . $row['publicationYear'] . '</td>'; echo '</tr>'; echo '</tbody>'; }; Still teaching myself php/mysql so I appreciate the patience and thanks in advance.
  5. Thanks @Ch0cu3r and @Barand - perfect
  6. @Barand if you read the question carefully you will see that it's actually different. The post in the MySql forum has been answered. However if you feel this is a double post then feel free to delete it.
  7. I have two tables 'book' and 'category'. They look like the following in phpmyadmin; book id title author category isbn ---- ------- ---------- ---------- ------- 1 Treasure Chest Jim Jones 1 14252637 2 Pirates Boat Sue Smith 2 88447737 3 Adventure Land Harry Jo 3 01918273 4 Winter Week Sam Dill 3 00999337 5 The Twite Roald Dahl Fiction 87873366 category id cat_name ---- ------- 1 Horror 2 Kids 3 Fiction 4 Science Users have the option of adding books into the library via an online form, or via a Google Booka api method (user enters isbn, searches, is presented with book info and then clicks 'add to library', done.). This is handled via ajax. The online form works fine, and successfully adds the book info. However my problem is with the Google Books method, it successfully adds the data to the db however the category column is in text format (i.e 'Juvenile Science' or 'Scary Fiction') as opposed to the manual form which adds categories as 1, 2 or 3 (1 =Horror, 2 = Kids, 3 = Fiction). Is there any way I can add the Google Book category data to my table and convert it to an integer or similar? Not sure what I need to do. Suggestions appreciated! Should I add the Google entries to another table (i.e googleCategory)? My HTML only outputs the numbered category entries and ignored the text format entries. my php $sql = "SELECT b.id, b.title, b.author, b.isbn, b.publicationYear, c.cat_name FROM book AS b INNER JOIN category AS c ON b.category = c.id WHERE status != 'Archive' ORDER BY id DESC LIMIT $startrow, 15 "; $res = $conn->query($sql) or trigger_error($conn->error."[$sql]"); while($row = $res->fetch_array()) { echo '<tbody>'; echo '<tr>'; echo '<td>' . $row['id'] . '</td>'; echo '<td>' . $row['title'] . '</td>'; echo '<td>' . $row['author'] . '</td>'; echo '<td>' . $row['cat_name'] . '</td>'; echo '<td>' . $row['isbn'] . '</td>'; echo '<td>' . $row['publicationYear'] . '</td>'; echo '</tr>'; echo '</tbody>'; }; Apologies if this is all a bit confusing I am very new to php and mysql. Thanks, J
  8. I figured it out I should have had echo '<td>' . $row['cat_name'] . '</td>'; Instead of echo '<td>' . $row['category'] . '</td>'; Thanks again @Ch0cu3r for the excellent answer!
  9. Thanks @Ch0cu3r - I can now see a full table of info however my 'Category' field in the table is completely empty. I'm getting a notice regarding line 104 (marked below). Notice: Undefined index: category in C:\xampp\htdocs\sslib\books.php on line 104 I have provided my complete code below, thanks. $sql = "SELECT b.id, b.title, b.author, b.isbn, b.publicationYear, c.cat_name FROM book AS b INNER JOIN category AS c ON b.category = c.id WHERE status != 'Archive' ORDER BY id DESC LIMIT $startrow, 15 "; $res = $conn->query($sql) or trigger_error($conn->error."[$sql]"); while($row = $res->fetch_array()) { echo '<tbody>'; echo '<tr>'; echo '<td>' . $row['id'] . '</td>'; echo '<td>' . $row['title'] . '</td>'; echo '<td>' . $row['author'] . '</td>'; echo '<td>' . $row['category'] . '</td>'; // <--line 104 echo '<td>' . $row['isbn'] . '</td>'; echo '<td>' . $row['publicationYear'] . '</td>'; echo '</tr>'; echo '</tbody>'; }; Any ideas why?
  10. I am creating a library app for personal development and would like to further my knowledge of MySql. I currently have two tables 'book' and 'category' book id title author category isbn ---- ------- ---------- ---------- ------- 1 Treasure Chest Jim Jones 1 14252637 2 Pirates Boat Sue Smith 2 88447737 3 Adventure Land Harry Jo 3 01918273 4 Winter Week Sam Dill 3 00999337 category id cat_name ---- ------- 1 Horror 2 Kids 3 Fiction 4 Science I am doing a simple search where I want to display all books (select * from book) and I would like to display all the books with their corresponding categories. This works but displays the category number instead of the category name. How can I alter these tables in such a way that the cat_name and category are joined? I tried to run a command in phpmyadmin as below however it returned an error; ALTER TABLE book ADD FOREIGN KEY (category) REFERENCES category(cat_name); error #1452 - Cannot add or update a child row: a foreign key constraint fails (`sslib`.`#sql-1ab4_1dae`, CONSTRAINT `#sql-1ab4_1dae_ibfk_1` FOREIGN KEY (`category`) REFERENCES `category` (`cat_name`)) I have been looking at foreign keys and indexes however I can't seem to make any sense of them. I am quite new to this so any help is much appreciated. Regards, J
  11. Ah figured it out <?php echo '?isbn='.str_replace ('-', '', $row['isbn']); ?>
  12. Firstly I'm sure the answer is simple however due to my inexperience I am unable to figure it out! I am executing a simple query to retrieve all ISBN numbers within my db and display these in a table. In my db some of the ISBN numbers contain hyphens and some don't. I would like to display then in my table without hyphens. I'm grateful of any assistance and advice. I know I can remove all the hyphens from the db by executing a separate query however I'd like to strip them if possibly. My Code; $query = "SELECT * FROM book WHERE status != 'Archive' ORDER BY id"; while($row = mysqli_fetch_array($query)){ ?> <tr> <td><a href="itemView.php<?php echo '?isbn='.$row['isbn']; ?>" </a></td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <?php } ?> J
  13. Actually figured it out, pretty simple. I just had to change my code as follows; 'title' : entry.volumeInfo.title
  14. I am retrieving Google Books info in JSON format and displaying it inside a div. I would like to send the contents of this div (name, title, description) to my database using Ajax. Currently only the ISBN field sends because I have it declared as a variable. However my question is, how do I send the other fields (name, title, author). How do I declare these also, I'm not sure what format they need to be in etc. My JS $(document).ready(function() { $('#submit').click(function(ev) { ev.preventDefault(); var isbn = $('#isbn_search').val(); //get isbn direct from input var url='https://www.googleapis.com/books/v1/volumes?q='+isbn; $.getJSON(url,function(data){ $.each(data.items, function(entryIndex, entry){ var html = '<div class="results well">'; html += '<h3>' + entry.volumeInfo.title + '</h3>'; html += '<div class="author">' + entry.volumeInfo.authors + '</div>'; html += '<div class="description">' + entry.volumeInfo.description + '</div>'; }); }); }); }); My Ajax; $.ajax({ type: 'POST', url: 'addIsbnScript.php', data: { 'isbn' : isbn, 'title' : title 'subtitle' : subtitle, 'authors' : authors, 'description' : description }, success: function () { $.growl({ message: " Record added" }); } }); Note, if i manually set the vars like below they all do successfully send to my database, so I know my query is working ok var title = "some text", var author = "some text", Var description = "some text" Thanks in advance for any help, newbie here (incase it wasn't obvious!). J
  15. I am creating a library app (personal dev) and have ran into some trouble. I'm very new to Ajax and Php I have a page named addEntryISBN which shows the results of a user search in a div named #results. I want to post the contents of #results to my database. The contents of #results comes from a page named searchISBN. What is the most effective way of doing this? My code so far is aas follows; addEntryISBN.php $(document).ready(function() { $('form').on('submit', function (e) { e.preventDefault(); $.ajax({ type: 'post', url: 'searchIsbn.php', data: $('form').serialize(), success: function (result) { $('.result').html(result); } }); return true; }); }); <div class="result"</div> searchISBN.php <?php echo '<table class="table">'; echo '<tr>'; echo '<h3>Book Info</h3>'; echo '</tr>'; echo '<tr>'; echo '<td> Title </td>'; echo '<td>' . $title . '</td>'; echo '</tr>'; echo '<tr>'; echo '<td> Page Count </td>'; echo '<td>' . $pageCount . '</td>'; echo '</tr>'; echo '<tr>'; echo '<td> Publish Date </td>'; echo '<td>' . $publishDate . '</td>'; echo '</tr>'; echo '<tr>'; echo '<td> Description </td>'; echo '<td>' . $description . '</td>'; echo '</tr>'; echo '</table>'; echo '<form><input id="add_isbn" method="post" type="submit" value="Add This Book" /></form>'; } ?> I hope this makes sense! Any help is much appreciated. J
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.