Jump to content

Help Veiwing A Table


migroo

Recommended Posts

Okay I have bean just using xml files. But I think a data base would be much more practical for a log In system and a forum. Anyway I am trying to create a data base for my users I set up the data base but now I need to access it. Here is the code:

<?php$con = mysql_connect("localhost","darva11_tamuj","lag");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("darva11_sceptre", $con);$result = mysql_query("SELECT * FROM userids");echo "<table border='1'><tr><th>UserName</th><th>DisplayName</th></tr>";while($row = mysql_fetch_array($result))  {  echo "<tr>";  echo "<td>" . $row['UserName'] . "</td>";  echo "<td>" . $row['DisplayName'] . "</td>";  echo "</tr>";  }echo "</table>";mysql_close($con);?>

Here is a picture of the error I keep getting:warning%20pic.pngI am just trying to display all the information so I can make sure everything setup correctly.Thank you.

Link to comment
Share on other sites

Are you sure that the table is called "userids"? Maybe you forgot an uppercase letter somewhere or the name is written wrong.There's something wrong with your query:

SELECT * FROM userids

You could try echo mysql_error(); to see what it is.

Link to comment
Share on other sites

Okay where would I put the echo mysql_error();? Sorry I am rather new to php.

Link to comment
Share on other sites

Bummer I am still getting the same thing. Any other ideas?

Link to comment
Share on other sites

Of course you're getting the same thing, but there should be another message in the output explaining what the error is. mysql_error() isn't a solution, it's just supposed to help debugging.

Link to comment
Share on other sites

I understand that but I am getting the exact same think as in the same error message. I just double checked and it is just the same. Here is my code now:

<?php$con = mysql_connect("localhost","darva11_tamuj","lagfromh3ll");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("darva11_sceptre", $con);$result = mysql_query("SELECT * FROM userids");echo "<table border='1'><tr><th>UserName</th><th>DisplayName</th></tr>";while($row = mysql_fetch_array($result))if(!$result) echo mysql_error();  {  echo "<tr>";  echo "<td>" . $row['UserName'] . "</td>";  echo "<td>" . $row['DisplayName'] . "</td>";  echo "</tr>";  }echo "</table>";mysql_close($con);?>

Is that what I was supposed to do?

Link to comment
Share on other sites

Boy do I feel stupid! Okay I put it in the right place and I got a message it says the data table does not exist.Thank you very much.

Link to comment
Share on other sites

Okay I am getting this message:warning%20pic2.pngHere is the code I am using to create the data table:

<?php$con = mysql_connect("localhost","darva11_tamuj","lagfromh3ll");if (!$con)  {  die('Could not connect: ' . mysql_error());  }  // Create tablemysql_select_db("darva11_sceptre", $con);$sql = "CREATE TABLE userids(userID int NOT NULL AUTO_INCREMENT,PRIMARY KEY(userID),UserName varchar(15),DisplayName varchar(15),Password varchar(15),Posts int,Avatar image,Signature varchar(100),AcountDate varchar(15),ProfileUrl varchar(15),Age int,AccountType varchar(15),)";// Execute querymysql_query($sql,$con);if(!$sql) echo mysql_error();if($sql) echo "Data bace created!";mysql_close($con);?>

The code I am using for inserting data:

<?php$con = mysql_connect("localhost","darva11_tamuj","lagfromh3ll");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("darva11_sceptre", $con);mysql_query("INSERT INTO userids (UserName, DisplayName, Password, Posts, AcountDate, ProfileUrl, AccountType,)VALUES ('tamuj', 'The Duck', 'lagfromh3ll', '43', '1-19-2010', 'sceptreclan', 'Admin',)");mysql_query("INSERT INTO Persons (UserName, DisplayName, Password, Posts, AcountDate, ProfileUrl, AccountType,)VALUES ('auckmed', 'Dumbo', 'asdf', '41', '1-18-2010', 'sceptreclan2', 'grunt',)");mysql_close($con);?>

