Solved

Simple PHP page that saves a few variables to a database

Posted on 2011-03-15
14
289 Views
Last Modified: 2013-12-13
Hi,

When I load ee-demo.php, I get a white screen. Something is wrong with my syntax or database structure.

I have 2 files attached. Can you please review them and make sure the syntax is correct.

I have a PHP file which has the code that connects to the database.
I have a SQL file which is the table of the database I am trying to work with.

Thank you!

ee-demo.php configuration.sql
0
Comment
Question by:Computer Guy
  • 6
  • 3
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

by:pius_babbun
Comment Utility
You syntax looks fine . You can have all you PHP Code under after the HTML tags.

And Please check the Database connection and the table name before you proceed. Hope it helps . If you require any further detail we shall discuss.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Config</title>	

    
    <style type="text/css">
<!--
.Red_Italics {
	color: #CC0000;
	font-style: italic;
}
.Entery_Fields {
	font-family:Arial, Helvetica, sans-serif;
	color: #000066;
}
.Bold_Heading {
	font-weight:bold;
}
-->
</style>
</head>
<body> 

<form action="#" method="post">
<label for="version">Version:</label><br />
<input type="textbox" name="version" value="<?php echo"$row[version]";?>" class="Entery_Fields"/><br /><br />

<label for="helpdesk">Helpdesk:</label><br />
<select name="helpdesk" class="Entery_Fields" value="<?php echo"$row[helpdesk]";?>"/><br /><br />
<option value="0">Offline</option>
<option value="1">Online</option>
<option value="2">Maintenance Mode</option>
</select><br /><br />

<label for="shopping_cart">Shopping Cart:</label><br />
<select name="shopping_cart" class="Entery_Fields" value="<?php echo"$row[shopping_cart]";?>"/><br /><br />
<option value="0">Offline</option>
<option value="1">Online</option>
<option value="2">Maintenance Mode</option>
</select><br /><br />

<label for="backup_server">Backup Server:</label><br />
<select name="backup_server" class="Entery_Fields" value="<?php echo"$row[backup_server]";?>"/><br /><br />
<option value="0">Offline</option>
<option value="1">Online</option>
<option value="2">Maintenance Mode</option>
</select><br /><br />

<label for="notes">Notes:</label><br />
<textarea name="notes" cols="100" rows="10" class="Entery_Fields" value="<?php echo"$row[notes]";?>"/></textarea><br /><br /><br />


<input type="submit" value="update" />
</form>

</body>
</html>
<?php 
$host = "localhost";
$username = "";
$password = "";
$database = "";
$table = "configuration";

$link = mysql_connect($host, $username, $password) or die(mysql_error());
$db = mysql_select_db($database, $link) or die(mysql_error());

if(!empty($_POST)){
 $version = $_POST[OS_version];
 $status = $_POST[server_status];
 $helpdesk = $_POST[helpdesk];
 $shopping_cart = $_POST[shopping_cart];
 $backup_server = $_POST[backup_server];
 $notes = $_POST[notes];
 

 $query = "update $table set OS_version = '$version', server_status = '$status', helpdesk = '$helpdesk', shopping_cart = '$shopping_cart', backup_server = '$backup_server', notes = '$notes'";
 $result = mysql_query($query) or die(mysql_error());

 echo"Record updated!";
}

$query = "select * from $table";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
?>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
the database name id seg_Config, the ee-demo file contains a table named configuration, but the sql file you provided creates pw_config table - either you change the file ee-demo to connect to pw_config table or you have to change a table name in the database from pw_config to configuration.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
This is the part of the ee-demo you need to change also:

$query = "select * from $table";
$result = mysql_query($query) or die ("eee");
$num = mysql_num_rows($result);
if($num > 0){
    $row = mysql_fetch_array($result) or die("error");
}

?>

<form action="#" method="post">
<label for="version">Version:</label><br />
<input type="textbox" name="version" value="<?php echo"$row[OS_version]";?>" class="Entery_Fields"/><br /><br />

