-
Posts
65 -
Joined
-
Last visited
Everything posted by CloudBreaker
-
Trouble converting user id into variable
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
Thanks Ch0cu3r. -
Trouble converting user id into variable
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
I did call session_start() at the top of the index.php page...I'm just not showing it. You can see in on my very first post before my previous edit. thanks. -
Trouble converting user id into variable
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
Thanks mac_gyver. I'm still having some issues...I've set the session variables that I need, but for some reason, on the next page the only variable that is carried through is the 'user_loginName' variable. When the login in directed to main.php line 43 successfully echos the "user_loginName" while line 15 and 16 returns an "undefined index" error. I don't understand why the "id" and the "user_firstName" variables are not echoing or not passing through to page for that matter. thanks CB index.php (the log-in page) <?php //Validate log-in and password if(isset($_POST['login'])) { $user_loginName = mysqli_real_escape_string($conn,$_POST['user_loginName']); $user_pass = mysqli_real_escape_string($conn,$_POST['user_pass']); $id = ($conn['id']); $user_firstName = ($conn['user_firstName']); $sel = "select * from hsa_users where user_loginName='$user_loginName' AND user_pass='$user_pass' AND id='$id' AND user_firstName='$user_firstName'"; $run = mysqli_query($conn, $sel); $check = mysqli_num_rows($run); if($check==0){ echo "<script>alert('Incorrect Log-in or Password. Try again.')</script>"; exit(); } else { $_SESSION['user_loginName']=$user_loginName; $_SESSION['id']=$id; $_SESSION['user_firstName']=$user_firstName; echo "<script>window.open('main.php','_self')</script>"; } } ?> main.php <!DOCTYPE html> <?php session_start(); if(!$_SESSION['user_loginName']){ header("location: index.php"); } else { ?> <?php // Echo session variables that were set on previous page echo " The ID is " . $_SESSION["id"] . ".<br>"; echo "The First Name is " . $_SESSION["user_firstName"] . "."; ?> <!--Project Hub main. Listed projects are dependant upon login permissions--> <html> <head> <title>Project Hub Projects</title> <link href="hsastyle.css" rel="stylesheet"> </head> <body> <div id="main_container"> <p><em>version 1.0 beta</em></p> <div id="banner"> <div id="logo"> <img src="images/hsa-logo.jpg" alt=HSA logo> </div> <H2><em>Project Hub</em></h2> <h5><a href="logout.php">Log Out</a></h5> <H6>Welcome <?php echo $_SESSION['user_loginName'];?> </div> <!--End Banner--> -
My goal here is to list projects by user once the user is logged in. I have the back end in PHPAdmin set up and the query works fine. I've sort of run into a wall due to my inexperience when it comes to making the query work in the code. I'm thinking it's somehow tied to setting variables relating to the SESSION VARIABLE for the user who has just logged in. You'll see my log-in page first, and in the next block I've posted my page which will list the projects for the user who has logged in. The User page is obviously not finished yet because I know the variables I need will go into the query on line 58, in addition this is my first attempt at relating tables in a query. Thanks, CB <!DOCTYPE html> <?php session_start(); $conn = mysqli_connect("localhost","root","","hsa_project_hub"); ?> <!--Project Hub index--> <html> <head> <title>Project Hub Login</title> <!-- Modernizr allows HTML5 elements to work in older browsers: http://modernizr.com/ --> <script src="js/modernizr.js"></script> </head> <style> img { display: block; margin-left: auto; margin-right: auto; box-shadow: 5px 5px 5px #888888; Float: left position: relative; } #container { background: #F0F0F0; width: 300px; display: block; margin-left: auto; margin-right: auto; border-style: solid; border-width: 1px; } h4 { font-family: "Avant Garde",Avantgarde,"Century Gothic",CenturyGothic,AppleGothic,sans-serif; font-size: 30px; color: #8F0000; float: left; position: relative; left: 70px; font-style:italic; } #copy_right { font-size: 10px; text-align: center; } </style> <body> <div id="container"> <img src="images/hsa-logo.jpg" align="middle" alt=HSA logo> <h4>Project Hub</h4> <!--Start of form--> <form action="index.php" method="post"> <table align="center" bgcolor="#F0F0F0" width="300" > <tr align="center"> </tr> <tr> <td align="right"><strong>Log-in:</strong></td> <td> <input type="text" name="user_loginName" placeholder="Enter your Log-in" required="required"/> </td> </tr> <tr> <td align="right"><strong>Password:</strong></td> <td> <input type="password" name="user_pass" placeholder="Enter your pass"required="required"/> </td> </tr> <tr align="center"> <td colspan="6"> <input type="submit" name="login" value="Login"/> </td> </tr> </table> </form> </div><!--End of Container--> <p id="copy_right">Heitkamp Swift Architects © 2015</p> <?php //Validate log-in and password if(isset($_POST['login'])) { $user_loginName = mysqli_real_escape_string($conn,$_POST['user_loginName']); $user_pass = mysqli_real_escape_string($conn,$_POST['user_pass']); $sel = "select * from hsa_users where user_loginName='$user_loginName' AND user_pass='$user_pass'"; $run = mysqli_query($conn, $sel); $check = mysqli_num_rows($run); if($check==0){ echo "<script>alert('Incorrect Log-in or Password. Try again.')</script>"; exit(); } else { $_SESSION['user_loginName']=$user_loginName; echo "<script>window.open('main.php','_self')</script>"; } } ?> </body> </html> User Page <!DOCTYPE html> <?php session_start(); if(!$_SESSION['user_loginName']){ header("location: index.php"); } else { ?> <!--Project Hub main. Listed projects are dependant upon login permissions--> <html> <head> <title>Project Hub Projects</title> <link href="hsastyle.css" rel="stylesheet"> </head> <body> <div id="main_container"> <p><em>version 1.0 beta</em></p> <div id="banner"> <div id="logo"> <img src="images/hsa-logo.jpg" alt=HSA logo> </div> <H2><em>Project Hub</em></h2> <h5><a href="logout.php">Log Out</a></h5> </div> <!--End Banner--> <h1>PROJECTS:</h1><br> <!--List of projects by user --> <table align="center"> <tr align="center"> <th>Project Name</th> <th>Project (HSA) No.</th> <th>RFI's</th> <th>Submittals</th> </tr> <?php //Getting projects from user session variable $sel = "select * from hsa_users,projects,member_project WHERE projects.id=member_project.project_id AND member_project.user_id=2 AND hsa_users.id=2"; ?> </table> </div> <!--End main container--> <div id="copy_right" <p id="copy_right">Heitkamp Swift Architects © 2015</p> </div> <?php ?> </body> </html> <?php } ?>
-
</body> </html> <?php } ?>
-
That is perfect...what I need to get started...thanks.
-
I have a table of users, a table of projects and another table with information that pertains back to their related projects along with associated ID's. I want certain users to only SEE and be able to add and edit information to the projects they belong to. I'd want the user to log in and then only be able to see and edit his or her projects in other words. I have already successfully coded an admin panel where I can create the projects and related tables. In your opinion, what is the most efficient way to do this? thank you.
-
That was it...thanks. Sometime you just have to know when to step away from the computer.
-
I'm trying to get my log-in script to work. I'm getting two undefined variables for $user_loginName and $user_pass. Plus, even with the correct log and pass I can't log in. My database connection is good and I didn't misspell the my user table. Can some see something that I'm unable to see. Is there an easy way to script this?...if so, got a link? Thanks, CB <!DOCTYPE html> <?php session_start(); $conn = mysqli_connect("localhost","root","","hsa_project_hub"); ?> <!--Project Hub index--> <html> <head> <title>Project Hub Login</title> <!-- Modernizr allows HTML5 elements to work in older browsers: http://modernizr.com/ --> <script src="js/modernizr.js"></script> </head> <style> img { display: block; margin-left: auto; margin-right: auto; box-shadow: 5px 5px 5px #888888; Float: left position: relative; } #container { background: #F0F0F0; width: 300px; display: block; margin-left: auto; margin-right: auto; border-style: solid; border-width: 1px; } h4 { font-family: "Avant Garde",Avantgarde,"Century Gothic",CenturyGothic,AppleGothic,sans-serif; font-size: 30px; color: #8F0000; float: left; position: relative; left: 70px; font-style:italic; } #copy_right { font-size: 12px; text-align: center; } </style> <body> <div id="container"> <img src="images/hsa-logo.jpg" align="middle" alt=HSA logo> <h4>Project Hub</h4> <!--Start of form--> <form action="index.php" method="post"> <table align="center" bgcolor="#F0F0F0" width="300" > <tr align="center"> </tr> <tr> <td align="right"><strong>Log-in:</strong></td> <td> <input type="text" name="user_loginName" placeholder="Enter your Log-in" required="required"/> </td> </tr> <tr> <td align="right"><strong>Password:</strong></td> <td> <input type="password" name="user_pass" placeholder="Enter your pass"required="required"/> </td> </tr> <tr align="center"> <td colspan="6"> <input type="submit" name="login" value="Login"/> </td> </tr> </table> </form> </div><!--End of Container--> <p id="copy_right">Heitkamp Swift Architects © 2015</p> <?php //Validate log-in and password if(isset($_POST['login'])) { $user_loginName = mysqli_real_escape_string($conn,$_post['user_loginName']); $user_pass = mysqli_real_escape_string($conn,$_post['user_pass']); $sel = "select * from hsa_users where user_loginName='$user_loginName' AND user_pass='$user_pass'"; $run = mysqli_query($conn, $sel); $check = mysqli_num_rows($run); if($check==0){ echo "<script>alert('Incorrect Log-in or Password. Try again.')</script>"; exit(); } else { $_SESSION['user_loginName']=$user_loginName; echo "<script>window.open('main.php','_self')</script>"; } } ?> </body> </html>
-
Thank you very much for the insight. I can see how it would become a management nightmare now. With one database, I can see that it's about linking tables and the corresponding data together. Time for redo. Good thing didn't get too far.
-
mikosiko, I'm using it to create a database for each project that is created. That particular project database will include tables such as an RFI table. You can have hundreds of RFIs in a project with associated files "pdf's" that will need to be uploaded to that particular RFI (an RFI is a Request for Information from a contractor). You'll also have a good number of submittals that will be associated with that project which will also require it's own table and associated file uploads with each submittal. Each project (database) can grow to be huge, and having one database which contains say 50 projects can disrupt workflow if that one database were to become corrupt. . There would also be contractors from different companies who should not be able to view or access the projects they are not working on. I thought about having one database and how I'd go about setting that up, for instance I guess you'd break it down into tables....ex project1_rfis, project1_submittals. However, each rfi will have sub-parameters..."rfi-sender" "date submitted" "field question" etc. Can you imagine how enormous those tables would become over time. Even if I decided to create tables for each separate rfi per project, I'd have hundreds of tables to sift through if something went wrong. If I create a database for each project, it allows me to create one table for all the rfi's in that project along with all the necessary attributes associated with each rfi created. I'm green at this, so if my logic is off... Please advise thanks
-
I appreciate the help from all of you. This Milestone has been accomplished. No errors and it works clean. I'm a architect by trade (the kind that designs buildings) so all of this twists my mind in a good way. Again, thanks. Here's the working code: <!DOCTYPE HTML> <html> <head> <title>Create database</title> </head> <body> <form action="createdatabase_tables.php" method="post"> <table align="center" bgcolor="gray" width="500"> <tr align="center"> <td colspan="6"> <h2>New Users Register Here</h2></td> </tr> <tr> <td align="right"><strong>Database Name:</strong></td> <td> <input type="text" name="db_name" placeholder="Enter the Project Name" /> </td> </tr> <tr align="center"> <td colspan="6"> <input type="submit" name="create_db" value="Create Project"/> </td> </tr> </form> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "test"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } if(isset($_POST['create_db'])){ $db_name = $_POST['db_name']; $sql = "CREATE DATABASE $db_name"; if ($conn->query($sql) === TRUE) { echo "Project $db_name created successfully"; } else { echo "Error creating database: " . $conn->error; } } // Selecting newly created database "db_name" if(isset($_POST['create_db'])){ $db_name = $_POST['db_name']; mysqli_select_db($conn,$db_name); // Creating tables in new database $sql = "CREATE TABLE rfis ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, rfi_no VARCHAR(30) NOT NULL, rfi_posted date, rfi_needed date )"; if ($conn->query($sql) === TRUE) { echo "Table created successfully"; } else { echo "Error creating table: " . $conn->error; } } $conn->close(); ?> </body> </html>
-
Thanx AM. I'm not sure what you mean by "fully-qualify table names." As you can see, I'm using a form to get the desired name of the new db from the user. Once submitted the new db is successfully created, then the connection is closed. A new connection is created to the new db that was just created, then the syntax is entered in order to create the desired tables, which should work. I'm looking into the select_db. thnx
-
I'm fairly new to PHP. I'm trying to create a database while simultaneaously including tables. My syntax seems correct, however while the database is successfully created the tables are not. See below. Thank you... <!DOCTYPE HTML> <html> <head> <title>Create database</title> </head> <body> <form action="createdatabase.php" method="post"> <table align="center" bgcolor="gray" width="500"> <tr align="center"> <td colspan="6"> <h2>New Users Register Here</h2></td> </tr> <tr> <td align="right"><strong>Database Name:</strong></td> <td> <input type="text" name="db_name" placeholder="Enter the Project Name" /> </td> </tr> <tr align="center"> <td colspan="6"> <input type="submit" name="create_db" value="Create Project"/> </td> </tr> </form> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "test"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } if(isset($_POST['create_db'])){ $db_name = $_POST['db_name']; $sql = "CREATE DATABASE $db_name"; if ($conn->query($sql) === TRUE) { echo "Project created successfully"; } else { echo "Error creating database: " . $conn->error; } } $conn->close(); // Create new connection $conn = new mysqli($servername, $username, $password, $db_name); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // sql to create table $sql = "CREATE TABLE rfis( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, rfi_no VARCHAR(30) NOT NULL, rfi_posted date, rfi_needed date )"; // sql to create table $sql = "CREATE TABLE rfis ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, rfi_no VARCHAR(30) NOT NULL, rfi_posted date, rfi_needed date )"; if ($conn->query($sql) === TRUE) { echo "Table created successfully"; } else { echo "Error creating table: " . $conn->error; } $conn->close(); ?> </body> </html>