Moneyman2010mn Posted March 20, 2007 Share Posted March 20, 2007 Hi, I'm working on a poll script and was wondering what was wrong with my code. The SQL I echoed seems right but nothing gets filled in. $votes actually equals vote_(voting number) where (voting number) equas 0, 25, 50, 75, or 100 depending on how they voted. vote_(voting number) is also the name of colums in the database. Okay so here is the code that runs when the info is submitted. (I will eventually protect it from SQL injections. If anyone has any tips on that then please tell me.) Maybe it's because I'm trying to use a variable in the MySQL code but how else would I do it? <?php $connect = mysql_connect('localhost', '*****', '*****') or die('Could not connect to database'); $db = mysql_select_db('*****') or die('Could not select database'); $poll_name = $_POST['poll_name']; $votes = $_POST['votes']; $sql = "UPDATE polls SET $votes = '$votes + 1' AND votes_number = 'votes_number + 1' WHERE poll_name = '$poll_name'"; $update = mysql_query($sql); $disconnect = mysql_close(); ?> The sql that was executed was "UPDATE polls SET votes_100 = 'votes_100 + 1' AND votes_number = 'votes_number + 1' WHERE poll_name = 'test'" Link to comment https://forums.phpfreaks.com/topic/43558-solved-updating-database-table/ Share on other sites More sharing options...
papaface Posted March 20, 2007 Share Posted March 20, 2007 try: $votes++; $votes_number++; $sql = "UPDATE polls SET $votes = '$votes', votes_number = '$votes_number' WHERE poll_name = '$poll_name'"; Link to comment https://forums.phpfreaks.com/topic/43558-solved-updating-database-table/#findComment-211545 Share on other sites More sharing options...
Moneyman2010mn Posted March 20, 2007 Author Share Posted March 20, 2007 Thanks, but no that didn't work since $votes isn't an integer. $votes is basically storing the column name. I was thinking I can just put the name of the column + 1 when setting something because of what the MySQl site shows: http://dev.mysql.com/doc/refman/5.0/en/update.html . You see "UPDATE t SET id = id + 1;"? id was updated to increase be increased by one when the sql was run, correct? Any ideas? Link to comment https://forums.phpfreaks.com/topic/43558-solved-updating-database-table/#findComment-211587 Share on other sites More sharing options...
Iceman512 Posted March 21, 2007 Share Posted March 21, 2007 Hello there, I think you may have an error in your sql statement. $sql = "UPDATE polls SET $votes = '$votes + 1' The query $votes = '$votes + 1' should be a name such as votes = '$votes + 1' If, as you say, it matches the name of the database column, you will have to define the variable, $votes (thedb column) somewhere in your code. Perhaps you could rename it to something else, such as $voteRow. Like this: $voteRow = $row['votes']; Either that or you could do the addition in a variable outside the sql statement. Such as: $putVotes = "($_POST['votes'] + 1)"; or rather: $putVotes = ($row['votes'] + 1); ... and then use the $putVotes variable in the sql statement. Just a thought. I hope it helps. Iceman Link to comment https://forums.phpfreaks.com/topic/43558-solved-updating-database-table/#findComment-211996 Share on other sites More sharing options...
Moneyman2010mn Posted March 22, 2007 Author Share Posted March 22, 2007 Thanks, that gave me an idea and helped me solve the problem! Link to comment https://forums.phpfreaks.com/topic/43558-solved-updating-database-table/#findComment-212450 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.