ZeroError Posted November 1, 2009 Share Posted November 1, 2009 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! Link to comment https://forums.phpfreaks.com/topic/179861-solved-mysql-update-issue-and-a-problem-with-fwrite/ Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 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)) {...} Link to comment https://forums.phpfreaks.com/topic/179861-solved-mysql-update-issue-and-a-problem-with-fwrite/#findComment-948835 Share on other sites More sharing options...
ZeroError Posted November 1, 2009 Author Share Posted November 1, 2009 Oh, dear >.< I can't believe I forgot the data to write :L Thanks very much for that. Still need help with the MySQL issue, though, most confusing. Link to comment https://forums.phpfreaks.com/topic/179861-solved-mysql-update-issue-and-a-problem-with-fwrite/#findComment-948837 Share on other sites More sharing options...
Alex Posted November 1, 2009 Share Posted November 1, 2009 put error_reporting(E_ALL); at the top of the page and see if any errors are thrown. Link to comment https://forums.phpfreaks.com/topic/179861-solved-mysql-update-issue-and-a-problem-with-fwrite/#findComment-948838 Share on other sites More sharing options...
ZeroError Posted November 1, 2009 Author Share Posted November 1, 2009 Thanks, I did that and it showed an undefined index on line 29. This was the $_POST variable for the amount of cans and was spelt wrong. Thanks for that, I'll be using that function a lot in the future. Link to comment https://forums.phpfreaks.com/topic/179861-solved-mysql-update-issue-and-a-problem-with-fwrite/#findComment-948845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.