• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Simple PHP page that saves a few variables to a database

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
Computer Guy
Asked:
Computer Guy
  • 6
  • 3
  • 2
  • +3
1 Solution
 
pius_babbunCommented:
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
 
Lukasz ChmielewskiCommented:
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
 
Lukasz ChmielewskiCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Lukasz ChmielewskiCommented:
The select fields have to be checked like the above way, you cannot set select field value like you did.
0
 
blueghoztCommented:
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
 
Computer GuyAuthor Commented:
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
 
sylenixCommented:
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
 
Lukasz ChmielewskiCommented:
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
 
Computer GuyAuthor Commented:
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
 
fundacionrtsAdministrador de SistemasCommented:
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
 
Lukasz ChmielewskiCommented:
Again: take a look at my code snippet. The fields are populated with mysql_fetch_array
0
 
Computer GuyAuthor Commented:
Roads_Roads: It worked, but didn't populate the notes section
0
 
Lukasz ChmielewskiCommented:
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
 
sylenixCommented:
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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now