如何通过添加先前的投票和新投票来更新我的php / mysql 例如,在mysql中。当我再次以25分进入时,投票点是25。它变成了50分。这就是场景。我有表名" subj_eva"与id,facultyname和totalvotes的coloumn。如何通过添加旧点和新点来更新我的总票数?
<?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>
答案 0 :(得分:1)
您可以修改查询以将新值添加到当前值。我建议事先将votecount转换为整数。
$votecount = intval($votecount);
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes=totalvotes + $votecount, subjects='$subj'";
答案 1 :(得分:1)
尝试:
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes=totalvotes + '$votecount', subjects='$subj'";
答案 2 :(得分:0)
我不明白你想要什么,但总是在传输函数mysql_real_escape_string()数据库中使用任何字符串变量!否则可能是Mysql注入。并且在双引号变量中突出显示括号{},否则该函数将赋予数据库不是变量。
答案 3 :(得分:0)
试试这个..
$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);
答案 4 :(得分:0)
尝试使用像这样的
$query="update table_name set 'totalvotes'=(select `totalvotes` from `table_name` where id='".$id."')+'".$current_count."' where id='".$id."' ";