<label for="helpdesk">Helpdesk:</label><br />
<select name="helpdesk" class="Entery_Fields" /><br /><br />
<option value="0" <?php if($row[helpdesk] == 0) echo"selected=\"selected\"";?> >Offline</option>
<option value="1" <?php if($row[helpdesk] == 1) echo"selected=\"selected\"";?>>Online</option>
<option value="2" <?php if($row[helpdesk] == 2) echo"selected=\"selected\"";?>>Maintenance Mode</option>
</select><br /><br />

<label for="shopping_cart">Shopping Cart:</label><br />
<select name="shopping_cart" class="Entery_Fields" /><br /><br />
<option value="0" <?php if($row[shopping_cart] == 0) echo"selected=\"selected\"";?>>Offline</option>
<option value="1" <?php if($row[shopping_cart] == 0) echo"selected=\"selected\"";?>>Online</option>
<option value="2" <?php if($row[shopping_cart] == 0) echo"selected=\"selected\"";?>>Maintenance Mode</option>
</select><br /><br />

<label for="backup_server">Backup Server:</label><br />
<select name="backup_server" class="Entery_Fields" /><br /><br />
<option value="0" <?php if($row[backup_server] == 0) echo"selected=\"selected\"";?>>Offline</option>
<option value="1" <?php if($row[backup_server] == 0) echo"selected=\"selected\"";?>>Online</option>
<option value="2" <?php if($row[backup_server] == 0) echo"selected=\"selected\"";?>>Maintenance Mode</option>
</select><br /><br />

<label for="notes">Notes:</label><br />
<textarea name="notes" cols="100" rows="10" class="Entery_Fields" /><?php echo"$row[notes]";?></textarea><br /><br /><br />


<input type="submit" value="update" />
</form>

</body>
</html>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
The select fields have to be checked like the above way, you cannot set select field value like you did.
0
 
LVL 6

Expert Comment

by:blueghozt
Comment Utility
many reasons why this won't be working for you:

1. a few errors in your PHP and mark-up, mainly not using quotes for indices in the $_POST and $row[] arrays - I have fixed these in the attached
2. also you names your textarea 'version' but were looking for $_POST['OS_version']
3. If your database is empty then you need to do an INSERT query and not an UPDATE query
4. If you are doing an UPDATE query you need to specify which row you are updating e.g. '.... WHERE id=1'
5. Your select query to populate the form does not specify which row to extract from the table, again you need to specify the id or some other unique identifier
6. You are missing a primary key on your table
7. You are specifying the wrong table in your script - the sql creates 'pw_config' your script tries to access 'configuration'

I have correct all of these errors and wrapped each input with mysql_real_escape_string() which ensures no nasties are posted and provide a means for creating a new record if no existing id has been posted by the form.

As the script stands also it will not connect to the database without the username, password and database name being specified, presumably you omitted these for security?

Also how are creating the database are you importing the sql file using phpMyAdmin's import method?

Hope this all helps.
configuration.sql
ee-demo.php
0
 
LVL 3

Author Comment

by:Computer Guy
Comment Utility
My Connection is really, I just changed some things for the upload, since this is "public"

$host = "localhost";
$username = "";
$password = "";
$database = "seg_Config";
$table = "pw_config";

With the way it is now, the HTML part of the page NEVER loads. I get a white screen

If I comment out the PHP the HTML portion loads.

0
 
LVL 2

Expert Comment

by:sylenix
Comment Utility
i reviewed your code and found these mistakes:

1. remove those double quotes when you echo each row as PHP will treat it as a string instead of variable
2.
$version = $_POST[OS_version];

Open in new window

must be
$version = $_POST[version];

Open in new window

since your input box is named "version"
3. also, check your SQL variables for values, i don't know if u left it blank on purpose.
4. since your program is executing an update only command you must manually insert a test data inside your database so you won't get a white screen.

happy coding!


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Config</title>	

    
    <style type="text/css">
