DiscoTrio Posted October 31, 2010 Share Posted October 31, 2010 I used to be good at this but I changed servers and everything is different... Heres my code so far: <?php $rated=$_REQUEST['rated']; echo $rated; $rating=$_REQUEST['rating']; echo $rating; // Make a MySQL Connection mysql_connect("localhost", "********", "********") or die(mysql_error()); mysql_select_db("*********") or die(mysql_error()); $result = mysql_query("SELECT * FROM main WHERE username = '$rated'") or die(mysql_error()); $row = mysql_fetch_array( $result ); $votes = $db_field['$rating']; $newvotes = $votes + 1; echo $newvotes; mysql_query("UPDATE main SET $rating = '$newvotes' WHERE username = '$rated'"); ?> Whats going on here is the colomb that I want to update comes as a variable $rated (That works) and then the database selects the row to update with $username (That works) and gets the variable $newvotes by taking the original value of the data its about to update and add 1 to it (That works) Then it updates the field to $newvotes.... I don't know why the update won't go through... there are no errors.... Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/ Share on other sites More sharing options...
Twitch Posted October 31, 2010 Share Posted October 31, 2010 maybe try without ' "UPDATE main SET $rating = $newvotes WHERE username = $rated" or try "UPDATE main SET $rating ='".$newvotes."' WHERE username = '".$rated."'" is the database column really named $rating or is it simply rating? Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128629 Share on other sites More sharing options...
DiscoTrio Posted October 31, 2010 Author Share Posted October 31, 2010 It really is called that. Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128632 Share on other sites More sharing options...
DiscoTrio Posted October 31, 2010 Author Share Posted October 31, 2010 Hmm putting the query in directly says this: The following errors were reported:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 ='1' WHERE username ='DiscoTrio'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128635 Share on other sites More sharing options...
Twitch Posted October 31, 2010 Share Posted October 31, 2010 Does the query work with the data hard coded? "UPDATE main SET $rating = '5' WHERE username = '1'" Not sure what your actual data is, I just picked some random numbers. Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128639 Share on other sites More sharing options...
DiscoTrio Posted October 31, 2010 Author Share Posted October 31, 2010 Nope, thats what I just did and got that error. Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128641 Share on other sites More sharing options...
gevans Posted October 31, 2010 Share Posted October 31, 2010 You should be able to do this with a single uery, something like this (please note the comments on the first few lines, I assumed $_POST data; <?php /** * $_REQUEST contains all od the data from $_POST, $_GET and $_COOKIE * * If you know where the data is coming from you should use the correct array * I've guessed $_POST */ $rated=$_POST['rated']; echo $rated; $rating=$_POST['rating']; echo $rating; // Make a MySQL Connection mysql_connect("localhost", "********", "********") or die(mysql_error()); mysql_select_db("*********") or die(mysql_error()); $result = mysql_query("UPDATE `main` SET `rating` = (`rating` + 1) WHERE `username` = '$rated'"); if($result) { echo 'good'; } else { echo 'bad'; } ?> I assumed that the field in the database that you're updating is `rating`. If this is called something different replace both instances of `rating` with the correct name. Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128644 Share on other sites More sharing options...
DiscoTrio Posted October 31, 2010 Author Share Posted October 31, 2010 Im already not liking my new hosting company, now I can't get a sing query to work, Gevans, your query makes it return "Your query did not return any results. (0.0004 seconds)" Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128646 Share on other sites More sharing options...
Twitch Posted October 31, 2010 Share Posted October 31, 2010 gevans, I assumed it was called 'rating' as well, but he said it really was called $rating. I wasn't even aware that you could use $ in the name, but I just tried it and mysql let me...haha Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128649 Share on other sites More sharing options...
gevans Posted October 31, 2010 Share Posted October 31, 2010 Im already not liking my new hosting company, now I can't get a sing query to work, Gevans, your query makes it return "Your query did not return any results. (0.0004 seconds)" The server you're using should make no difference if you're still using php and mysql (as I'm guessing you were before). Also an update query shouldn't attempt to return results, It would just return true or false. If you use phpmyadming try using the following query in the sql tab; UPDATE `main` SET `rating` = `rating` + 1 WHERE `username` = 'PUT A USERNAME IN HERE' Just replace 'PUT USERNAME IN HERE' with a username you know is in the db. Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128654 Share on other sites More sharing options...
DiscoTrio Posted October 31, 2010 Author Share Posted October 31, 2010 Its called the value that $rating returns, which is 1,2,3,4 I have that established and it works. And this server has SQL Buddy, I use PHPMYADMIN before... Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128655 Share on other sites More sharing options...
gevans Posted October 31, 2010 Share Posted October 31, 2010 So let me just get it clear. This page finds two varialbes (I'm assuming via $_POST) '$rated' and '$rating'. Then you want to update the db called 'main'. Update the 'rating' field by '$rating' where the username is equal to '$rated'?? If this is correct then a little change and you should have no problem. Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128658 Share on other sites More sharing options...
DiscoTrio Posted October 31, 2010 Author Share Posted October 31, 2010 yes Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128660 Share on other sites More sharing options...
gevans Posted October 31, 2010 Share Posted October 31, 2010 <?php $rated = $_POST['rated']; echo $rated; $rating = (int) $_POST['rating']; echo $rating; // Make a MySQL Connection mysql_connect("localhost", "********", "********") or die(mysql_error()); mysql_select_db("*********") or die(mysql_error()); $result = mysql_query("UPDATE `main` SET `rating` = `rating` + $rating WHERE `username` = '$rated'"); if($result) echo 'good'; else echo 'bad'; ?> Now if everything works you should get the username and rating printed, followed by either 'good' or 'bad'. Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128661 Share on other sites More sharing options...
DiscoTrio Posted October 31, 2010 Author Share Posted October 31, 2010 DiscoTrio3bad For some reason the query won't go through the database... Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128665 Share on other sites More sharing options...
gevans Posted October 31, 2010 Share Posted October 31, 2010 So there's an error in the SQL somewhere, I'm guessing you have errors turned off, add; ini_set('display_errors', 1); ini_set('error_reporting', E_ALL); To the top of the script, just after the opening php tag and reply with your results! Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1128666 Share on other sites More sharing options...
DiscoTrio Posted November 4, 2010 Author Share Posted November 4, 2010 Here is another reason my host sucks: Warning: ini_set() has been disabled for security reasons in /www/zzl.org/r/o/b/robloxfashion/htdocs/voting.php on line 10 Warning: ini_set() has been disabled for security reasons in /www/zzl.org/r/o/b/robloxfashion/htdocs/voting.php on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/217349-need-help-updating-data-in-mysql-database-from-php-page/#findComment-1130127 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.