話說Preare是什麼,
其實如果執行有問題的話,
應該會有錯誤訊息,
要不然就是用try catch包住,
自己記錯誤的Log也是可以.
而且你根本沒判斷有沒有成功都回傳成功嗎??
另外現在mysqli應該快淘汰了,
如果剛學其實可以直接學PDO了.
雖然說問題可能不在這
不過怎麼會先組sql語句後
才 init variable
有可能是你組的sql語句有問題
想辦法把它印出來
也有可能是 post 進來的 fname 有問題
把 code 補完整一點會比較有幫助
然後可以的話
code的部分別貼圖片
update3
<?php
// connect to my sql
// Connect to server and select databse.
$conn = mysqli_connect("localhost","abook","abook","abook");
if (mysqli_connect_error()) {
die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";
// retrieve all variables
$fname = $_POST['fname'];
$size = count($fname);
for($i = 0;$i < $size; $i++){
$g = $conn->preare("update contacts set email = ?,homePhone = ?,cellPhone = ?,officePhone = ?,address = ?,comment = ? where lname = ? and fname = ?");
$g->bind_param('ssssssss',$email[$i],$homePhone[$i],$cellPhone[$i],$officePhone[$i],$address[$i],$comment[$i],$lname[$i],$fname[$i]);
$email=$_POST['email'];
$homePhone=$_POST['homePhone'];
$cellPhone=$_POST['cellPhone'];
$officePhone=$_POST['officePhone'];
$address=$_POST['address'];
$comment=$_POST['comment'];
$lname=$_POST['lname'];
$fname=$_POST['fname'];
$g->execute();
$g->close();
}
}
// number of contacts to update
/*
$ufname = $_POST['ufname'];
$ulname = $_POST['ulname'];
$checkbox = $_POST['checkbox'];
*/
// update information to database
// Close connection
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<BODY background="http://localhost/hw10/gifs/bkg2.gif" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<p> </p>
<p> </p>
<p> </p>
<blockquote>
<p>
<h3>Your information is updated. </h3>
<body>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
update2.php也需要唷
有可能是你form沒有寫好
然後像魚大說的
你的 preare 是不是拼成 preare 了
建議照這篇文章說的
把 php.ini 的 display_errors 打開
然後你的
<h3>Your information is updated. </h3>
在這其實沒有太大的作用
無論成功失敗都會秀出來
update2
preaare已經改成prepare了
我只有打<?php - ?>
裡面,其他都是老師的程式碼
<?php
// connect to my sql
// Connect to server and select databse.
// get all parsed variables
$conn = mysqli_connect("localhost","abook","abook","abook");
if (mysqli_connect_error()) {
die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";
$g = $conn->prepare("select * from contacts");
$g->execute();
$result = $g->get_result();
// show all contact information
$ufname = $_POST['ufname'];
$ulname = $_POST['ulname'];
$checkbox = $_POST['checkbox'];
// close connection
$g->close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
</head>
<BODY background="http://tinman.cs.gsu.edu/~raj/gifs/bkg2.gif" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<SCRIPT language=JavaScript type=text/javascript>
function validate_form(){
var message ="";
var total = document.getElementsByName("ufname[]").length;
for (i = 0; i<total; i++){
mssg ="";
fname = document.getElementsByName("ufname[]")[i].value;
lname = document.getElementsByName("ulname[]")[i].value;
email = document.getElementsByName("email[]")[i].value;
homePhone = document.getElementsByName("homePhone[]")[i].value;
cellPhone = document.getElementsByName("cellPhone[]")[i].value;
officePhone = document.getElementsByName("officePhone[]")[i].value;
address = document.getElementsByName("address[]")[i].value;
mssg += checkEmail(email);
mssg += checkPhone("Home phone", homePhone);
mssg += checkPhone("Cell phone", cellPhone);
mssg += checkPhone("Office phone", officePhone);
mssg += checkAlphabet("Address", address);
if (mssg != "")
message += (lname + ", " + fname + ": \n" + mssg + "\n");
}
if (message == ""){
return true;
} else {
alert(message);
return false;
}
}
function checkEmail(itemcontent){
var emailregexp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
if (!itemcontent.match(/[a-zA-Z]+/)){
return " - E-mail address is required!\n";
} else if (itemcontent.match(emailregexp)){
return "";
} else {
return " - Invalid E-mail Address!\n";
}
}
function checkAlphabet(field, itemcontent){
var nameregexp = /[a-zA-Z]+/;
if (!itemcontent.match(nameregexp)){
return " - "+field+" is required!\n";
}
return "";
}
function checkPhone(field, itemcontent){
var phoneregexp1 = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
var phoneregexp2 = /^[0-9]{2}-[0-9]{4}-[0-9]{4}$/;
var phoneregexp3 = /^[0-9]{4}-[0-9]{3}-[0-9]{3}$/;
if ((field =="Home phone") && (itemcontent=="")){
return " - "+field+" is required!\n";
} else if ((itemcontent!="") &&
!(itemcontent.match(phoneregexp1) || itemcontent.match(phoneregexp2) || itemcontent.match(phoneregexp3) ) ){
return " - "+field+" is in incorrect format!\n";
} else{
return "";
}
}
</SCRIPT>
<blockquote>
<p>
<h2>Update Contact Information</h2>
<form name="updateform" action="update3.php" method=post onSubmit="return validate_form()">
<?php
foreach ($checkbox as $index){
for ($i = 0; $i < mysqli_num_rows($result); $i++){
$row = mysqli_fetch_assoc($result);
$fname = $row["fname"];
$lname = $row["lname"];
if (($fname == $ufname[$index]) && ($lname == $ulname[$index])){
$email = $row["email"];
$homePhone = $row["homePhone"];
$cellPhone = $row["cellPhone"];
$officePhone = $row["officePhone"];
$address = $row["address"];
$comment = $row["comment"];
echo "<table width=\"40%\" border=\"0\" cellpadding=\"2\" cellspacing=\"5\">";
echo "<tr>
<td width=\"130\"><strong>First Name : <span class=\"style1\">*</span></strong></td>
<td><input name=\"fname[]\" disabled type=\"text\" size=\"30\" maxlength=\"30\" value=\"$fname\" />
<input name=\"ufname[]\" type=\"hidden\" value=\"$fname\" />
</td>
</tr>";
echo "<tr>
<td width=\"130\"><strong>Last Name :</strong> <span class=\"style1\">*</span></td>
<td><input name=\"lname[]\" disabled type=\"text\" size=\"30\" maxlength=\"30\" value=\"$lname\" /></td>
<input name=\"ulname[]\" type=\"hidden\" value=\"$lname\" /></td>
</tr>";
echo "<tr>
<td width=\"130\"><strong>E-mail Address :</strong> <span class=\"style1\">*</span></td>
<td><input name=\"email[]\" type=\"text\" size=\"30\" maxlength=\"30\" value=\"$email\" /></td>
</tr>
<tr>
<td width=\"130\"><p><strong>Home Phone :</strong> <span class=\"style1\">*</span><br />
xxx-xxx-xxxx</p> </td>
<td><input name=\"homePhone[]\" type=\"text\" size=\"30\" maxlength=\"20\" value=\"$homePhone\" /></td>
</tr>";
echo "<tr>
<td width=\"130\"><strong>Cell Phone : <br />
</strong>xxx-xxx-xxxx</td>
<td><input name=\"cellPhone[]\" type=\"text\" size=\"30\" maxlength=\"20\" value=\"$cellPhone\" /></td>
</tr>";
echo "<tr>
<td width=\"130\"><strong>Office Phone : <br />
</strong>xxx-xxx-xxxx</td>
<td><input name=\"officePhone[]\" type=\"text\" size=\"30\" maxlength=\"20\" value=\"$officePhone\" /></td>
</tr>";
echo "<tr valign=\"top\">
<td width=\"130\"><strong>Address :</strong> <span class=\"style1\">*</span></td>
<td><textarea name=\"address[]\" cols=\"25\" rows=\"3\" >$address</textarea></td>
</tr>";
echo "<tr valign=\"top\">
<td width=\"130\"><strong>Comment :</strong> </td>
<td><textarea name=\"comment[]\" cols=\"25\" rows=\"2\" >$comment</textarea></td>
</tr>";
echo "</table>
<hr align=\"left\" width=\"40%\" />
<br />";
}
}
}
?>
<blockquote>
<p><input type="submit" name="Submit" value="Update Contact" />
</p>
</blockquote>
</form>
</p>
</blockquote>
</body>
</html>
update1
<?php
// connect to my sql
// Connect to server and select databse.
$conn = mysqli_connect("localhost","abook","abook","abook");
if (mysqli_connect_error()) {
die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";
$g = $conn->prepare("select * from contacts");
$g->execute();
$result = $g->get_result();
$g->close();
// show all contact information
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<BODY background="gifs/bkg2.gif" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<SCRIPT language=JavaScript type=text/javascript>
<!-- function to check whether checkbox is selected -->
function checkboxchecked(){
element = document.getElementsByName("checkbox[]")
for (var index=0; index< element.length; index++){
if (element[index].checked == true)
return true;
}
alert("Please select name to be deleted");
return false;
}
</SCRIPT>
<p> </p>
<p> </p>
<p> </p>
<blockquote>
<p>
<h2>Update Contact Information</h2>
<form name="myform" method="post" action="update2.php" onSubmit="return checkboxchecked()">
<table width="40%" border="0">
<?php
$index=0;
while($row = mysqli_fetch_assoc($result)) {
$lname = $row['lname'];
$fname = $row['fname'];
echo "<tr><td width=\"25\" valign=\"top\"><input type=\"checkbox\" name=\"checkbox[]\" value=\"$index\" />";
echo "</td><td valign=\"bottom\"><h4><a href=\"detail.php?lname=$lname&fname=$fname\">$lname, $fname</a><h4></td></tr>";
echo "<input type=\"hidden\" name=\"ufname[]\" value=\"$fname\" />";
echo "<input type=\"hidden\" name=\"ulname[]\" value=\"$lname\" />";
$index++;
}
echo "<tr><td width=\"25\" valign=\"top\"> </td><td valign=\"bottom\"><input type=\"submit\" name=\"update\"";
echo "value=\"Update\" /> <input type=\"reset\" name=\"Submit2\" value=\"Clear\" /></td></tr>";
?>
</table>
</form>
<?php
if (mysqli_num_rows($result)==0)
echo "<h4>No data<h4>";
?>
</p>
</blockquote>
</body>
</html>
你這有兩個問題
1 .
看起來你是想要有勾選多個可以同時編輯多個的功能
但實際上不管勾選幾個都只會出現第一個
因為你的update2迴圈有問題
改成如下才能正常
// update2.php
...
for ($i = 0; $i < mysqli_num_rows($result); $i++){
foreach ($checkbox as $index){
...
}
}
2 .
無法更新的原因是因為
你 post 到 update3 的 data
根本就沒有 lname 跟 fname
只有 ufname 跟 ulname
然後
後 init variable
的確會影響
改法如下
// update3.php
...
$fname = $_POST['ufname'];
...
$lname=$_POST['ulname'];
$fname=$_POST['ufname'];
$g = $conn->prepare("update contacts set email = ?,homePhone = ?,cellPhone = ?,officePhone = ?,address = ?,comment = ? where lname = ? and fname = ?");
$g->bind_param('ssssssss',$email[$i],$homePhone[$i],$cellPhone[$i],$officePhone[$i],$address[$i],$comment[$i],$lname[$i],$fname[$i]);
$g->execute();
$g->close();
...
這樣就能正常執行了
現在會變成我email以下的東西會空白
我沒有改你 detail.php 的東西呀
我甚至沒有你的 detail.php 的 code
我上面說的修改
只有貼上需要改的部分
不是把那個檔案改成只剩那樣喔
話說你的錯誤訊息有打開了嗎