从php更新多行SQL

时间:2019-02-23 08:32:47

标签: php mysql

我已经编写了一个php页面,用于根据下拉选择从mysql表中加载数据,该下拉列表也从表中填充。

然后,我希望某些数据能够被更改,然后在提交后将其写回到数据库中。

这是我到目前为止所拥有的;我可以使用底部的$ sql update命令获取投票人数和要更新的百分比,但是其他数据是我无法更新的名称列表,请参见$ sql1命令。

我尝试了很多方法,包括for($i=0;$i<$count;$i++){等。但是,我只能用它来更新其中一行。

真的很希望有人能提供帮助,因为我现在已经很近了

<?php 


require 'config.php'; 
?> 

<!doctype html public "-//w3c//dtd html 3.2//en"> 

<html> 

<head> 
<title>Election Results</title> 
<SCRIPT language=JavaScript> 
function reload(form) 
{ 
var val=form.cat.options[form.cat.options.selectedIndex].value; 
self.location='index.php?cat=' + val ; 
} 

</script> 
</head> 

<body> 
<?Php 


@$cat=$_GET['cat']; 
if(strlen($cat) > 0 and !is_numeric($cat)){ 
echo "Data Error"; 
exit; 
} 


$quer2="SELECT DISTINCT * FROM ward order by ward"; 




echo "<form method='post' action='$_PHP_SELF'>"; 


echo "<select name='cat' style='width:220px' onchange=\"reload(this.form)\"><option value=''>Select Ward</option>"; 
foreach ($dbo->query($quer2) as $noticia2) { 
if($noticia2['ward_id']==@$cat){echo "<option selected value='$noticia2[ward_id]'>$noticia2[ward]</option>"."<BR>"; 

} 
else{echo "<option value='$noticia2[ward_id]'>$noticia2[ward]</option>"; 

} 
} 
echo "</select><br><br>"; 


if(isset($cat) and strlen($cat) > 0){ 
$quer="SELECT DISTINCT * FROM candidate where ward_id=$cat order by candidate_id"; 
}else{ } 



foreach ($dbo->query($quer2) as $noticia2) { 
if($noticia2['ward_id']==@$cat){ 
echo "Turnout: <input type='text' id='result' name='turnout' style='width:35px' value='$noticia2[turnout]'> Percentage: <input type='text' id='result' name='percentage' style='width:35px' value='$noticia2[percentage]'>%<br><br>"; 
echo "Candidates: <br>"; 

$conn = mysql_connect($dbhost_name, $username, $password); 
mysql_select_db('raynerco_election'); 
function mysql_query_or_die($query) { 
$result = mysql_query($query); 
if ($result) 
return $result; 
else { 
$err = mysql_error(); 
die("<br>{$query}<br>*** {$err} ***<br>"); 
} 
} 


$query="SELECT DISTINCT * FROM candidate where ward_id=$cat order by candidate_id"; 
$result = mysql_query_or_die($query); 

echo("<table>"); 
echo '<tr>'; 
echo "<th></th>"; 
echo "<th>Candidate:</th>"; 
echo "<th>Results:</th>"; 
echo '</tr>'; 
while ($rows = mysql_fetch_assoc($result)) { 
echo '<tr>'; 

echo "<td><input type='text' name='candidate_id' value='$rows[candidate_id]'></td>"; 
echo "<td><input type='text' name='candidate' value='$rows[candidate]'></td>"; 
echo "<td><input type='text' name='result' value='$rows[result]'></td>"; 
// } 

echo '</tr>'; 
} 
echo("</table>"); 

echo "<br><input name='update' type='submit' value='Update'>"; 
echo "</form>"; 

} 
else{ 

} 
} 


?> 

<?php 


$ward_id = $_POST['ward_id']; 
$turnout = $_POST['turnout']; 
$percentage = $_POST['percentage']; 
$candidate_id = $_POST['candidate_id']; 
$candidate = $_POST['candidate']; 
$result = $_POST['result']; 


if(isset($_POST['update'])) 
{ 


$conn = mysql_connect($dbhost_name, $username, $password); 

$sql = "UPDATE ward SET turnout = '$turnout', percentage = '$percentage' WHERE ward_id = '$cat'"; 
$sql1 = "UPDATE candidate SET candidate = '$candidate', result = '$result' WHERE candidate_id = 'candidate_id' "; 
mysql_select_db('raynerco_election'); 
$retval = mysql_query( $sql, $conn ); 

if(! $retval ) 
{ 
die('Could not update data: ' . mysql_error()); 
} 
header( "Location: index.php?cat=$cat&updated" ); die; 
} 
if (isset($_GET['updated'])) { 
echo "Data updated successfuly"; 

} 


?> 


<br><br> 
<a href=index.php>Start again</a> 
<br><br> 
</body> 

</html>

0 个答案:

没有答案