Jump to content

Mr Chris

Members
  • Posts

    336
  • Joined

  • Last visited

    Never

Everything posted by Mr Chris

  1. Hello, Hopefully an easy one. Say I want to search two tables for the same thing, where each of the tables has the column 'name' in it: How would I correctly do it, I tried: SELECT * FROM table1, table2 WHERE table1.name = 'John' OR table2.name = 'John' But this brings back results for table1 and table2 in a single row, even though the result returned should just be from table2 as that's the only occurrance of John? Thank you
  2. Hello All, I'm trying to work out how to use array_rand() to output a random value in an array. So i've tested it in a single array, and it works fine: $input1 = array("Link1", "Link2", "Link3", "Link4", "Link5"); $rand_keys = array_rand($input1, 2); echo $input1[$rand_keys[0]] . "\n"; However, I want 'Link1', 'Link2' etc to have their own SPECIFIC URL values, so i've tried declaring another array: $input1 = array("Link1", "Link2", "Link3", "Link4", "Link5"); $input2 = array('page.php','file.php','ends.php','smile.php'); //So 'Link1 = page.php, Link2 = file.php etc... $rand_keys = array_rand($input1, 2); echo 'The value for input 1 is: '.$input1[$rand_keys[0]] . 'and the url is '.??????????????.' "\n"; But can't work out how i'd grab this value - anyone kindly help?
  3. Yep sure. Simply forgot to have session_start() on my opening page - my bad. One of those silly errors.
  4. Thanks, but yes I have error reporting on (did not notice the double session, so thanks for that!) but even if I comment out the redirect and replace it with a "I am not aware of any session" it prints that, even though $_SESSION['password'] exists? <?php ini_set('display_errors', 1); ini_set('log_errors', 1); ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); error_reporting(E_ALL); function checklogin() { session_start(); if(!isset($_SESSION['password'])) { echo "I aint aware of any session!"; } } checkLogin(); Print_r ($_SESSION); echo "Erm, I have reached page.php and the password is ".$_SESSION['password']; ?>
  5. Afternoon, Not sure if i've been staring at the screen for too long or not, but have a log in script which saves the password as a session variable and I can pass that variable across my page like so: <?php session_start(); //Print_r ($_SESSION); echo "Erm, I have reached page.php and the password is ".$_SESSION['password']; ?> However, I want to make this secure, so I added a function to it which when you log in says if $_SESSION['password'] does not exist then send me away, like so: <?php session_start(); function checklogin() { session_start(); if(!isset($_SESSION['password'])) { header("location: /index/"); exit; } } checkLogin(); Print_r ($_SESSION); echo "Erm, I have reached page.php and the password is ".$_SESSION['password']; ?> However, using the function and a valid log-in it (as shown above) it chucks me out of the page even though $_SESSION['password'] exists? Am I doing something really stupid here? Thank you
  6. Hello, Wondering if anyone can help me with a PDO query for outputting a html table. Take this query: function outputSchedule ($dbc) { $QUERY = $dbc->query("SELECT * from tbl"); $QUERY->setFetchMode(PDO::FETCH_ASSOC); $output =' <table> <thead> <tr> <th>Name</th> <th>Weight</th> </tr> </thead> <tbody>'; while($ROW = $QUERY->fetch()) { $output .=' <tr> <td style="width:160px;'.$bg.'">'.$ROW['name'].'</span></td> <td style="width:180px;'.$bg.'">'.$ROW['weight'].'</td> </tr> '; } $output .=' </tbody> </table>'; return $output; } You will see I run through a loop to catch all my records in my database, however my problem is with my table headings and the closing of my table. Say there are no records in the database then the start of the table will automatically print (where the <th>'s are) as well as the end of the table. So I want to basically want to use mysql_num_rows to achieve this. Now if it was in a normal query i'd be fine ($NUM = mysql_num_rows then if $NUM > 0, outputting a message if there are no rows in the database). However, i'm not at all sure how to do it using PDO as what I used threw up errors, can anyone help?
  7. Hello all, This is driving me potty. I'm trying to run a query in PDO that a pass to a function. function myFunction ($dbc,$the_date) { echo $the_date; $QUERY = $dbc->query("SELECT * from table WHERE the_date = ':the_date' ORDER BY id DESC"); $QUERY->bindParam(':the_date', $the_date); $QUERY->setFetchMode(PDO::FETCH_ASSOC); //etc... } Now as you can see I echo $the_date and it shows, but when the query runs the named placeholder does not seem to work. When I take the WHERE clause out it works fine, so I know it's not an issue there. Any thoughts? Thanks
  8. Thanks, but I was thinking maybe in some kind of switch statement. Is that possible within a function?
  9. Hello All, I'm trying to put a lot of what I do into function calls now to make my code a bit tidier and consise. Take this simple if statement: if ($_GET['thedate'] =='') { $var = date('l, jS F, Y',time()); } else { $var = date('l, jS F, Y', strtotime($_GET['thedate'])); } How could I put this into a function and in some kind of switch statement? Thanks
  10. Hello All, Trying to write a log-in script using PDO. But have one question. With the below when I query the database and even run a print_r on $RES nothing is outputted? Any ideas? Thanks if (isset($_POST['Submit'])) { $email = $_POST['email']; $password = $_POST['password']; $QUERY = $dbc->prepare("SELECT email, password FROM tbl WHERE email = :email"); $QUERY->bindParam(':email', $email); $QUERY->execute(); $RES = $QUERY->fetchColumn(); echo "Email is:".$RES['email']; echo "PW is: ".$RES['password']; print_r($RES); }
  11. Say I have an array like so: $var = array ("One","Two","Three","Four","Five","Six","Seven","Eight"); print_r($var); foreach ($var as $newname) { //Do something } and the print_r is recorded as Array ( [0] => One [1] => Two [2] => Three [3] => Four [4] => Five [5] => Six [6] => Seven [7] => Eight ) How in that foreach loop can I run an if statement which says if the element is greater than [5], then do something? Thanks
  12. Hello All, I currently code in procedural PHP, but am now in a position to start learning OOP in PHP. Now i'm hoping to get my company to pay for a course in the UK (Ideally in the South East/London but if up North or elsewhere then that's fine). Anyone been to any training courses or heard of any that they can recommend and can post the links to? Thanks
  13. Thanks. Had another thought and added <? ob_start();?> To the first part of the script. That won't cause any issues will it and fine to do it this way?
  14. Actually, sorry how would I reorder the structure of my PHP to do this. I've misunderstood, do you mean re-order what i've done here?: $SQL = "SELECT headline FROM news WHERE news_id=".mysql_real_escape_string($_GET['news_id']); $result = mysql_query($SQL) OR die(mysql_error()); $num = mysql_num_rows($result); //** Check that the entry exists otherwise send to error page if ($num > 0) { $row = mysql_fetch_array($result); echo $headline = $row['headline']; } else { header("Location: error.php"); exit; }
  15. Thanks Yeah, I can do. Just how i've created the page. Thanks
  16. Thats the problem - any way around this apart from echoing a message saying there is no link or a meta redirect?
  17. Thanks but nope, no error messages. As for the value of $num I do find it out $num = mysql_num_rows($result)? Just wondered if there was anything obvious?
  18. Afternoon All. I wish to re-direct users to a 404 error page on my site if an article does not exist in my database. Here's my code: $SQL = "SELECT headline FROM news WHERE news_id=".mysql_real_escape_string($_GET['news_id']); $result = mysql_query($SQL) OR die(mysql_error()); $num = mysql_num_rows($result);//** Check that the entry exists otherwise send to error pageif ($num > 0) { $row = mysql_fetch_array($result); $headline = $row['headline'];} else { echo "Why is this printed? - I should be leaving this page?"; header("Location: error.php"); exit;} Now the wierd thing is that when I enter a news_id for a value that does not exist it prints the message Why is this printed? - I should be leaving this page? so it's actually going to the ELSE statement which is good, but surely it should not do this as I ask the page to re-direct? Thank you
  19. Thanks for the repies guys, but i'm confused, so I need to use: $id = strip_tags($_GET['id']); or $id = mysql_real_escape_string($_GET['id']);
  20. Hello People, Been reading up on these and trying to understand them more. Say I have a file called page.php?id=12345 and when users hit the page I run this code in the background: $id = $_GET['id']; $query = "UPDATE tbl SET live = '1' WHERE id = '".$id."'"; That page is not open to any attack right? Even though i'm using $_GET. Am I right in thinking that attacks only happen on online forms. So for example there is no way an attacker could somehow output all the data in my table tbl Thank yo
  21. Worked it out using foreach and keys.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.