Use the "Change Branch" above to select the relevant code sample.
spbooks / phpmysql6 Goto Github PK
View Code? Open in Web Editor NEWCode archive for the book PHP: Novice to Ninja, 6th Edition
Code archive for the book PHP: Novice to Ninja, 6th Edition
The editjokes.php page is not updating when i use the save() function
Your help will be appreciated.
I have written the code below;
function save($pdo, $table, $primaryKey, $record){
try{
if($record[$primaryKey] == ' ') {
$record[$primaryKey] = null;
}
insert($pdo, $table, $record);
}
catch(PDOException $e) {
update($pdo, $table, $primaryKey, $record);
}
A bit confused by what I'm thinking must be typo or update error. Since I'm still in the novice category, I can't figure it out.
On page 142 in the book, it says:
"You’ll have noticed that there are two different variables being set, $jokes and
$error, depending on whether the try block executed successfully."
This is the first time $error appears. I believe it's referring to $e from "(PDOException $e)" but I haven't been able to confirm that despite looking through much of the book, placing errors in the code, searching the pdf version, and searching the website's forums. $e seems to used throughout the book in the try-catch sequences as part of $output. $error seems to be in the template files to be printed out, but I can't find anywhere that it's defined.
What am I missing? Thanks
So I am trying to improve user registration a little bit and by that I mean password checker. So I added two new methods to Register.php ContainsNumbers -> Which checks if string contains a number. Min8 checks if string has 8 or more characters.
While trying to register a user to see if he/she could register to a page bypassing basic password requirements I get this:
Fatal error: Uncaught Error: Call to undefined function Ijdb\Controllers\ContainsNumbers() in /srv/http/Project/classes/Ijdb/Controllers/Register.php:76 Stack trace: #0 /srv/http/Project/classes/Ninja/EntryPoint.php(47): Ijdb\Controllers\Register->registerUser() #1 /srv/http/Project/public/index.php(8): Ninja\EntryPoint->run() #2 {main} thrown in /srv/http/Project/classes/Ijdb/Controllers/Register.php on line 76
Line 76 of Register.php looks like this:
if(!ContainsNumbers($author['password']) && (!min8($author['password']))){
Contains numbers ,method:
public function ContainsNumbers($password){
if (preg_match('/[0-9]/', $password)){
return true;
}
else{
return false;
}
}
min8 method:
public function min8($password){
if (strlen($password)>=8){
return true;
}
else{
return false;
}
}
hi Tom, thank you. i made it using xampp with directory structure C:\xampp\htdocs\ijdb6\Project\public
if anyone wants to try using xampp, here's how:
change file httpd.conf like this:
you can search the file in c:\xampp\apache or you can hit Config button in Xampp control panel and choose httpd.conf file
dont forget to stop and restart your apache server so the change will take effect. just hit stop and start button in Xampp control panel
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L,QSA]
you can create this file using IDE or using notepad. with notepad choose to 'save as' and 'save as type' All Files
you need windows special permission to make the edit. try make the edit using windows provided account named administrator
thats all.
Tom has introduced very good technique that we should put our front controller (index.php) and other necessary files in root folder but put those important files outside of this folder. by making these changes we can make our root folder 'public' to serve the internet while we put the important files out from this folder. it greatly improve security
the provided .htaccess file make sure that for every non-existent folder/path or file we query in browser's address bar will go to index.php. the next thing is obvious, we make the index.php to response to specific URI. in other words, any URI is acceptable to the controller as long as we provide their corresponding response
After the implementation of the framework the display of joketext in the textarea no longer works...
Editing actually inserts a new entry to the DB, and breaks the ability to edit existing jokes
This;
` $author = $this->authorsTable->findById($joke->authorId);
$jokes[] = [
'id' => $joke->id,
'joketext' => $joke->joketext,
'jokedate' => $joke->jokedate,
'name' => $author->name,
'email' => $author->email,
];
`
Should be:
` $author = $this->authorsTable->findById($joke->authorId);
$jokes[] = [
'id' => $joke->id,
'joketext' => $joke->joketext,
'jokedate' => $joke->jokedate,
'name' => $author->name,
'email' => $author->email,
'authorId' => $author->id
];`
Otherwise, you get error messages where Edit & Delete should be. (It's correct in the sample code.)
The method hasPermission (in Author class) uses $this->permissions
but the field isn't defined in the class.
(Also I wonder whether the line $joke['jokedate'] = new \DateTime();
in the saveEdit method inside the Joke controller is not violating the separation of concerns principle and the [awesome] explanations of the book about why we use an ORM? Here, if I'm not mistaken, we're directly referring to an implementation detail of our jokes table-which is the field name "jokedate".)
Here are few errors when I try running "index.php" :
PHP Fatal error: Uncaught TypeError: Argument 2 passed to Ninja\EntryPoint::__construct() must be of the type string, null given, called in /srv/http/ph/examples/index.php on line 6 and defined in /srv/http/ph/classes/Ninja/EntryPoint.php:9
Stack trace:
#0 /srv/http/ph/examples/index.php(6): Ninja\EntryPoint->__construct('', NULL, Object(Ijdb\IjdbRoutes))
#1 {main}
thrown in /srv/http/ph/classes/Ninja/EntryPoint.php on line 9
When I got the book, I already got my LAMP server running, ph is just directory for php projects. Also, I am using php storm IDE
By the question, I mean how it's forms is protected from javascript and html code? The book had written about htmlspecialchars function, however I can't see it anywhere in code.
Hello.
It is a grate book and I adore it. But it seems to me I found out an error in NGINX configuration for URL Rewriting.
location / {
try_files $uri $uri/ /index.php?args;
}
Missed a dollar sign before args. Should be:
location / {
try_files $uri $uri/ /index.php?$args;
}
It's simple, but it drove me crazy while debugging.
After downloading the sample code, am receiving multiple errors
***** LOGGING OUT*****
The logout.html.php is displayed if a logout attempt is made, and the link changes to "Login".
But the session is not destroyed, is still possible to navigate to edit jokes. And as soon as you navigate away from the logout page, the link returns to "Logout".
******EDIT ****
Logging out now working ... Added session_destroy(), but other errors persist(help would be appreciated)
public function logout() {
unset($_SESSION);
session_destroy();
return ['template' => 'logout.html.php', 'title' => 'You have been logged out'];
}
This is correct in the sample code.
else if (isset($routes[$this->route]['permissions'])
➥ &&
!$this->routes->checkPermission
(**$$routes**[$this->route]['permissions'])) {
header('location: /login/error');
}
else
Also, the sample code appears to be missing the functionality for hasPermission() though it's in the book.
The error (from Intellij IDEA) says SET without WHERE clause....etc. Why isn't the rest of the query visible to PHP?
Here is the code which causes error:
function updateJoke($pdo, $fields) {
$query = ' UPDATE joke
SET '; <--------"Reference or Select expected"
foreach ($fields as $key => $value) {
$query .= '`' . $key . '` = :' .$key . ',';
}
$query = rtrim($query, ',');
$query .= ' WHERE `id` = :primaryKey';
It's just an example code does not affect the code for the website, but just saying...
What's showing in the book:
// Delete the book with the ISBN 978-3-16-148410-0
delete($pdo, 'book', '978-3-16-148410-0', 'isbn');
Believe the isbn# and 'isbn' should change place, like following:
// Delete the book with the ISBN 978-3-16-148410-0
delete($pdo, 'book', 'isbn', '978-3-16-148410-0');
This is the function code:
function delete($pdo, $table, $primaryKey, $id ) {
$parameters = [':id' => $id];
query($pdo, 'DELETE FROM `' . $table . '`
WHERE `' . $primaryKey . '` = :id', $parameters);
}
Hello and thanks for this book!
First of all I'm on php so maybe it's a silly question.
I have a problem when a try to rewrite URL on chapter 9:
I think that the problem is in the following line on Cms-EntryPoint-Class (index.php)
$route = ltrim(strtok($_SERVER['REQUEST_URI'], '?'), '/');
because it duplicate my URL after "public":
I tried to give a empty string to $route and it works for the index page, but obviously the other links in the page doesn't work.
I also try to create an .htaccess file in the public directory and I try to change the Apache root folder in httpd.conf but it doesn't work for me and give me back an error 500.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.