-
Posts
4,704 -
Joined
-
Last visited
-
Days Won
179
Community Answers
-
kicken's post in Make youtube video play on click of image was marked as the answer
The variable is undefined because of how variable scope works in JS. Variables are limited to the function they are defined in.
Regardless, you shouldn't be typing that into the console, it should just be part of your script.
-
kicken's post in PDO connection with error check was marked as the answer
Why do you think it wouldn't work for other errors? It says in the manual
It doesn't say it throws on invalid username/password, it says it throws on failure. Invalid username / password is only one of many reasons why the connection attempt might fail.
-
kicken's post in PHP and Text (txt) was marked as the answer
This is wrong. fopen returns a resource, but file_get_contents wants a filename. This should be triggering a warning and/or error for you (depending on PHP version), something like:
if you cannot see that warning/error in your output or in PHP's error logs then you need to adjust your error reporting settings for your installation.
If you want to use file_get_contents to read a file, then you don't fopen it first, you just call file_get_contents.
$contents = file_get_contents('accounts.txt'); If you want to fopen the file (for example, so you can lock it) then you need to read the data using fread, fgets, stream_get_contents, or similar.
-
kicken's post in Prevent Blank entry into database and doesnt allow duplicate entry in database was marked as the answer
Then you need to validate the entered value to ensure it is not empty before running your query.
Then you need to add a unique constraint to your database table to prevent duplicates.
-
kicken's post in PHP form not posting on SQL database was marked as the answer
You cannot nest forms. That should just be a single form tag with all the attributes, not separate.
This will only be true if you have an input in your form with name="submit". Since you do not, it will never be true. A generic test for a form post is to test the request method.
if ($_SERVER['REQUEST_METHOD']==='POST'){
-
kicken's post in Remove extra data within meta value... was marked as the answer
The original data is JSON. If the " is actually part of the data and not a copy/paste issue here, then it's been run through htmlentities() or similar and you'll need to reverse that.
If you're using a new enough version of mysql, you could potentially use json_extract to get the URLs.
select json_extract(replace(jsonColumn, '"','"'), '$[*].imageurl') Otherwise, you'd probably just want to select the column, and parse and extract the data as you fetch results in your code.
-
kicken's post in Dynamically accessing html value within the same file was marked as the answer
Why? Your current JS code essentially just gets the SID using an input text field, just in a prompt() window instead of the page. There's really no effective difference. If this is just some placeholder for whatever you want to do with your RPi, maybe expanding on that would be good. The only way I imagine grabbing a value with JS from a RPi would be via an AJAX request.
All in all, I'm confused and don't really understand what you're trying to do or why?
You say
As mentioned, the typical way to accomplish that would just be a simple html form without any JS complexity behind it. The warning you mentioned is probably from trying to run the form processing code without having submitted the form. If you're using the same page to display and process the form, your form processing code needs to only run when the form is submitted, not during the initial page load. This is typically done with something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST'){ //Form processing code here }
-
kicken's post in Error in the first 2 lines - PDO. was marked as the answer
Notice the highlighting error?
You have an extra ' after your dbname parameter.
-
kicken's post in How to store an image in MySQL was marked as the answer
If you want to store a path to an image in the DB, just use a VARCHAR column with some resonable maximum length. If you have control over the images file names (ie, you're making them / receiving them as an upload) then you can choose a length and just stick to it. For uploaded files, I tend to use a VARCHAR(16) and generate a random name of that length for the file.
If you don't control the name (say, you're just storing a URL to an external image) then choose a maximum length that should be fine and just validate that in your form when creating a record. Either that or us a text column if you want to allow crazy long URLs. I tend to use VARCHAR(2048) for arbitrary URLs.
-
kicken's post in Escape string for file uploads to Mysql in Php was marked as the answer
You'd just do the same thing, but with the variable for your file name.
$ppname = mysqli_real_escape_string($conn , $ppname); However, this is not really the way you should be handling this issue. Instead, you should be using prepared statements with bound parameters.
$sql = " UPDATE users SET band2 = ?, audio1 = ? WHERE id = ? "; $stmt=mysqli_prepare($conn, $sql); $stmt->bind_param('ssi', $_POST['band2'], $ppname, $id); $stmt->execute();
-
kicken's post in PHP code validatation was marked as the answer
You have your if structure's out of order. This is the structure you currently have:
if ($_SERVER['REQUEST_METHOD'] === 'POST'){ //validate data if (empty($errors)){ //save file } } else { //Display errors } You are only trying to display the errors when it's a GET request, which won't be running any of the validations and thus never generate any errors.
The code to display your errors should be the else branch of your empty($errors) check instead.
-
kicken's post in seeking tips with reading files was marked as the answer
It's better in that you're not doing a line-based approach any more. What I would say is you're getting a little too specific right now. If you look at the linked reference, you'll see:
What you can take from that, is your file will essentially a repeating sequence of "\xFF\x??<marker>\x????<length>\x??...<data>". The two exceptions to worry about right away are the start of image and end of image markers, they don't have a length and data component.
As such, you should start by being able to parse that repeating sequence. Don't worry about parsing what exactly is contained inside the data, just get the individual blocks. In pseudo code that'd be something like:
while (!feof($file)){ $marker = findNextMarker($file); //Scan the file until you find a 0xFF?? value, return the ??. if ($marker !== 0xD8 && $marker !== 0xD9){ $length = parseLength($file); //Read two bytes, convert them to a integer length value $data = fread($file, $length - 2); // -2 because the encoded length includes the two bytes read above. } }
I did this, and have a simple script that does like I said above, just parses out the different blocks and shows a hex dump of their data. I'll share eventually, but I want to see what you come up with after taking the above into consideration first.
-
kicken's post in What are they mening? was marked as the answer
The document root of your project should be a sub-directory within your project, rather than the project folder itself.
project └── www ├── count.html.php └── index.php They are saying to move that file out of the document root sub-directory and put it into the project folder itself.
project ├── count.html.php └── www └── index.php Since the document root of the web server would point to the www sub directory, the count.html.php file is now inaccessible by any url.
-
kicken's post in Filter Date Range not working!! was marked as the answer
Did you get that query from your code by echoing out $sql?
echo $sql; $result = oci_parse($conn,$sql); The question is whether the query your generating is what you're expecting it to be, so you will want to echo it and verify it.
If you can copy/paste the echo'ed query from the page into sql developer and get results, but get no results in PHP then maybe there is some environment difference or the code for reading the results is problematic. I've not used oracle so cannot say much about it.
-
kicken's post in Calling a function on an IF/ELSE I think was marked as the answer
Make your functions just do nothing if the condition is not met.
function Mileage($entry, $form){ $MileageQty = $entry["5"]; if ($MileageQty <= 0){ return; } $Name = $entry["1"]; $Service = "Fixed-Milage"; $MileageRate = $entry["10"]; $con=mysqli_connect(""); mysqli_query($con,"INSERT INTO beta_pay (Name,Type,Value,Qty) VALUES ('$Name','$Service','$MileageRate','$MileageQty')"); } Since you only want the function to run if $MileageQty is > 0, you check if it's 0 or less and if so, return. Return will exit the function, so nothing that comes after it will be executed.
-
kicken's post in Class error handling examples was marked as the answer
Maybe because whatever you're doing doesn't need OOP. Maybe you just don't understand OOP well enough to see the usefulness. It's hard to say. The example class you posted here seems pretty pointless. If you want to see your class fixed up though,
<?php declare (strict_types=1); namespace SpyderEyes; class Reflection { protected string $mirror; public function __construct(string $shine){ $this->mirror = $shine; } public function shine() : string{ return $this->mirror; } public function dust() : string{ return bin2hex(base64_encode($this->mirror)); } } try { $reflector = new Reflection(['Mirror, Mirror on the Wall']); echo '<p>' . $reflector->shine() . '</p>' . PHP_EOL; echo '<p>' . $reflector->dust() . '</p>' . PHP_EOL; } catch (\TypeError $error){ echo 'Oops'; } The constructor will throw a type error if you pass a non-string value which you can catch and handle if you want. Since the constructor can only accept a string, you ensure that the mirror property will always be a string and thus do not have to do any checking in the individual methods.
One way to dive into the deep end and see how OOP can be beneficial is by looking at the various frameworks that exist. I like Symfony personally and think spending some time using and learning about it can be quite beneficial in showing how useful an OOP approach can be.
A couple other reasons why classes are nice:
Class autoloading saves you from getting into an include/require nightmare. IDE's can easily parse the classes and provide auto-completion.
Yes, return is valid in a function. Returning a value from a constructor is not though. Take your original code that returned false
class Reflection { protected string $mirror; public function __construct($shine){ if (!is_string($shine)){ return false; } $this->mirror = $shine; } } What do you expect return false to do there? If you think it's something you can test for to see if the constructor failed, you're mistaken
$o = new Reflection([]); if ($o === false){ echo 'Oops'; } else { echo 'Success!'; } That if statement will never be true, $o will always be an instance of Reflection. The return only causes the constructor to stop running, which means your mirror property never gets initialized and thus causes you future issues.
This I don't understand at all. Trying to emulate private functions by checking if some session variable exists make zero sense and doesn't actually make the function private at all since all one would have to do to use it is set that variable.
-
kicken's post in get new value of type='date' field in its onchange event was marked as the answer
Because the javascript Date object parses the date you pick as YYYY-MM-DD 00:00:00 UTC. If you use any of the non-utc methods of that date object to access the details of that selected time, it gives you those details in your local timezone (EST) which is -5 hours from UTC (thus, the previous day).
I was thinking you wanted the full date (m/d/Y) value not the day of the week, but either way the solutions are essentially the same.
//1. Extract the day from the generated UTC String console.log(input.valueAsDate.toUTCString().substring(0,3)); //2. Map the day to a string via numeric index (0=Sunday) console.log(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][input.valueAsDate.getUTCDay()]); //3. Create a new date object adjusted for the timezone offset, then do the above but with the local functions const adjustment = ((new Date()).getTimezoneOffset()*60*1000); const cs = new Date(input.valueAsDate.getTime() + adjustment); console.log(cs.toString().substring(0,3)); console.log(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][cs.getDay()]); -
kicken's post in Is it possible to pass multiple variables through one method argument? was marked as the answer
You want Variable length arguments which are accomplished using the ... operator.
// Using ... collects arguments and puts them into an array public function prepareSelect($query, $ts, ...$params){ // Using ... here breaks $params into individual arguments. $this->bind_param($ts, ...$params); }
-
kicken's post in Fatal error: Uncaught Error: Class "mysqli" not found... was marked as the answer
Sounds like a permission problem with the directory where mysql stores it's database files. Seems like your mysql setup must have not be done properly (no unix socket, bad data dir permissions, who knows what else).
-
kicken's post in docker-compose and yii2 not work was marked as the answer
This is a docker-specific problem and their documentation does not appear to cover docker usage.
The error tells you essentially what to do, and this is also documented in the Installing from an Archive File section.
-
kicken's post in prepared statement fail in php8 was marked as the answer
If you're seeing the source code to your page displayed when you load it, then this is not some problem with prepared queries but a problem of your code not being parsed at all. This would either be due to PHP not being installed and configured correctly, or you using short tags and your PHP installation no longer being configured to allow them.
-
kicken's post in Output on Browser after ob_clean command and flush was marked as the answer
If you use a header redirect then the browser will not render the output of that request and instead just fetch the next page. So you need to delay the redirect by returning a normal response that the browser will render and have that page then perform the redirect. That delay can be accomplished by using either JavaScript or a meta refresh to issue the redirect after the page has loaded.
-
kicken's post in mysql select statement is not taking space in consideration was marked as the answer
Think about how that will get rendered after the PHP code is processed. You'll have:
<input class="form-control" type="text" value=Information and Technology disabled readonly> You've set the value to just Information. The words and and Technology become unknown attributes.
You need to put quotes around the value attribute content if you want to be able to use spaces.