<!--
.Red_Italics {
	color: #CC0000;
	font-style: italic;
}
.Entery_Fields {
	font-family:Arial, Helvetica, sans-serif;
	color: #000066;
}
.Bold_Heading {
	font-weight:bold;
}
-->
</style>
</head>
<body> 
<?php 
$host = "localhost";
$username = "root";
$password = "";
$database = "test";
$table = "pw_config";

$link = mysql_connect($host, $username, $password) or die(mysql_error());
$db = mysql_select_db($database, $link) or die(mysql_error());

if(!empty($_POST)){
 $version = $_POST[version];
 $status = $_POST[server_status];
 $helpdesk = $_POST[helpdesk];
 $shopping_cart = $_POST[shopping_cart];
 $backup_server = $_POST[backup_server];
 $notes = $_POST[notes];
 

 $query = "update $table set OS_version = '$version', server_status = '$status', helpdesk = '$helpdesk', shopping_cart = '$shopping_cart', backup_server = '$backup_server', notes = '$notes'";
 $result = mysql_query($query) or die(mysql_error());

 echo"Record updated!";
}

$query = "select * from $table";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
?>
<form action="#" method="post">
	<label for="version">Version:</label><br />
	<input type="textbox" name="version" value="<?php echo $row[version]; ?>" class="Entery_Fields"/><br /><br />

	<label for="helpdesk">Helpdesk:</label><br />
	<select name="helpdesk" class="Entery_Fields" value="<?php echo $row[helpdesk]; ?>"/><br /><br />
		<option value="0">Offline</option>
		<option value="1">Online</option>
		<option value="2">Maintenance Mode</option>
	</select>
	<br /><br />

	<label for="shopping_cart">Shopping Cart:</label><br />
	<select name="shopping_cart" class="Entery_Fields" value="<?php echo $row[shopping_cart]; ?>"/><br /><br />
		<option value="0">Offline</option>
		<option value="1">Online</option>
		<option value="2">Maintenance Mode</option>
	</select>
	<br /><br />

	<label for="backup_server">Backup Server:</label><br />
	<select name="backup_server" class="Entery_Fields" value="<?php echo $row[backup_server]; ?>"/><br /><br />
		<option value="0">Offline</option>
		<option value="1">Online</option>
		<option value="2">Maintenance Mode</option>
	</select>
	<br /><br />

	<label for="notes">Notes:</label><br />
	<textarea name="notes" cols="100" rows="10" class="Entery_Fields" value="<?php echo $row[notes]; ?>"/></textarea><br /><br /><br />


	<input type="submit" value="update" />
</form>

</body>
</html>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Then take a look at the code I posted. You do not get any results because you do not have any records in the pw_config.
0
 
LVL 3

Author Comment

by:Computer Guy
Comment Utility
Referring to this persons comment: sylenix:

How do I make the tags from the DB populate the "real time value" in the select fields when the page is loaded?
0
 
LVL 8

Expert Comment

by:fundacionrts
Comment Utility
After change table named configuration to pw_config, i execute your script. I notice that there is a problem when at line
..
$query = "select * from $table";
>> $result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
...

the query return no result. Then, when you call mysql_fetch_row($result) it raise an error.

You would try to include this code lines

..
$query = "select * from $table";
$result = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($result) > 0)
   $row = mysql_fetch_row($result) or die(mysql_error());
else
   $ row = null;
...

to resolve this problem. I notice too that your are using $row[<fieldname>] instead $row[<fieldnumber>]. To use $row[<fieldaname>] you must to use mysql_fetch_assoc($result) instead mysql_fetch_row($result).

Some variables retrieves from $row[...] have mismatch names with database table, by example, version > OS_version. Coreect this names too.

I hope this intructions help you!

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Again: take a look at my code snippet. The fields are populated with mysql_fetch_array
0
 
LVL 3

Author Comment

