Jump to content

Saving to mysql database doesn't work?


jomak73

Recommended Posts

Ooook I just started learning php and mysql about a week ago, so I dunno if this code is like super messed up.. but here it is.

 

function saveForm()
{
global $suname, $task11, $task12, $task13, $task21, $task22, $task23, $task31, $task32, $task33, $task41, $task42, $task43, $task51, $task52, $task53,  $sproject1, $sproject2, $sproject3, $sproject4, $sproject5, $sweekending;

$numrows=15;
$irow=1;
$ncnt=0;

while ($irow <= 3)
{
	trim('saveDay($suname, $sproject1, "mon", $task1'+$irow+', 1, $ncnt++, $irow);');   // I dunno if trim is right here. I needed something that would let me have the 4th variable change on each loop
	trim('saveDay($suname, $sproject1, "tue", $task1'+$irow+', 2, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject1, "wed", $task1'+$irow+', 3, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject1, "thu", $task1'+$irow+', 4, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject1, "fri", $task1'+$irow+', 5, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject1, "sat", $task1'+$irow+', 6, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject1, "sun", $task1'+$irow+', 7, $ncnt++, $irow);');

	++$irow;
}
while ($irow <= 6)
{
	trim('saveDay($suname, $sproject2, "mon", $task2'+$irow-3+', 1, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject2, "tue", $task2'+$irow-3+', 2, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject2, "wed", $task2'+$irow-3+', 3, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject2, "thu", $task2'+$irow-3+', 4, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject2, "fri", $task2'+$irow-3+', 5, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject2, "sat", $task2'+$irow-3+', 6, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject2, "sun", $task2'+$irow-3+', 7, $ncnt++, $irow);');

	++$irow;
}
while ($irow <= 9)
{
	trim('saveDay($suname, $sproject3, "mon", $task3'+$irow-6+', 1, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject3, "tue", $task3'+$irow-6+', 2, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject3, "wed", $task3'+$irow-6+', 3, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject3, "thu", $task3'+$irow-6+', 4, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject3, "fri", $task3'+$irow-6+', 5, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject3, "sat", $task3'+$irow-6+', 6, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject3, "sun", $task3'+$irow-6+', 7, $ncnt++, $irow);');

	++$irow;
}
while ($irow <= 12)
{
	trim('saveDay($suname, $sproject4, "mon", $task4'+$irow-9+', 1, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject4, "tue", $task4'+$irow-9+', 2, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject4, "wed", $task4'+$irow-9+', 3, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject4, "thu", $task4'+$irow-9+', 4, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject4, "fri", $task4'+$irow-9+', 5, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject4, "sat", $task4'+$irow-9+', 6, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject4, "sun", $task4'+$irow-9+', 7, $ncnt++, $irow);');

	++$irow;
}
while ($irow <= 15)
{
	trim('saveDay($suname, $sproject5, "mon", $task5'+$irow-12+', 1, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject5, "tue", $task5'+$irow-12+', 2, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject5, "wed", $task5'+$irow-12+', 3, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject5, "thu", $task5'+$irow-12+', 4, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject5, "fri", $task5'+$irow-12+', 5, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject5, "sat", $task5'+$irow-12+', 6, $ncnt++, $irow);');
	trim('saveDay($suname, $sproject5, "sun", $task5'+$irow-12+', 7, $ncnt++, $irow);');

	++$irow;
}
}

 

The point is to take information that the user has entered (all the global variables) and save them in a database. Buuut.. it doesnt work.

 

The saveDay function is defined as:

function saveDay($suname, $sproject, $sday, $itask, $iday, $ncnt, $irow)
{	
	$sdate    = $_REQUEST[$sday];
	$sdate2 = date("Y-m-d",strtotime($sdate));
	mysql_query("DELETE FROM Time WHERE date='$sdate2' AND username='$suname' AND project_id='$sproject' AND task_id=$itask");

	$shours   = trim($_REQUEST["hrs" . $irow . "-" . $iday]);   // <--- takes the number entered into a cell on row(1-15) and day of the week(1-7) of a table
	if ( strlen($shours)>0 && doubleval($shours)>0 ) 
	{
		$sbhours   = trim($_REQUEST["bhrs" . $irow . "-" . $iday]);      // takes another number entered into a cell on row(1-15) and day of the week
		if ( strlen($sbhours)==0 ) $sbhours="0";    // if nothing was entered, make it 0

		$scomment   = rawurldecode($_REQUEST["comment" . $irow . "-" . $iday]); // Make sure the comment raw text       // gets a comment entered into a cell on row(1-15) and day of the week

		$query = "INSERT INTO Time VALUES (0, '$sdate2', '$suname', '$sproject', $itask, $shours, '$scomment', $sbhours)";

		mysql_query($query);
	}
}

 

Sorry if it's hard to understand. A solution would be greaaatly appreciated. THANKS!

Link to comment
https://forums.phpfreaks.com/topic/207764-saving-to-mysql-database-doesnt-work/
Share on other sites

Add some error message to your mysql_query:

 

mysql_query("QUERY HERE") or trigger_error("Unabled to do [ACTION HERE].<br />Error: " . mysql_error());

 

And this will tell you if there is an error in your query syntax. I have a hunch that in your insert 0 should be null.

this code is like super messed up..

 

Yes, definitely -

 

A) trim() returns the value it trims, so it would be necessary to assign the value to something in order to use it,

B) Don't pass values into a function using the global keyword and definitely don't pass that many. You should be using an array to hold a set of related data.

C) Just putting a function definition around a block of code does not make that code into a function. You must design a function to perform a specific task, accept specific parameters, and return a specific result.

 

it doesn't work.

 

Yes, clearly.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.