[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

PHP database help

I am writing a little php script just to learn php and mysql a little bit.  The code is attached below, for some reason it is not writing to the database.  Any help would be appreciated.

Database resides on the same machine, and all database user permissions are correct.  I long log into mysql command line and add to the database as this user.


<?php
	$usr = "dailystat";
	$pwd = "okaydokay";
	$db = "statrepo";
	$host = "localhost";

	// connect to database
	$cid = mysql_connect($host,$usr,$pwd);
	if (!$cid) { echo("ERROR: " . mysql_error() . "\n");	}
?>
<html>
<head>
   <title>Vona Daily Status Repository</title>
</head>
<body bgcolor="#ffffff">

<p><font size=5><b> Add a status... </b> </font></p>

<?php
	// this is processed when the form is submitted
	// back on to this page (POST METHOD)
	if ($_SERVER['$REQUEST_METHOD'] == 'POST') {

		// double-up apostrophes
		// $stat1 = str_replace("'","''",$stat1);
		// $stat2 = str_replace("'","''",$stat2);
		// $stat3 = str_replace("'","''",$stat3);
		// $stat4 = str_replace("'","''",$stat4);
		// $stat5 = str_replace("'","''",$stat5);
		// $stat6 = str_replace("'","''",$stat6);
	
		$stat1 = $_POST['stat1'];
		$stat2 = $_POST['stat2'];
		$stat3 = $_POST['stat3'];
		$stat4 = $_POST['stat4'];
		$stat5 = $_POST['stat5'];
		$stat6 = $_POST['stat6'];

		echo ( $stat1 );
		// setup SQL statement
		$SQL = " INSERT INTO dailyupdate (stat1,stat2,stat3,stat4,stat5,stat6) VALUES ('$stat1','$stat2','$stat3','$stat4,'$stat5','$stat6') ";

		//execute SQL statement
		$result = mysql_db_query($db,"$SQL",$cid);

		// check for error
		if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");	}

		echo ("<p><b>New Status Added</b></p>\n");

	}

?>

<form name="fa" action="insert_status.php" method="post">
<table>
<tr><td valign=top><b>Status Update #1: </b> </td><td> <textarea name="stat1" rows=5 cols=100></textarea></td></tr>
<tr><td valign=top><b>Status Update #2: </b> </td><td> <textarea name="stat2" rows=5 cols=100></textarea></td></tr>
<tr><td valign=top><b>Status Update #3: </b> </td><td> <textarea name="stat3" rows=5 cols=100></textarea></td></tr>
<tr><td valign=top><b>Status Update #4: </b> </td><td> <textarea name="stat4" rows=5 cols=100></textarea></td></tr>
<tr><td valign=top><b>Status Update #5: </b> </td><td> <textarea name="stat5" rows=5 cols=100></textarea></td></tr>
<tr><td valign=top><b>Status Update #6: </b> </td><td> <textarea name="stat6" rows=5 cols=100></textarea></td></tr>
<tr><th colspan=2><p><input type="submit" value="Add Updates to DB"></p></th></tr>
</table>
</form>


<?php
	mysql_close($cid); 
?>

</body>
</html>

Open in new window

0
savone
Asked:
savone
  • 3
  • 2
1 Solution
 
CKY092Commented:
What the error, if any?
0
 
Dave BaldwinFixer of ProblemsCommented:
This page http://us3.php.net/manual/en/function.mysql-db-query.php  recommends a different way of doing that.

Are you getting an error messages?  To make sure you see any errors, put the code below at the top of your PHP code.
// Report all PHP errors
error_reporting(E_ALL);

Open in new window

0
 
savoneAuthor Commented:
This is the error I am getting in the httpd error log:

PHP Notice:  Undefined index: $REQUEST_METHOD in /var/www/html/insert_status.php on line 23, referer: http://localhost/insert_status.php


If I remove the following if statement the database is updated fine.

if ($_SERVER['$REQUEST_METHOD'] == 'POST') {
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
Dave BaldwinFixer of ProblemsCommented:
"REQUEST_METHOD" is a constant, not a variable, so you don't want the $.  

if ($_SERVER['REQUEST_METHOD'] == 'POST')

http://www.php.net/manual/en/reserved.variables.server.php
0
 
savoneAuthor Commented:
Works like a charm now!
0
 
Dave BaldwinFixer of ProblemsCommented:
Good, glad to help.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

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