by:Computer Guy
Comment Utility
Roads_Roads: It worked, but didn't populate the notes section
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
Comment Utility
This is the whole file content, does work for me

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Config</title>	

    
    <style type="text/css">
<!--
.Red_Italics {
	color: #CC0000;
	font-style: italic;
}
.Entery_Fields {
	font-family:Arial, Helvetica, sans-serif;
	color: #000066;
}
.Bold_Heading {
	font-weight:bold;
}
-->
</style>
</head>
<body> 
<?php 

$host = "localhost";
$username = "";
$password = "";
$database = "seg_Config";
$table = "pw_config";

$link = mysql_connect($host, $username, $password) or die(mysql_error());
$db = mysql_select_db($database, $link) or die(mysql_error());

if(!empty($_POST)){
 $version = $_POST[OS_version];
 $status = $_POST[server_status];
 $helpdesk = $_POST[helpdesk];
 $shopping_cart = $_POST[shopping_cart];
 $backup_server = $_POST[backup_server];
 $notes = $_POST[notes];
 
 

 $query = "update $table set OS_version = '$version', server_status = '$status', helpdesk = '$helpdesk', shopping_cart = '$shopping_cart', backup_server = '$backup_server', notes = '$notes'";
 $result = mysql_query($query) or die(mysql_error());

 echo"Record updated!";
}


$query = "select * from $table";
$result = mysql_query($query) or die ("eee");
$num = mysql_num_rows($result);
if($num > 0){
    $row = mysql_fetch_array($result) or die("error");
}

?>

<form action="#" method="post">
<label for="version">Version:</label><br />
<input type="textbox" name="version" value="<?php echo"$row[OS_version]";?>" class="Entery_Fields"/><br /><br />

<label for="helpdesk">Helpdesk:</label><br />
<select name="helpdesk" class="Entery_Fields" /><br /><br />
<option value="0" <?php if($row[helpdesk] == 0) echo"selected=\"selected\"";?> >Offline</option>
<option value="1" <?php if($row[helpdesk] == 1) echo"selected=\"selected\"";?>>Online</option>
<option value="2" <?php if($row[helpdesk] == 2) echo"selected=\"selected\"";?>>Maintenance Mode</option>
</select><br /><br />

<label for="shopping_cart">Shopping Cart:</label><br />
<select name="shopping_cart" class="Entery_Fields" /><br /><br />
<option value="0" <?php if($row[shopping_cart] == 0) echo"selected=\"selected\"";?>>Offline</option>
<option value="1" <?php if($row[shopping_cart] == 0) echo"selected=\"selected\"";?>>Online</option>
<option value="2" <?php if($row[shopping_cart] == 0) echo"selected=\"selected\"";?>>Maintenance Mode</option>
</select><br /><br />

<label for="backup_server">Backup Server:</label><br />
<select name="backup_server" class="Entery_Fields" /><br /><br />
<option value="0" <?php if($row[backup_server] == 0) echo"selected=\"selected\"";?>>Offline</option>
<option value="1" <?php if($row[backup_server] == 0) echo"selected=\"selected\"";?>>Online</option>
<option value="2" <?php if($row[backup_server] == 0) echo"selected=\"selected\"";?>>Maintenance Mode</option>
</select><br /><br />

<label for="notes">Notes:</label><br />
<textarea name="notes" cols="100" rows="10" class="Entery_Fields" /><?php echo"$row[notes]";?></textarea><br /><br /><br />


<input type="submit" value="update" />
</form>

</body>
</html>

Open in new window

0
 
LVL 2

Expert Comment

by:sylenix
Comment Utility
How do I make the tags from the DB populate the "real time value" in the select fields when the page is loaded?

since your form is designed for only 1 active record at a time, you could try making your SQL statement dynamic by using a variable in place of your WHERE or LIKE in your SQL statement. also, instead of mysql_fetch_row() use mysql_fetch_assoc() function so you can move to the the next row in recordset, just then create a button to call that statement to move backwards or forwards.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Creating and Managing Databases with phpMyAdmin in cPanel.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now