2016-10-01 52 views
0

中的選定數據更新數據庫中的數據我有一個HTML表單,我使用下拉列表或選項顯示所有候選列表。所有選項的數據都來自數據庫。這裏:使用選項

<form method ="post" action=""> 
<div class="gov-align"> 
<div class="hero-body-candidate_gov"> 
<font color="black">Candidate for Governor</font> 
</div> 
<div class="governor"> 
<div class="gov-margin"> 
<?php 
$governor= "SELECT a.cand_id, cand_Lname, cand_Fname,  cand_partylist,cand_position, image FROM cand_info as a INNER JOIN cand_account as b ON a.cand_id=b.cand_id INNER JOIN cand_position as c ON b.cand_id=c.cand_id WHERE cand_position = 'President'"; 
$res = mysqli_query($con,$governor); 
while($row=mysqli_fetch_array($res)){ $governor_id=$row['cand_id']; ?> 
<img class="gov" src="<?php echo $row['image'];?>" width="150" height="150" border="0" onmouseover="showtrail('<?php echo $row['image'];?>','<?php echo $row['cand_Fname']." ".$row['cand_Lname'];?> ',200,5)" onmouseout="hidetrail()"> 
&nbsp;&nbsp;&nbsp;&nbsp; 


<?php 
} 
?> 
</div> 
</div> 
<div class="select_gov"> 
<div class="margin-gov"> 
<select name="governor" class="span222"> 
<option class="option">--Select Candidate--</option> 
<?php 
$governor= "SELECT a.cand_id, cand_Lname, cand_Fname, cand_partylist,cand_position FROM cand_info as a INNER JOIN cand_account as b ON a.cand_id=b.cand_id INNER JOIN cand_position as c ON b.cand_id=c.cand_id WHERE cand_position = 'President'"; 
$res = mysqli_query($con,$governor); 
while($row=mysqli_fetch_array($res)){ $governor_id=$row['cand_id']; ?> 

<option name="" value="<?php echo $row['cand_id'] ?>" class="option"><?php echo $row['cand_Fname']." ".$row['cand_Lname']; ?></option> 
<?php } ?> 
</select> 
</div> 
</div> 
</div> 
<div class="thumbnail_widget"> 

<div class="submit-vote"> 

<button id="submit" id="vote" class="btn btn-success" name="submit"><i class="icon-thumbs-up icon-large"></i>&nbsp;Submit Vote</button> 
</div> 
</div> 

這是PHP行下面那個HTML。

<?php 

if(isset($_POST['submit'])) { 
$gov  = isset($_['cand_id']) ? $_POST['cand_id'] : ''; 
$sql1="UPDATE cand_votecount SET votecount = votecount + 1 WHERE cand_id = '$gov'"; 

$result1 = mysqli_query($con,$sql); 
} 
?> 
</form> 

我想爲學生選擇的候選人投票計數添加一個。每當我點擊提交它只刷新並沒有發生。

PS。我知道我的代碼可以很容易地注入sql注入。沒有必要對此發表評論。

+0

你定義什麼是'votecount + 1'? – Karthi

+0

你是否提到了你的整個表單HTML代碼?如果是這樣,它缺少表單關閉標籤和提交按鈕。 –

+0

votecount是我的數據庫中的行 – Pardz

回答

0

做一個小的變化

if(isset($_POST['governor'])) 
{ 
$sql1="UPDATE cand_votecount SET votecount = votecount + 1 WHERE cand_id =".$_POST['governor']; 
$result1 = mysqli_query($con,$sql); 
} 
+0

仍未更新。 – Pardz

+0

反對!對不起..但你沒有形式結束表格並提交按鈕它將如何提交?好吧好吧,我會糾正它@Pardz –

+0

我更新了我的貼子的形式和提交按鈕的帖子。 – Pardz