PHP database help

Posted on 2011-10-13
Last Modified: 2012-05-12
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.

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

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

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

	// this is processed when the form is submitted
	// back on to this page (POST METHOD)

		// 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">
<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>



Open in new window

Question by:savone
    LVL 4

    Expert Comment

    What the error, if any?
    LVL 82

    Expert Comment

    by:Dave Baldwin
    This page  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

    Open in new window

    LVL 23

    Author Comment

    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') {
    LVL 82

    Accepted Solution

    "REQUEST_METHOD" is a constant, not a variable, so you don't want the $.  

    LVL 23

    Author Closing Comment

    Works like a charm now!
    LVL 82

    Expert Comment

    by:Dave Baldwin
    Good, glad to help.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    755 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

    19 Experts available now in Live!

    Get 1:1 Help Now