Viewed   45 times

I have this little code, which in fact is a login script which check if the register is on, and show it after the login button:

   <?php
include("../inc/db.php"); 
if(isset($_POST['user']) && isset($_POST['pass']))
{
    $password = $_POST['pass'];
    $username = $_POST['user'];

    $sql = "SELECT * FROM `users` WHERE `user` = '".$username."' AND `password` = '".$password."'";
    $rez = $pdo->query($sql);   
    if($rez->fetchColumn()  > 0)
    {
        ...

    }
    else {echo '<p align="center">...</p>';}
    }
    else { echo '<p align="center">...</p>'; }
    }
    ?>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form name="form1" method="post" action="login">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td colspan="3"><strong>Member Login </strong></td>
    </tr>
    <tr>
    <td width="78">Username</td>
    <td width="6">:</td>
    <td width="294"><input name="user" type="text" id="user"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="pass" type="password" id="pass"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Login"></td>
    </tr>
    <?php $sql = "SELECT setare FROM setari WHERE nume_setare = 'OPEN_REG'";
    $openreg = $pdo->query($sql)->fetch();
    if($openreg['setare'] == 1)
    {
     ?>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><a href="register">Inregistrare</a></td>
    </tr><?php } ?>
    </table>
    </td>
    </form>
    </tr>
    </table>

My problem is this line:

include("../inc/db.php"); Warning: include(E:/wamp/www//inc/db.php): failed to open stream: No such file or directory in E:wampwwwproiect1-testscriptslogin.php on line 3 Warning: include(): Failed opening '../inc/db.php' for inclusion (include_path='.;C:phppear') in E:wampwwwproiect1-testscriptslogin.php on line 3

and i can't figure it out where i'm wrong. The path is correct, and if i hit the login button, it works.If i hit login button with an inccorect combination of username and password, the warning disappear. However, it doesn't include that when i open it for the first time. This login file is included in the index of the site.

 Answers

5

Your path to that file is obviously incorrect. This commonly happens when you use a relative path to a file and then start placing files in different directories. You should use the full system path to the file to avoid this issue:

include("/path/from/root/to/inc/db.php"); 

A common thing to do is define a variable or constant that defines the root path to your web files. That way if it ever changes (i.e. you change hosts) you only need to change it in one place.

In your config file:

define('ROOT_PATH', '/path/from/root/to/');

In your PHP files;

include(ROOT_PATH . "inc/db.php"); 
Thursday, August 18, 2022
5

It's relative to the main script, in this case A.php. Remember that include() just inserts code into the currently running script.

That is, does it matter which file the include is called from

No.

If you want to make it matter, and do an include relative to B.php, use the __FILE__ constant (or __DIR__ since PHP 5.2 IIRC) which will always point to the literal current file that the line of code is located in.

include(dirname(__FILE__)."/C.PHP");
Sunday, December 4, 2022
2

I would say just build it yourself. You can set it up like this:

$query = "INSERT INTO x (a,b,c) VALUES ";
foreach ($arr as $item) {
  $query .= "('".$item[0]."','".$item[1]."','".$item[2]."'),";
}
$query = rtrim($query,",");//remove the extra comma
//execute query

Don't forget to escape quotes if it's necessary.

Also, be careful that there's not too much data being sent at once. You may have to execute it in chunks instead of all at once.

Saturday, November 5, 2022
1

PHP's a bit odd in how it looks for files. If you include a file whose name starts with a slash or a dot, PHP ignores the include_path entirely. If a dot, it assumes the name is relative to the script that kicked off everything (ie: the one the web server decided to run).

If you want to specify a path relative to the included script and not the startup one, what you really want is an absolute include that specifies the full name of the file. That's easy to do with the __FILE__ and __DIR__ constants, which reflect the name and directory of the currently running script.

include __DIR__ . '/../dirB/c.php';

If you like, you can also set the include_path config setting to include the root of the app, and just specify all filenames relative to that.

Thursday, December 1, 2022
2

The function you're looking for is find_in_set:

 select * from ... where find_in_set($word, pets)

for multi-word queries you'll need to test each word and AND (or OR) the tests:

  where find_in_set($word1, pets) AND find_in_set($word2, pets) etc 
Wednesday, August 17, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :