无法找出问题所在...例如我想添加2行数据...当我按下+按钮时...我输入的数据将会消失(看起来像刷新)......数据库只存储第二行数据...第一行不存储。请帮我找出答案。谢谢〜
<?php if($_POST['btnPlus1'])
$_SESSION['count1'] += 1;
else if($_POST['btnMinus1'])
$_SESSION['count1'] -= 1;
$AddEducationalQ = "INSERT INTO tbleducational(Id,University,Level,Specialization,Year) VALUES('".$_POST['txtStaffIc']."','".strtoupper($_POST['txtUniversity'])."','".strtoupper($_POST['sLevel'])."','".strtoupper($_POST['txtSpecialization'])."','".$_POST['txtYear']."')";
$AddEducationalResult = mysql_query($AddEducationalQ,$link); ?>
<tr>
<td>
<fieldset>
<legend>Educational Background</legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<?php
for($tempfield = 1; $tempfield <= $_SESSION['count1']; ++$tempfield)
{?>
<fieldset>
<legend><?php echo $tempfield ?></legend>
<table width="200" border="0">
<tr>
<td>University</td>
<td>Level</td>
<td>Specialization</td>
<td>Year Graduated</td>
</tr>
<tr>
<td>
<input type="text" name="txtUniversity" id="txtUniversity" /></td>
<td>
<select name="sLevel" id="sLevel">
<option></option>
<option>Diploma</option>
<option>Degree</option>
<option>Master</option>
<option>Doctor</option>
</select>
</td>
<td>
<input type="text" name="txtSpecialization" id="txtSpecialization" />
</td>
<td>
<input type="text" name="txtYear" id="txtYear" />
</td>
</tr>
</table>
</fieldset>
<?php
}?>
</td>
</tr>
<tr>
<td colspan="4" align="center"><input type="submit" name="btnPlus1" id="btnPlus1" value="+" /> <input type="submit" name="btnMinus1" id="btnMinus1" value="-" /></td>
</tr>
</table>
</fieldset>
</td>
</tr>
答案 0 :(得分:0)
好的,首先您必须将表单元素更改为:
<form method="post" action="filename.php" accept-charset="utf-8">
确保将filename.php更改为此代码的文件名。
现在你必须做出选择。您可以继续使用mysql_ *,这已被弃用并且非常不安全!或者,您可以将代码升级为PDO()。
如果您希望继续使用mysql_ *,请将您的PHP部分更改为:
if(isset($_POST['txtStaffIc'], $_POST['txtUniversity'], $_POST['sLevel'], $_POST['txtSpecialization'], $_POST['txtYear'])){
$AddEducationalQ = "INSERT INTO tbleducational(Id,University,Level,Specialization,Year) VALUES('".$_POST['txtStaffIc']."','".strtoupper($_POST['txtUniversity'])."','".strtoupper($_POST['sLevel'])."','".strtoupper($_POST['txtSpecialization'])."','".$_POST['txtYear']."')";
mysql_query($AddEducationalQ,$link) or die(mysql_error());
echo "Data succesfully added to database.";
}
如果您希望升级到PDO(),请将您的PHP部分更改为:
if(isset($_POST['txtStaffIc'], $_POST['txtUniversity'], $_POST['sLevel'], $_POST['txtSpecialization'], $_POST['txtYear'])){
$AddEducationalQ = "INSERT INTO tbleducational(Id,University,Level,Specialization,Year) VALUES(':txtStaffIc',':txtUniversity',':sLevel',':txtSpecialization',':txtYear')";
$prepare = $pdo->prepare($AddEducationalQ);
$prepare->bindValue(":txtStaffIc",$_POST['txtStaffIc']);
$prepare->bindValue(":txtUniversity",strtoupper($_POST['txtUniversity']));
$prepare->bindValue(":sLevel",strtoupper($_POST['sLevel']));
$prepare->bindValue(":txtSpecialization",strtoupper($_POST['txtSpecialization']));
$prepare->bindValue(":txtYear",$_POST['txtYear']);
if($prepare->execute()){
echo "Data succesfully added to database.";
} else {
print_r($prepare->errorInfo());
}
}
还要确保将数据库连接文件更改为:
<?php
$dbhost = ""; //Enter MySQL server host
$dbuser = ""; //Enter MySQL database user
$dbpass = ""; //Enter MySQL database pass
$dbname = ""; //Enter MySQL database name
$pdo = new PDO("mysql:host=".$dbhost.";dbname=". $dbname, $dbuser, $dbpass);
?>
最后一点我应该补充一点,我在PHP代码中没有看到任何require数据库连接文件。我认为你根本就没有发布这个。