Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple PHP page that saves a few variables to a database

Posted on 2011-03-15
14
Medium Priority
?
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

by:pius_babbun
ID: 35136558
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
ID: 35136574
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
ID: 35136633
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35136643
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
ID: 35136652
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
ID: 35136663
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
ID: 35136677
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35136680
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
ID: 35136805
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
ID: 35136814
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
ID: 35136830
Again: take a look at my code snippet. The fields are populated with mysql_fetch_array
0
 
LVL 3

Author Comment

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

Accepted Solution

by:
Lukasz Chmielewski earned 2000 total points
ID: 35137037
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
ID: 35137468
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 Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 count occurrences of each item in an array.

722 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