Jump to content

[SOLVED] Updating Database Table


Moneyman2010mn

Recommended Posts

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

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?

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

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.