And the code I am using for displaying:

<?php$con = mysql_connect("localhost","darva11_tamuj","lagfromh3ll");if (!$con)  {  die('Could not connect: ' . mysql_error());  }if ($con) echo "Conected!!!";mysql_select_db("darva11_sceptre", $con);$result = mysql_query("SELECT * FROM userids");if(!$result) echo mysql_error();echo "<table border='1'><tr><th>UserName</th><th>DisplayName</th></tr>";while($row = mysql_fetch_array($result))  {  echo "<tr>";  echo "<td>" . $row['UserName'] . "</td>";  echo "<td>" . $row['DisplayName'] . "</td>";  echo "</tr>";  }echo "</table>";mysql_close($con);?>

The display code is giving off the only error message.The insert code doesn't have any messages and it is just a white screen.The table create code just says "connected!!!"I don't know where my error is but if you can help me I would be very great full.

Link to comment
Share on other sites

It's not a PHP error. It's a MySQL error. The table doesn't exist. Go look at your databases and made sure you spelled the table name correctly. It's case sensitive.

Link to comment
Share on other sites

I have my website hosted through hostgator.com and they use cpanl. I created the database with cpanel. When I named my database they put my user name in front of it do you know if that should be included? An example would be if my user name was Jim and I labeled my database users the result would be Jim_users. Also how do I view my database? Is there a way to view it like you do a xml file or anything else?

Link to comment
Share on other sites

If your Cpanel has PHPmyAdmin you can use that to look at the databases.I don't know how hostgator puts the usernames, you'll have to ask them if you want to be sure.

Link to comment
Share on other sites

Thank you! I found the php admin and It is saying that there are no tables so I am going to mess around with that for a while.THANK YOU!

Link to comment
Share on other sites

Okay I found the phpadmin in cpanel and it has bean extremely useful. I have created a working sign up page. Once a person has signed up I want them to log in.I have a forum and that asks for the user name and password. It then sends the information to login.php which is supposed to validate it and set 2 cookies if the information is correct. Here is my code:

<?php	$user = $_POST['username'];	$pass = $_POST['password'];		 //conection information			  $con = mysql_connect("localhost","darva11_tamuj","lagfromh3ll");			 if (!$con)			   {				die('Could not connect: ' . mysql_error());			   }			 		  //selecting databace			 mysql_select_db("darva11_sceptre", $con);		  //Check for propper password			 $result = "SELECT * FROM users WHERE UserName = '$user' AND Password='$pass'";			  $result2 = mysql_query($result);			 $num2 = mysql_num_rows($result2);			 if ($num2 == 1)			 {			   $url=mysql_query("SELECT userID FROM users WHERE UserName='$user'");			   $get_url=mysql_fetch_array($url);				setcookie("user", $user, time()+21600);				setcookie("password", $pass, time()+21600);				echo '<a href="'.$get_url.'">Profile</a>';			   			 }else{			  echo "Sorry wrong Username or password!";			 			 }			 			 			    ?>

It just skips to the else statement and says "Sorry wrong Username or password!"I have double triple checked to make sure I am putting in the write username and password and that I am using the proper Database column names.I think my error is in this area:

$result = "SELECT * FROM users WHERE UserName = '$user' AND Password='$pass'";			  $result2 = mysql_query($result);			 $num2 = mysql_num_rows($result2);			 if ($num2 == 1)

I have bean looking all over and rewriting this code for the last 3 hours so if anyone can tell me what I did wrong I would be very glad.Also Sorry about double posting this thread. It came up with a error message so I thought the thread had not bean created so I went of and posted it again.

Link to comment
Share on other sites

First of all, putting the user's password in a cookie is as good as printing it out on the page for everybody to see. Do't put sensitive information in cookies.With that cleared up, it's obvious that $num2 is not equal to 1.Go print out $num2 to see what it's getting. If it isn't getting anything, you'll have to see if that user actually exists in the database.

