Jump to content

[SOLVED] MySQL UPDATE issue and a problem with fwrite().


ZeroError

Recommended Posts

It may seem odd that I have this combination, but it works best for me given that I'm still a beginner and am only just moving into MySQL. Anyhoo, here's my MySQL issue:

 

I created a simple script to increment the amount of cans I have (For an art project) in my inventory, and write that into a database. That database is then read on the homepage and printed to the screen as "I have <number> cans left, but I need <65 - number> more!". I have this code in the backend, which SHOULD allow me to update the number, but it just seems to stay at 6!

/* Before we do anything, we should connect to MySQL. */
mysql_connect("myserver", "db_user", "password") or die(mysql_error());
mysql_select_db("table_name") or die(mysql_error());

function incrementCans()
{
// Declare variables required for MySQL
$prevTotal 		= mysql_query("SELECT * FROM cans") or die(mysql_error());
$prevTotalRow 	= mysql_fetch_array( $prevTotal );
$updateTo		= $prevTotalRow['total'] + $_POST['amount'];

// Update can value
mysql_query("UPDATE cans SET total=".$updateTo." WHERE total=".$prevTotalRow['total']."") or die(mysql_error());

// Check database for updated table
$newTotal		= mysql_query("SELECT * FROM cans") or die(mysql_error());
$newTotalRow	= mysql_fetch_array( $newTotal );

echo("Total cans updated to " . $newTotalRow['total']);
}

 

I've changed nothing since it last worked (About a week ago) and no errors are given.

 

 

 

And now onto my second problem, with fwrite(). The script should check to see if a checkbox is ticked and write the contents of the corresponding textbox to the file, but I get the following message:

Warning: Wrong parameter count for fwrite() in /home/kittycat/public_html/admin/addline_all.php on line 140

 

My code goes like this:

133.    if (isset($_POST['maincheck']))
134.    	{
135.    	$file	= "../random/homebody.txt";
136.    	$handle	= fopen($file, 'a');
137.    	// checkFileOK_Write();
138.    	if (fopen($file, 'a'))
139.    		{
140.    		if (fwrite($handle))
141.    			{
142.    			echo ('Written \"' . $data . '\" successfully.');
143.    			fclose($handle);
144.    			}
145.    		else
146.    			{
147.    			echo ('Failure to write \"' . $data . '\".');
148.    			fclose($handle);
149.    			}
150.    		}
151.    	else
152.    		{
153.    		echo ('Failure to open \"' . $data . '\".');
154.    		}
155.    	}

Line 140 is

if (fwrite($handle))

in case my numbering didn't work.

 

Any input for either of these would be very useful, and much appreciated.

Thanks in advance!

If you check the manual you'll see that fwrite() takes 2 mandatory parameters: 1. The resouce ($handle in your case) which you are passing. 2. The data that you want to write to the file.

 

if(fwrite($handle, $data)) {...}

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.