如何通过添加以前的点和当前点来更新点
如何通过在mysql中添加前面的投票和新的投票 来更新我的php/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"><span>Insert Data Into mySQL Database </span></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_escape_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);
回答:
尝试使用废弃的`mysql_ *`函数中使用这样的
$query="update table_name set 'totalvotes'=(select `totalvotes` from `table_name` where id='".$id."')+'".$current_count."' where id='".$id."' ";
以上是 如何通过添加以前的点和当前点来更新点 的全部内容, 来源链接: utcz.com/qa/258380.html