Link to comment
Share on other sites

Okay I will check that. Also how should I store usernames and passwords?

Link to comment
Share on other sites

You shouldn't need to store them. If you want to make sure a user is logged in, just leave a "logged_in" session variable that you check when you open the page.

Link to comment
Share on other sites

Okay I will go and read up on sessions in the tutorials. You are right $num is returning 0 and I have no idea why I have double checked my database using PHPAdmin and I am using the correct column names.Oh well I will keep plugging away and eventually I will find what I did wrong. Its probably something really silly lol.

Link to comment
Share on other sites

I figured it out my code was good I just omitted the method="post" attribute in my LogIn forum. Oh well live and learn.

Link to comment
Share on other sites

Umm... Do I want to put my session_start() before the DTD or after but before the <HTML> tag? The tutorial says it must go before the <HTML> tag but it never says anything about DTD;Thanks

Link to comment
Share on other sites

Yep, that was my problem. I was getting errors and it was because I had some output before the session. It was in an if statement but I got rid of it and that seems to have fixed the problem.Thanks!

Link to comment
Share on other sites

Okay I have a forum that sends A username and password:

<form action="/login.php" method="post">	 <table>	 <tr>	 <th colspan="2"> <img border="0" src="/images/welcome1.png" width="230" height="75" />  </th>	 </tr>	 <tr>	 <td><input class="grey" type="text" name="username" value="Username"/></td>	 <th rowspan="2"> <a href="/register.html"> <img border="0" src="/images/join.png" width="60" height="120" /> </a> </th>	 </tr>	 <tr>	 <td><input class="grey" type="password" name="password" value="Password" /></td>	 </tr>	 <tr>	 <th colspan="2"> <input type="image" src="/images/log1.png" width="230" height="75"/>  </th>	 </tr>	 </table>	</form>

Then I have another page called login.php:

 <?php $user = $_POST['username']; $pass = $_POST['password'];?>   <?php			 //conection information			  $con = mysql_connect("localhost","herelo","lagggg");		  //selecting databace			 mysql_select_db("sceptre", $con);		  //Check for propper password			 $result = mysql_query("SELECT * FROM users WHERE UserName='$user'");			 $row = mysql_fetch_array($result);			 if($row['UserName'] == $user && $row['Password'] == $pass)				  {				  $good = "Welcome ".$row['DisplayName'].". You are now Securly Logged in. <a href='/profiles/".$row['ProfileUrl']."/index.php'>OK</a>";				  $_SESSION['username']=$user;				  }			 			 else				$bad="Sorry, Your username or password is wrong!";			  			 			 			 			    ?>My DTD, css, and some tables for positioning.<?php	echo $good;	echo $bad;	echo $_SESSION['username'];?>

Then I have a page that is suposed to get my session:

<?php $logkey=$_SESSION['username']; //conection information			  $con = mysql_connect(localhost","herelo","lagggg");		  //selecting databace			 mysql_select_db("sceptre", $con);		 			 $result = mysql_query("SELECT * FROM users WHERE UserName='$logkey'");			 $row = mysql_fetch_array($result);?>My DTD, css, some html.  <?php   if($logkey=="airjeteric")   {	echo '	  <br />	   <div class="othernav">		<div class="othernav2">		 <table border="0">		  <tr>		   <td> <p>Welcome: '.$logkey.' |</p></td> <td><a href="">LogOut</a></td>		  </tr>		 </table>		</div>	   </div>	  <br />	  <img class="welcome" src="images/profile.png" width="550" height="100" alt="SCEPTRE Profile"/>	 <div class="log">	   <div class="logbut">	   </div>	 </div>	 '; } else   echo 'Sorry you are not logged in! <a href="http://sceptreclan.com/index.html/"> Login </a>'; ?>

The only problem is in the end I am getting the "Sorry you are not logged in! Login" I have no idea what I have done wrong but I think it has to do with my sessions.Thank you all so much for all the help you have bean giving me.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...