Jump to content

[SOLVED] Page shows up blank


zenix

Recommended Posts

Hey, I am learning php right now. I have been attempting to make a simple database that would store just 2 fields of information. I thought this would be a place to start and it'd be something I could buil upon later as I learn more. Anyway, I have coded and re-coded this thing and whenever I attempt to view it in my browser I get a blank screen in Firefox and a 500 server error in IE. If someone would take a look at what I have and offer a suggestion or 2 I would REALLY appreciate it. The code is below, thank you in advance!

 

 

This code is for viewing the table contents:

<html>
<head>
<title>View Database</title>
</head>

<body>

<?php

//connect and select
if($dbc = mysql_connect('localhost','root'));
{
if(!mysql_connect('database1'))
{
	die('<p>Could not select to mysql because <b>' . mysql_error() . '</p></b>');
}
}
else
{
die('<p>Could not connect to mysql because:<b>' . mysql_error() . '</p></b>');
}
//define query
$query = 'SELECT * FROM db_entries ORDER BY date_entered DESC';

if($r = mysql_query($query))
{
//retrieve and print records
while($row = mysql_fetch_array($r))
{
	print "<p><h3>{$row['title']}</h3>
	{$row['entry']}<br/>
	<a href=\"edit_entry.php?id={$row['db_id']}\">Edit</a>
	<a href=\"delete_entry.php?id={$row['db_id']}\">Delete</a></p><hr/>\n";
}
}
else
{
//Query didn't run
die('<p>Could not retrieve data because <b>' . mysql_error() . "the query was $query</b></p>");
}
mysql_close();
?>

															  




</body>
</html>

 

 

 

This coed is for adding the data:

<html>
<head>
<title>Entry 2</title>
</head>

<body>
<?php

//Didn't show anything
//ini_set('display_errors', 1);
//error_reporting(E_ALL);

if(isset($_POST['submit']))
{
   //connect and select
   if($dbc = mysql_connect ('localhost', 'root'))
   {
      if(!@mysql_select_db ('myblog'))
      {
         dir('<p>Could not select database because <b>' . mysql_error() . '</b></p>');
      }
   }
   else
   {
      print "<p>Could not connect to database because <b>" . mysql_error() . "</b></p>";
   }
//define query
$query = "INSERT INTO blog_entries
(bog_id, title, entry, date_entered) VALUES(0, '{$_POST['title']}', '{$POST['entry']}', NOW())";

//execute query
if(@mysql_query($query))
{
   print '<p>The blog entry has been added</p>';
}
else
{
   "<p>Could not add entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}
}
mysql_close();
?>

<form action ="add_entry.php" method ="post">
<p>Entry title: <input type ="text" name ="title" size ="40" maxsize ="100"/></p>
<p>Entry text: <textarea name="entry" cols ="40" rows ="5"/></textarea></p>
<br/>
<input type ="submit" name ="submit" value ="Add my entry!"/>
</form>
</body>
</html>

 

 

This code is for creating the DB. I know not very efficient, but I am just learning now.

 

<html>
<head>
<title>Database 10</title>
</head>

<body>

<?php
//attempt to connect to mysql
if($dbc = mysql_connect('localhost', 'root'))
{
print '<p>Successfully connected!</p>';

if(mysql_query('CREATE DATABASE database1'))
{
	print '<p>The database has been created!</p>';
}
else
{
	die('<p>Could not create database because: <b>' . mysql_error() . '</b></p>');
}
if(mysql_select_db('database1'))
{
	print '<p>The database has been selected!</p>';
}
else
{
	die('The database could not be slected because: <b>' . mysql_error() . "</b>The query being run was<b> $query</b>");
}
mysql_close();
}
else
{
die('<p> could not connect to mysql because<b>' . mysql_error . '</p></b>');
}
?>
</body
</html>

				   

																	  

				   



</body>
</html>

 

AND creating the table

 

<html>
<head>
<title>Create Table</title>
</head>

<body>

<?php
//connect and select
if($dbc = @mysql_connect('localhost', 'root'))
{
if (!@mysql_select_db ('database1'))
{
	die('<p>Could not select the database because<b>' . mysql_error() . '</b></p>');
}

}
else
{
die('<p>Could not connect to database because: <b>' . mysql_error() . '</b></p>');
}
//define query
$query = 'CREATE TABLE db_entries
(db_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(100) NOT NULL,
  entry TEXT NOT NULL, date_entered DATETIME NOT NULL)';

//run query
if(mysql_query($query))
{
 print '<p>The table has been created!</p>';
}
else
{
 die('<p>The table was not created because <b>.' . mysql_error() . '</b></p>
															   <p> the query being run was:' . $query . '</p>');
}
mysql_close();
?>
																	  



</body>
</html>

Link to comment
Share on other sites

That's a lot of code. Too much for these weary eyes.

 

Are you running this on a local box or remote server? You should set error reporting to E_ALL and display_errors = on so you have a better idea of what is happening.

Link to comment
Share on other sites

In your first code excerpt, "This code is for viewing the table contents:" you have a syntax error:

 

if($dbc = mysql_connect('localhost','root'));

 

In case you can't see it, there is a semi-colon after your IF statement.

Link to comment
Share on other sites

Someone already told him that, apparently he did not want php's help in finding basic errors.

 

Yes, I did insert the ini_set('display_errors' 1 );

                          error_reporting = (E_ALL);

sorry it wasn't included here though. Someone told me that it doesn't need to be on every page so I omitted it. More than likely would have been better if I had just left it or even commented it out. Sorry. I also apologize for there having been so much code. I will try to pin point it a little btter in the future. I looked at the post this morning, yeah...it's too much with so many snippets.

Link to comment
Share on other sites

Could someone please let me know how to mark this resolved? I'm going to do more troubleshooting and studying. Probably learn better that way. Thank Maq for pointing out my typo. I appreciate all the comments here. Thanks a lot!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.