
leke
Members-
Posts
39 -
Joined
-
Last visited
Profile Information
-
Gender
Male
-
Location
Finland
leke's Achievements

Member (2/5)
0
Reputation
-
Whoops, it turned out I wasn't auto incrementing the english table.
-
Hi, I'm trying to insert data into two different tables using, but am getting an error I can't figure out. If I move the $mysqli->commit(); into the foreach loop, I get at least one returned row before the rest fail. The error current error message is Array ( [0] => Error: Couldn't insert into english! ). Any idea what is causing this? <?php $file_array = file('../grammar/conjunctions.txt'); $csv = array_map('str_getcsv', $file_array); // DB $mysqli = new mysqli('localhost', 'root', '******', 'angos'); $mysqli->autocommit(false); $error = array(); foreach($csv as $value) { $angos_query = $mysqli->query("INSERT INTO angos (angos, grammar) VALUES ('$value[0]', 'con')"); $id = $mysqli->insert_id; // grab the currant angos table id if($angos_query == false) { array_push($error, "Error: Couldn't insert into angos!"); } $english_query = $mysqli->query("INSERT INTO english (angos_id, english) VALUES ('$id', '$value[1]')"); if($english_query == false) { array_push($error, "Error: Couldn't insert into english!"); } if(!empty($error)) { $mysqli->rollback(); } } $mysqli->commit(); print_r($error); // print_r($csv); ?> More info SQL: CREATE TABLE angos ( id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, angos varchar(255) not null, grammar varchar(3) not null, updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT unique_input UNIQUE (angos) ) engine=InnoDB; CREATE TABLE english ( id int unsigned not null primary key, angos_id int unsigned, english varchar(255), grammar_note varchar(500), CONSTRAINT fk_angos_source FOREIGN KEY (angos_id) REFERENCES angos(id) ON DELETE CASCADE ON UPDATE CASCADE ) engine=InnoDB;
-
To be honest, I didn't really give it much thought I was just going with something that works quick, then I usually play with it and make some changes to the formatting based on problems I might foresee. I usually go with year month day as it's usually the most computer friendly format (along with being quite readable and international), but I also see I made an error in the comment writing mm-dd instead of m-d.
-
htmlentities didn't show any newline evidence, but I noticed in the source output, the elements are formatted with a newline so I guess they hang around when using file().
-
Thanks, trim() worked, and yeah, you were right about the date() thing too Would it have been a \n that needed trimming on those array elements? Thanks again
-
Hi, For some I can't match these two strings... $timestamp = mktime() + CURR_TIME_OFFSET * 3600; $d = date('j', $timestamp); $m = date('n', $timestamp); $y = date('Y', $timestamp); $isCancelledCheck = (string) "$y-$m-$d"; echo "Date: $isCancelledCheck is a " . gettype($isCancelledCheck) . "<br>"; $db_read = file('cancelled_dates.txt'); // read YYYY-MM-DD lines of file to an array. foreach($db_read as $key => $val) { // iterate through each element of db_read if ($val == $isCancelledCheck){ echo "<br> $val == $y-$m-$d"; } else { echo "<br>Skipped $val"; } } print_r($db_read); which returns... How come the string 2013-3-4 is not matched here? Thanks.
-
Wow, it appears I over complicated things Thanks for the file suggestion. $movie_title_file = '4-movies.txt'; $movie_title_db = file($movie_title_file); shuffle($movie_title_db); print_r($movie_title_db);
-
I have a file with four lines of text (no empty lines). and I want to grab four random keys from it... php > $movie_title_file = "4-movies.txt"; php > $access_movie_title_db = file_get_contents($movie_title_file); php > $movie_title_db = explode("\n", $access_movie_title_db); php > $random_index = array_rand($movie_title_db,4); php > shuffle($random_index); php > print_r($random_index); Array ( [0] => 4 [1] => 1 [2] => 0 [3] => 2 ) as you can see, the results range form 0 to 4. That's five though and if I wanted to use the results to print out the index key to that line, then echo $movie_title_db[$random_index[0]]; would print nothing because the $movie_title_db only has 0 to 3 (or 4) items. How come array_rand works like this? Thanks
-
One of my preg_replace lines doesn't work and I can't figure out why. It's very similar to another line which works fine. $quote = '<ol> <li> <span class="bold quote_actor"> Delia Surridge: </span> <span class="line"> Is it meaningless to apologize? </span> </li> <li> <span class="bold quote_actor"> V: </span> <span class="line"> Never. </span> </li> <li> <span class="bold quote_actor"> Delia Surridge: </span> <span class="line"> I'm so sorry. </span> </li> </ol>'; $quote = preg_replace("'<ol>(.*?)<li>'", '', $quote); // first 2 tags $quote = preg_replace("'\s+'", ' ', $quote); // more than one space $quote = preg_replace("'</li>(.*?)<li>'", '<br /><br />', $quote); // separate quote lines $quote = preg_replace("'</li>(.*?)</ol>'", "\n", $quote); // last 2 tags echo $quote; So, everything works fine except $quote = preg_replace("'<ol>(.*?)<li>'", '', $quote); // first 2 tags ...which doesn't do its job. How come? It's very similar to $quote = preg_replace("'</li>(.*?)</ol>'", "\n", $quote); // last 2 tags Which works just fine. Thanks.
-
How can I store the returned result of a function that outputs an array, so I can pass parts of it to other functions? As you can se, this function will have different output each time it is called as a function. Thanks. <?php $apikey = '1234567890'; $movie_title_file = "5000-movies.txt"; $access_movie_title_db = file_get_contents($movie_title_file); $movie_title_db = explode("\n", $access_movie_title_db); function getMovieObject() { global $apikey, $movie_title_file, $access_movie_title_db, $movie_title_db; $random_index = array_rand($movie_title_db,1); $query = $movie_title_db[$random_index]; // end result is a film title to query. $url_title = urlencode($query); // make sure to url encode an query parameters // Taken from the RT API... // construct the query with our apikey and the query we want to make $endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=' . $apikey . '&q=' . $url_title; // setup curl to make a call to the endpoint $session = curl_init($endpoint); // indicates that we want the response back curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // exec curl and get the data back $data = curl_exec($session); // remember to close the curl session once we are finished retrieveing the data curl_close($session); // decode the json data to make it easier to parse the php $search_results = json_decode($data); if ($search_results === NULL) die('Error parsing json'); $movies = $search_results->movies; return $movies; }
-
Unexpected behaviour changing plain text password login check code
leke replied to leke's topic in PHP Coding Help
Solved. Using a different variable later on did something to the session data. I can't find where $pw is used later on, but overwriting the value solved the problem. -
Unexpected behaviour changing plain text password login check code
leke replied to leke's topic in PHP Coding Help
I've managed to follow it to if ( $password_hash == $row["password"] ) { // changed $auth = $row['userlevel']; if ($register) { $_SESSION['authdata'] = array( 'login' => $row['username'], 'password' => $row['password'], 'userlevel' => $row['userlevel'], 'uid' => $row['uid'], ); All the data here is correct until it leaves the function. Then the $_SESSION['authdata'][...] data disappears (resets to $auth = 0;). But all the conditions were met, so I have no idea why it resets. -
I'm modifying php event calendar and tried to add support for sha1 salted passwords. The check seems to return true, but the code to display the calendar fails if I use the new code (no error, just goes back to the original screen). Could someone have a look at the new and old functions responsible for the login? I've commented the changed lines with // changed. I've also modified the calendar not to show unless $auth was true. Also included at the end. This is the one that doesn't evaluate $auth for some reason after the login. Thanks. function auth($login = '', $passwd = '') { session_start(); $auth = 0; $register = false; $authdata = null; if (isset($_SESSION['authdata'])) { $authdata = $_SESSION['authdata']; } # return false if login neither passed to func, nor in session if (empty($login) && empty($authdata['login'])) { return 0; } # get login passed to function if (!empty($login)) { $username = $login; $pw = $passwd; $salt = $pw; // changed $password_hash = sha1($salt.sha1($pw.$salt)); // changed $register = true; } else { $username = $authdata['login']; $pw = $authdata['password']; } mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME) or die(mysql_error()); $sql = " SELECT * FROM " . DB_TABLE_PREFIX . "users WHERE username = '" . $username . "'"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($result); # validate login, and register session data if appropriate if ( $password_hash == $row["password"] ) { // changed $auth = $row['userlevel']; if ($register) { $_SESSION['authdata'] = array( 'login' => $row['username'], 'password' => $row['password'], 'userlevel' => $row['userlevel'], 'uid' => $row['uid'], ); } } else { # if passwords didn't match, delete authdata session data unset($_SESSION['authdata']); } return $auth; } function OLD_auth($login = '', $passwd = '') { session_start(); $auth = 0; $register = false; $authdata = null; if (isset($_SESSION['authdata'])) { $authdata = $_SESSION['authdata']; } # return false if login neither passed to func, nor in session if (empty($login) && empty($authdata['login'])) { return 0; } # get login passed to function if (!empty($login)) { $username = $login; $pw = $passwd; $register = true; } else { $username = $authdata['login']; $pw = $authdata['password']; } mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME) or die(mysql_error()); $sql = " SELECT * FROM " . DB_TABLE_PREFIX . "users WHERE username = '" . $username . "'"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($result); # validate login, and register session data if appropriate if ( $pw == $row["password"] ) { $auth = $row['userlevel']; if ($register) { $_SESSION['authdata'] = array( 'login' => $row['username'], 'password' => $row['password'], 'userlevel' => $row['userlevel'], 'uid' => $row['uid'], ); } } else { # if passwords didn't match, delete authdata session data unset($_SESSION['authdata']); } return $auth; } ...code to display the calendar... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <?php javaScript() ?> <link rel="stylesheet" type="text/css" href="css/default.css"> </head> <body> <br><br> <table cellpadding="0" cellspacing="0" border="0" align="center"> <?php if(!$auth){ echo "<tr><td><span style=\"font-size:xx-large; text-align:center; color:red; padding:100px;\">Please login to see the availability calendar.</span></td></tr>"; } else { echo "<tr><td>"; echo $scrollarrows; echo '<span class="date_header"> '; echo $lang['months'][$m-1]; echo ' '; echo $y; echo '</span></td><!-- form tags must be outside of <td> tags --><form name="monthYear"><td align="right">'; monthPullDown($m, $lang['months']); yearPullDown($y); echo '<input type="button" value="GO" onClick="submitMonthYear()"></td></form></tr><tr> <!-- This is the calendar layout --><td colspan="2" bgcolor="#000000">'; echo writeCalendar2($m, $y); echo "</td></tr>"; } ?> <tr> <td colspan="2" align="center"> <?php echo footprint($auth, $m, $y) ?></td> </tr> </table> </body> </html>
-
I was wondering if it was good practice to use switch without break for this example... <input type="checkbox" name="box1" value="1" /> <input type="checkbox" name="box2" value="1" /> <input type="checkbox" name="box3" value="1" /> <input type="checkbox" name="box4" value="1" /> $box1 = $_POST['box1']; $box2 = $_POST['box2']; $box3 = $_POST['box3']; $box4 = $_POST['box4']; switch ($i = '1') { case $box1: // do something; case $box2: // do something; case $box3: // do something; case $box4: // do something; default: echo "All checkboxes where left unchecked"; } ?> Or perhaps there would be a better way? Thanks.