jcbones
Staff Alumni-
Posts
2,653 -
Joined
-
Last visited
-
Days Won
8
Everything posted by jcbones
-
Just add it in on line 131.
-
HTTP_POST_FILES was deprecated in PHP4.1.0, which was release December 10, 2001. You should be using the $_FILES array. This one point alone, makes the current script 10+ years out of date. PLEASE don't use the video's as a valid way to check if the file uploaded is an actual image. Anyone can put an image ext on any file they wish to. My suggestion would be to use finfo_open() asking for mime type, finfo_file(), and finfo_close(), and checking against the IMAGETYPE_*** constants. You could use getimagesize(), or exif_imagetype(), the first just gives more info than the second. You can also try to recreate the image with imagecreatefromjpeg() (or png, gif, etc), which will fail if it isn't an image. This way is more resource intensive, but it will let you know 100% if it is an image or not. Bottom line is, don't trust file extensions.
-
Not sure what $blogTagParams has in it, but: $blogTagInsert = 'INSERT INTO blog_post_tags (blog_post_id , tag_id) VALUES (?, ?)'; $blogTagParams = array($blogPostId, $tagId) $blogTagstmt = $DBH->prepare($blogTagInsert); $blogTagstmt->execute(array($blogPostId,$tagId));
-
Remove the error suppression: @ Next you need to return any errors that the database is sending. $result = mysql_query($query) or trigger_error(mysql_error()); Additionally, this code will not work very long, as mysql has been depreciated and is slated for removal. You should change over to the PDO or mysqli classes, in order to avoid future downtime.
-
All of this is, of course, based on the notion that your database tables are set up so that the date is held in a proper `date`, `datetime`, or `timestamp` field.
-
You need to re-read the PDO manual.
-
As well as this array: $candy = array('chocolate'=> 'M&Ms', 'chocolate'=>'Snickers', 'chocolate'=>'Reese\'s', 'gummy'=>'bears', 'gummy'=>'worms'); Doesn't contain what you think it does. Dump it and see. echo '<pre>' . print_r($candy,true) . '</pre>';
-
sending a list from a form to a mysql table as 1 record can i
jcbones replied to Baxt01's topic in PHP Coding Help
So, create these tables in your database (should be able to copy paste). -- -------------------------------------------------------- -- -- Table structure for table `players` -- CREATE TABLE IF NOT EXISTS `players` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) COLLATE utf8_bin NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `player` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=0 ; -- -------------------------------------------------------- -- -- Table structure for table `scores` -- CREATE TABLE IF NOT EXISTS `scores` ( `games_id` int(11) NOT NULL, `players_id` int(11) NOT NULL, `score` int(11) NOT NULL, KEY `games_id` (`games_id`,`players_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- -------------------------------------------------------- -- -- Table structure for table `games` -- CREATE TABLE IF NOT EXISTS `games` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(40) COLLATE utf8_bin NOT NULL, `dateOfEvent` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=0 ; Next you need a database connection script: (make sure you edit these to the proper credentials. config.php <?php //Edit these values to reflect your database connection. define('DATABASE','test_tourny'); //name of database define('USER','root'); //name of database user define('PASSWORD',''); //password to access database define('HOSTNAME','localhost'); //server where database is located //No need to edit below this line: $db = new mysqli(HOSTNAME,USER,PASSWORD,DATABASE); Next we need a insertion script: (this should be a members only script, so it needs to be locked like your script to save to a file. <?php include('config.php'); //You must have a database connection. $date = date('Y/m/d'); //current date in Year/Month/Day format (just the way the database likes it). if($_SERVER['REQUEST_METHOD'] == 'POST') { //if the page was POSTED to (form sent). $parts = explode(PHP_EOL,$_POST['text']); //break up the text area on the line breaks. $game = $_POST['game']; //get the game name. unset($_POST['text']); //free memory of the $_POST variable. foreach($parts as $part) { //loop the array we created. list($name,$score) = explode(' ',$part); //break the names from the scores. $name = trim($name); //get rid of whitespace. $score = trim($score); //get rid of whitespace. $names[] = sprintf("'%s'", $db->real_escape_string($name)); //escape the names for db insertion, and put them in an array. $scores[$name] = $score; //save scores to array, associated with the name. } //This block is for game name insertion. $sql = "INSERT INTO games (name,dateOfEvent) VALUES (?,?)"; $stmt = $db->prepare($sql); $stmt->bind_param('ss',$game,$date); $stmt->execute(); $gameID = $stmt->insert_id; //get the game ID back from the database. $stmt->close(); //end game insertion. //insert all names into the players table. $sql = "INSERT INTO players (name) VALUES (" . implode('),(',$names) . ')'; if(!$result = $db->query($sql)) { //if it fails, throw an error. trigger_error($db->error); } //get the id's back from the players table, since the database creates them for us. $sql = "SELECT id, name FROM players WHERE name IN(" . implode(',',$names) . ')'; $names = array_flip($names); //flip the names array, so the player names are the keys. if($result = $db->query($sql)) { //execute the query, if it fails run the else block below, if it is successful, drop to the while statement. while($row = $result->fetch_assoc()) { //loop through the results, one row at a time. $names[$row['name']] = $row['id']; //reset the values in the names array to the ID's from the players table. } } else { trigger_error($db->error); //throws error if the database query fails. } foreach($scores as $name => $score) { //loop through the scores. $sqlPart[] = sprintf("%d,%d,%d", $gameID,$names[$name],$score); //create an array holding the values for the score insertion. } $sql = "INSERT INTO scores(games_id,players_id,score) VALUES (" . implode('),(',$sqlPart) . ')'; //string together the scores for a multi-row insert into the scores table. if(!$db->query($sql)) { //execute the query, if it fails. trigger_error($db->error); //throw an error. } } //show the form. ?> <form action="" method="post"> <label for="game">Game Name</label><br /> <input name="game" id="game" type="text" /><br /> <label for="text">Name / Score list</label><br /> <textarea cols="50" rows="10" name="text"></textarea><br /> <input type="submit" name="submit" value="Submit" /> </form> Next we need a script that will search and display the results: <?php include('config.php'); //must have a database connection. //main part of query string. $sql = "SELECT g.name AS gName, p.name AS pName, s.score FROM games AS g JOIN scores AS s ON s.games_id = g.id JOIN players AS p ON p.id = s.players_id"; if(!empty($_GET['search'])) { //if a search was submitted. $search = trim($_GET['search']); //get rid of the whitespace. if(stripos($search,'game') !== false) { //if it contains the lowercase word 'game' list(,$search) = explode(' ',$search); //get the part that isn't 'game' $sql .= sprintf(" WHERE g.name LIKE '%s%%'", $db->real_escape_string($search)); //but search the database on the game name. } else { $sql .= sprintf(" WHERE p.name LIKE '%s%%'", $db->real_escape_string($search)); //else search the database for the player name. } } $sql .= " ORDER BY g.name, s.score DESC"; //add the order by, which searchs for the game name and player score DESCENDING. if(!$result = $db->query($sql)) { //if the query fails trigger_error($db->error); //throw error. } //if the query succeeds, pring form and results. echo '<form action="" method="get"> <input type="text" name="search" /> <input type="submit" name="submit" value="Search" /><sub>case sensitive</sub> </form> <table>'; $i = 0; //control number. while($row = $result->fetch_assoc()) { //loop through the rows in the database. if(++$i == 1 || (isset($lastGame) && $lastGame != $row['gName'])) { //if the incremented control number is equal to 1 or the lastGame is different from the current game. echo '<tr><th colspan=2>' . $row['gName'] . '</th></tr>' //echo the table headers. . '<tr><th>Name</th><th>Score</th></tr>'; } echo '<tr><td>' . $row['pName'] . '</td><td>' . $row['score'] . '</td></tr>'; //now echo each and every row. $lastGame = $row['gName']; //end of the loop, store current game name to lastGame to be checked on new row. } echo '</table>'; //table ends, all finished. Thats it, You can make it more efficient, but I just had a moment to play around. Hope it gets you close. -
sending a list from a form to a mysql table as 1 record can i
jcbones replied to Baxt01's topic in PHP Coding Help
Here are a couple of questions, and we can get this sorted. 1. Is there a specific games that the players play, ie game specific to this week, or 1 of multiple games this week? 2. Do you want a specific players name searchable, ie shows all games and scores for individual player? -
Update query with nl2br and preg_replace, something funny happening
jcbones replied to Guber-X's topic in PHP Coding Help
I would try adding one bbcode at a time, until the divisions started showing up. I think you will find it a nesting problem, but I cannot spot it right off hand. -
I understand what you are saying, just not why you are trying to do it. What I would do (if I wanted to do this): Use a database. If that wasn't an option: 1. write the files. file_put_contents() 2. loop through the files. glob() file_get_contents() 3. see if the contents matched. strcmp() 4. write the results to a file combining the results. file_put_contents()
-
Update query with nl2br and preg_replace, something funny happening
jcbones replied to Guber-X's topic in PHP Coding Help
I would suggest to convert the bbcode on the display, instead of the insertion. This way you could return the bbcode back to the user for editing. -
Wrap the database call, and display inside of: if($_SERVER['REQUEST_METHOD'] == 'POST') { //database call and display... }
-
First I would check if the files are indeed returning the same values. Then decide the best course of action from there. After all, it is better to make an informed decision, than to throw things at it in an attempt to get it to "work". $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae tortor odio. Proin ac enim a orci consectetur venenatis. Nunc metus nunc, convallis eget tincidunt sit amet, suscipit in urna. Maecenas imperdiet vestibulum malesuada. Nulla quis pharetra neque. Nunc a erat interdum, dictum metus in, dignissim orci."; $text_split = str_split($text, 25); foreach($text_split as $texts) { $text_ok = urlencode($texts); if(!@file_get_contents("http://example.org/?q=".$text_ok)) { echo "Error."; } else { $txt = file_get_contents("http://example.org/?q=".$text_ok); $file = "texts/".md5($text).".txt"; file_put_contents($file, $txt); } echo $text_ok . ' = ' . $txt . '<br />'; }
-
Edit: this forum hates Chrome!!! Note that you should really pass the database object into the class, instead of creating it inside the class. This is so that you will have access to it inside and outside the class. Also, you should name your function System() to __construct(), as the former is sooooooo PHP4. It will not be treated as a constructor after PHP5.3.3. This function is expecting to see a variable $IDSystem, but you haven't defined it: public function System($IDSystem) { $this->db = new Database(); $this->IDSystem = $IDSystem; } //If you want it to have a default value: [code] public function System($IDSystem = 1) { $this->db = new Database(); $this->IDSystem = $IDSystem; }
-
The reason you are getting duplicates is that the URI in the file_get_contents() is returning the same text. Thereby duplicating it. To verify this, add a little debugging. echo $text_ok . ' = ' . $txt . '<br />';
-
Are you talking about adding bad words to a bad words list? Not tested- May not be error free <?php if(!empty($_POST['badwords'])) { if(strpos(',',$_POST['badwords']) !== false) { $arr = explode(',',$_POST['badwords']); $str = implode(PHP_EOL,$arr); } else { $str = $_POST['badwords'] . PHP_EOL; } file_put_contents('badwords.txt',$str,FILE_APPEND); } if(file_exists('badwords.txt')) { $badwords = file('badwords.txt'); } else { $badwords = array(); } $text = 'This is a test. Ass. Grass. bad1.'; function filterBadwords($text, array $badwords, $replaceChar = '*') { return preg_replace_callback( array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords), function($match) use ($replaceChar) { return str_repeat($replaceChar, strlen($match[0])); }, $text ); } echo filterBadwords($text, $badwords); ?> <form action="" method="post"> <label for="badwords">Add a Word to the Filter</label><br /> <input type="text" name="badwords" /> <input type="submit" name="submit" value=" ADD " /> </form>
-
Are you running this in a loop? What you are stating would only make sense if you are running in a loop. Using "if no file exists" with a FILE_APPEND doesn't really make any sense. Since you are creating the file if it doesn't exist, there is nothing to append. That is why you only have 1 record. Perhaps showing a little more code for context, we can help you get it sorted.
-
Check your POSTed variables. <?php echo '<pre>' . print_r($_POST,true) . '</pre>';
-
Check all of your super global variables ($_GET, $_POST, $_SESSION, $_COOKIE) to make sure they exist. Then check to make sure that your database column type and size allow the values you are passing.
-
Your object creation is failing, returning a boolean false. Make sure your date string matches your format string. Right now you are telling it to create the string from a date that looks like "Mon, 01 Jan 2013 23:40:12 +0500". I would suggest dumping the $datestr variable, and making sure it matches that format.
-
I'm not sure what you are trying to do with the files. Are you just trying to set the ON/OFF text on the page to "active" or "in-active"? If you post the reasons, or goal of what you are trying to accomplish, then we can give you the best course of action to take. There are several ways to do anything in PHP, but some ways are more efficient. Case in point. Instead of fopen(), fwrite(), fclose(), you could just use file_put_contents(), file(), or file_get_contents().
-
Date, timezone problems after upgrading server to PHP 5.3.27
jcbones replied to Tomislav's topic in PHP Coding Help
it isn't date.strtotime() it is just strtotime(). The error is telling you that PHP cannot find a constant named date and thinks it is suppose to be a string. Then it throws an error that says the second parameter of date() is passed as a string. Those two clues led me to believe that the "date" part of date.strtotime() was not right. -
When you create a public_html folder in your home/user folder, make sure you symlink it back to /var/www
-
Is apache the group that the server runs under? I know that most of the time apache server runs under the user www-data. But, being that it is 777, this shouldn't be the problem. Are you sure the file path is correct: /home/orders NOT: /home/user/orders OR, you could just remove the error suppression, and see if fopen() is throwing a warning.