-
Posts
4,207 -
Joined
-
Last visited
-
Days Won
209
Everything posted by Jacques1
-
None of this requires str_split(). Just iterate over the characters: <?php $input = '1234'; for ($i = 0; $i < strlen($input); $i++) { $char = $input[$i]; var_dump($char); }
-
The example output you claim to need isn't JSON. If you want JSON, use json_encode() together with the right Content-Type header: <?php header('Content-Type: application/json'); $data = ['a' => 1, 'b' => 2]; echo json_encode($data);
-
When to use standard functions versus spl methods?
Jacques1 replied to NotionCommotion's topic in PHP Coding Help
The former is procedural, the latter is object-oriented. Choose whatever suits your application and personal preferences. Functionally, they're equivalent, which is why the manual always mentions both approaches. If you already rely heavily on object-orientation, it makes sense to go all the way. Otherwise use the classical PHP functions.- 1 reply
-
- 1
-
Why? What are you trying to do?
-
Write a coherent problem description with specific information (error messages, observations, things you've tried), format your code properly with code tags, and then we might be able to help you. The mysql_* functions you're using are obsolete since more than a decade and have been removed from recent PHP versions.
-
In PHP, how to save a password in encrypted form ?
Jacques1 replied to abhik143's topic in PHP Coding Help
password_hash() requires an algorithm and should be called with specific parameters: <?php const PW_HASH_ALGORITHM = PASSWORD_BCRYPT; // bcrypt is the de-facto standard and currently the only choice const PW_HASH_COST = 14; // adjust this to your own needs $password = 'test'; $hash = password_hash($password, PW_HASH_ALGORITHM, ['cost' => PW_HASH_COST]); var_dump($hash); -
How to write correctly path in include function?
Jacques1 replied to SGUserFace's topic in PHP Coding Help
Write a coherent problem description (preferably with actual code), then we might be able to help you. -
All you have to do is turn your brain back on and describe the problem in plain English. Something like: “I've tried to insert a new user with the values ..., but when I check the record in the database, all fields are empty”. Is it so hard? But since you're hoplessly confused, and since the approach is generally poor, I suggest you scrap the code and start over. For example: // TODO: distinguish between required and optional attributes const USER_ATTRIBUTES = ['name', 'username', 'password', 'email', /* ... */]; public function addNewUser(array $data) { $values = []; foreach (static::USER_ATTRIBUTES as $att) { if (!isset($data[$att])) { throw new InvalidArgumentException('Missing user attribute: '.$att); } $values[] = $data[$att]; } $addQuery = ' INSERT INTO users ('.implode(',', array_values(static::USER_ATTRIBUTES)).') VALUES ('.implode(',', array_fill(0, count(static::USER_ATTRIBUTES), '?')).') '; $this->attachValues($addQuery, $values); } private function attachValues($sqlQuery, array $params = []) { if ($params) { $stmt = $this->database->prepare($sqlQuery); $stmt->execute($params); } else { $this->database->query($sqlQuery); } }
-
What – is – the – problem? Or did you just want to tell us that it's fine now? Also read #4.
-
What exactly is the problem? Actually, why don't you just fix your database and add the missing default values? Also note that your function has no protection against SQL injection attacks through the array keys. A far more secure and robust approach is to explicitly write down the columns in the query. This not only prevents injection attacks. It also protects columns which should never be manually defined (like auto-incremented IDs).
-
Form processing and return with page reload
Jacques1 replied to lanredoyes's topic in Javascript Help
This tells us absolutely nothing. I know the code “doesn't work”, otherwise you wouldn't be here. The question is: What exactly is the problem? Be specific. What goes wrong when? Are there any PHP or JavaScript errors? What do the developer tools of your browser tell you about the HTTP communication? We're not sitting in front of your screen. We don't have your application on our PC. We know nothing except what you tells us. So if you want help, you need to actually provide information.- 7 replies
-
- no page reload
- form processing
-
(and 1 more)
Tagged with:
-
The code is badly fudged up. You have a random mixture of mysqli_* calls and mysql_* calls (copy-and-paste, I guess), you overwrite your own Location headers because you never stop the script. And the plaintext passwords are hopefully just a bad joke. I'm fairly sure the code does nothing but fill the PHP error log with messages (which you should check, by the way). You should scrap it, learn PDO and start over.
-
It doesn't make sense to hire a programmer when it's a problem with the data. In the worst case, you'll pay a lot of money for useless workarounds. If it's too late to fix the database itself (which is probably the only real solution), you can at least fix the query: SELECT DISTINCT IF(sn_School1 <> '', sn_School1, sn_School2) AS school FROM user_info ORDER BY school ASC Since I don't know your data, I'm guessing here. Maybe your "empty" actually means NULL rather than an empty string.
-
Server to Server database connections
Jacques1 replied to farr433's topic in Other RDBMS and SQL dialects
That was more than three years ago, and the user hasn't been online ever since. Create your own threads, don't hijack or resurrect old topics. -
Form processing and return with page reload
Jacques1 replied to lanredoyes's topic in Javascript Help
Help you with what? You haven't even told us what the problem is. Until then, all I know is that your database queries are messed up. First you use a prepared statement and manual escaping at the same time, which will screw up the data. Then you use neither, which will lead to security vulnerabilities. Use prepared statements; no manual escaping, no unprotected queries. The code is also vulnerable to cross-site scripting attacks. You need to HTML-escape strings before you can insert them into your page.- 7 replies
-
- no page reload
- form processing
-
(and 1 more)
Tagged with:
-
And why are there two columns for the school? Right now, it sounds like you need to repair your database, not write any code. After 11 years?
-
This is a help forum, not a We-write-code-for-random-people-on-the-Internet forum. You are expected to turn your brain on and actively work on the problem. If you do that, you're welcome. If you're just here to show off your laziness and lack of respect, you should go somewhere else.
-
Why is that an issue?
-
The code alone doesn't tell us anything. We need concrete information, which is why I'm asking you to insert var_dump() statements into your code and tell us the exact content of $_POST['comment'] and $comment. Are you sure the comment box in the contact form is a text field with the name comment?
-
“Doesn't work” isn't sufficient information. What exactly is the problem? Is $_POST["comment"] set at all? What's the content of $comment? Use var_dump() to check this. Are there any error messages in the PHP log? The code in general doesn't make a lot of sense. For example, you check if the client used the POST method (you even do it twice, for some strange reason), but you never actually check if the specific fields are present, and you send the e-mail in any case. If I simply visit the page and reload it a couple of times, you end up with dozens of empty e-mails in your inbox. I assume this is all copied and pasted from different websites? While I can understand that you're a beginner who just wants to get things done, this approach will lead to very bad code and is a dead end. You should learn the language and write your own code. Even if it won't be perfect at first, you'll at least understand what's going on and make progress.
-
You haven't attached anything, but you have marked the problem as solved, which means you longer need help. So, case closed? Or are you having trouble with the user interface?
-
PHP: Supported Versions
-
PHP 5.3 was abandoned years ago and doesn't even receive security updates. Either get your hoster install a current PHP version (5.6 or at least 5.5), or get a new hoster. In this particular case, I suspect it's the short array syntax. But like I said, there are much more serious problems. PHP 5.3 is dead.
-
This is not how JSONP works. Why did you choose it, anyway? JSONP is a horrendously insecure legacy hack. Unless you specifically need to support ancient browsers, use CORS instead.