Jump to content

Need help updating data in MYSQL database from PHP page


DiscoTrio

Recommended Posts

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....

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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'.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.