這是我最新的問題。我有佈局和一切工作,但現在我想弄清楚這部分的PHP一面。我想要做的是我如何具體說哪個單選按鈕使用PHP檢查?我嘗試使用$ _POST ['ins'](其中'ins'是插入按鈕的ID),但沒有奏效。我希望它能夠在插入按鈕被選中時執行,做任何步驟,但我不知道如何獲取它,所以說插入按鈕被選中/檢查。識別哪個單選按鈕被檢查並使用php
這裏是我的代碼
<html>
<head><title></title>
<script type="text/javascript">
function show()
{
if (document.getElementById('ins').checked){
document.getElementById("p1").style.display = "block"
document.getElementById("zn").style.display = "block";
document.getElementById("p2").style.display = "block"
document.getElementById("gpa").style.display = "block";
document.getElementById("p3").style.display = "none"
document.getElementById("ngpa").style.display = "none";
}
else if (document.getElementById('upd').checked){
document.getElementById("p1").style.display = "block"
document.getElementById("zn").style.display = "block";
document.getElementById("p2").style.display = "none"
document.getElementById("gpa").style.display = "none";
document.getElementById("p3").style.display = "block"
document.getElementById("ngpa").style.display = "block";
}
else {
document.getElementById("p1").style.display = "block"
document.getElementById("zn").style.display = "block";
document.getElementById("p2").style.display = "none"
document.getElementById("gpa").style.display = "none";
document.getElementById("p3").style.display = "none"
document.getElementById("ngpa").style.display = "none";
}
}
function check()
{
if((document.getElementById("zn").value == "") || (document.getElementById("gpa").value == "") ||
(isNaN(parseFloat(document.getElementById("gpa").value))))
{
alert("Please complete both fields and check to see if the GPA entered is a number!");
return false;
}
return true;
}
</script>
</head>
<body>
<form action = "index.php" onsubmit="return check();">
<input type="radio" name="group" id = "ins" onclick ="show()"/> Insert Student <br />
<input type="radio" name="group" id = "upd" onclick ="show()"/> Update Student <br />
<input type="radio" name="group" id = "del" onclick ="show()"/> Delete Student <br />
<p id="p1" style="display:none">Enter Z-number: <br /></p>
<input type='text' name = 'z' id ="zn" maxlength='9' style="display:none"/>
<p id="p2" style="display:none"><br />Enter GPA: <br /></p>
<input type='text' name='gpa' id ="gpa" style="display:none"/>
<p id="p3" style="display:none"><br /> Enter new GPA: <br /></p>
<input type="text" name ="ngpa" id ="ngpa" style="display:none"/>
<br /><input type='submit' value='Submit request' name='submit'/>
</form>
<?php
if (isset($_POST['z'])) {
$con = mysql_connect('localhost', '*****', '***********');
if ($con) {
mysql_select_db('ghamzal', $con);
$a = $_POST['z'];
$b = $_POST['gpa'];
$sql = "INSERT INTO Students VALUES (' $a ' , '$b')";
if (mysql_query($sql, $con)) echo 'Operation succeeded';
else echo 'Not succeeded ' .mysql_error();
if (isset($_POST['ins'])) {
$sql = "SELECT * FROM Students";
$res = mysql_query($sql, $con);
echo "<table border ='1'>";
echo "<tr><th>Znum</th><th>GPA</th></tr>";
while ($r = mysql_fetch_array($res)) {
echo "<tr><td>".$r['Znum']."</td><td>".$r['gpa']. "</td></tr>";
}
echo "</table>";
}
mysql_close($con);
}
else {
echo 'Insertion failed'. 'Could not connect to DB.';}
}
?>
</body>
</html>
您的代碼易受SQL注入攻擊。你真的**應該使用準備好的語句,將你的變量作爲參數傳遞給你,這些參數不會被SQL評估。如果你不知道我在說什麼,或者如何去做,請閱讀[Bobby Tables](http://bobby-tables.com)。 – eggyal 2012-04-28 15:54:02
您是否試過在Google搜索[PHP單選按鈕](http://www.google.com/search?q=PHP+radio+button)時出現的任何教程? – eggyal 2012-04-28 15:56:28