如何通过添加以前的点和当前点来更新点


How to update points by adding previous points and current points

如何通过添加以前的投票和新的投票来更新我的php/mysql例如,在mysql中。投票点是25。当我以25分再次参赛时。它变成了50分。这就是场景。我有一个表名"subj_eva",上面有id、facultyname和totalvotes。如何通过添加旧积分和新积分来更新我的总票数?

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="password"; // Mysql password 
$db_name="ramon_pascual"; // Database name 
$tbl_name="subj_eva"; // Table name 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form 
$profname=$_POST['profname'];
$votecount=$_POST['votecount'];
$subj=$_POST['subject'];

// Insert data into mysql 
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes='$votecount', subjects='$subj'";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='indextest.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?> 
<?php 
// close connection 
mysql_close();

?>

这是我的html代码

<html>
<head><title> index test</title></head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="welcome.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td width="71">Professor Name</td>
<td width="6">:</td>
<td width="301"><input name="profname" type="text" id="profname"></td>
</tr>
<tr>
<td>vote count</td>
<td>:</td>
<td><input name="votecount" type="text" id="votecount"></td>
</tr>
<tr>
<td>subject</td>
<td>:</td>
<td><input name="subject" type="text" id="subject"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

您可以修改查询,将新值添加到当前值中。我建议事先将votecount转换为整数。

$votecount = intval($votecount);
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes=totalvotes + $votecount, subjects='$subj'";

尝试:$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes=totalvotes + '$votecount', subjects='$subj'";

我不明白你想要什么,但在传输函数mysql_real_aescape_string()数据库中总是使用任何字符串变量!否则可能会注射Mysql。并且在双引号中变量突出显示括号{},否则函数将给出数据库不是变量。

试试这个。。

$votecount=$_POST['votecount'];
$getprevious =mysql_fetch_array(mysql_query("select * from $tbl_name order by id desc"));
$previouspoint= $getprevious[0]['totalvotes'];
$votecount = intval($previouspoint) + intval($votecount);
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes='$votecount', subjects='$subj'";
$result=mysql_query($sql);

尝试像这样使用

$query="update table_name set 'totalvotes'=(select `totalvotes` from `table_name` where id='".$id."')+'".$current_count."' where id='".$id."' ";