/    Sign up×
Community /Pin to ProfileBookmark

Handle This Error

I’m posting this from my phone. So I’ll explain this without too much code if I can.

I have an object, let’s call it a car class. I’m creating this on my public index page like this..

[code]
$car = new car;
$car->getID($_GET[‘Id’]);
[/code]

Later on in my code I’m using other methods like

[code]
echo $car->getColor();
[/code]

it all works nicety until I have an Id of something that doesn’t exist.

how should I manage this?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmNov 29.2021 — If you are not validating the input before calling this method then you have to modify the method to handle it. Obviously.
Copy linkTweet thisAlerts:
@kiwisauthorNov 29.2021 — @ginerjm#1639995

Obviously.

the first question is how? Or what way is best because…

second secondly simply echo'ing out nothing delivery’s bad page.

is there another way to do something here?

I’m tempted to do a conditional query at the very top and redirect using headers if my get element is not an integer or not existent.

but that doesn’t cater for the scenario where the database doesn’t find a record.
Copy linkTweet thisAlerts:
@NogDogNov 29.2021 — getID() should return something (true/false?) so that you can test the result when you call it, and proceed accordingly or output an error (or whatever makes sense).
Copy linkTweet thisAlerts:
@kiwisauthorNov 29.2021 — @NogDog#1639999

it returns an object if it finds a result and builds it.

When I do my getById code, can I do a conditional statement around that?

then all over mt page I have properties being retrieved and printed, so I need to wrap all of these in a conditional statement if set then else etc?

One of the functions is getting a JSON file and loading it into a JS file so is messy
Copy linkTweet thisAlerts:
@sibertNov 29.2021 — > @kiwis80#1639997 second secondly simply echo'ing out nothing delivery’s bad page.

Just an idea. Not that smart, but might give some other ideas :-)

https://www.db-fiddle.com/f/eaQG8H4yqY9hnQBZjzJgz/46
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...