我真的希望你能帮助我解决我的问题。我已经完成了我的表格,所有按钮都在工作,一切都很好,但今天我决定在我的表格中添加一个额外的列。列称为“国家”。现在,我所有的按钮都还在工作,添加或更新数据没有问题,除了那一列。那里什么都没有,但我可以毫不费力地从该列中检索数据。不确定我在哪里搞砸了。这是我第一次接触php和mysql,所以如果我犯了一些愚蠢的错误,请原谅我。你能帮我吗???
<?php
//adding functions
function initVars() {
//declaring variables
global $ID, $First_name, $Last_name, $Address,$Country,
$Ph_number,$add,$find,$reset, $delete,
$update, $next, $previous;
$ID="";
$First_name = "";
$Last_name="";
$Address = "";
$Country="";
$Ph_number="";
$add = "";
$find = "";
$reset="";
$delete="";
$update="";
$next="";
$previous="";
}
initVars();
function getRecord($query,$resultOffset){
global $ID, $First_name, $Last_name, $Address,$Country, $Ph_number;
$result = mysql_query($query);
$resultCount = mysql_num_rows($result);
echo "FOUND ", $resultCount, " RESULTS";//3
$r=0;
while ($r <= $resultOffset) {
$rec = mysql_fetch_assoc($result);
$r = $r + 1;
}
$ID=$rec['ID'];
$First_name = $rec['First_name'];
$Last_name = $rec['Last_name'];
$Address = $rec['Address'];
$Country= $rec['Country'];
$Ph_number = $rec['Ph_number'];
return($resultCount);
}
// collecting form input
if ( isset($_POST['First_name'])) $First_name = $_POST['First_name'];
if ( isset($_POST['Last_name'])) $Last_name = $_POST['Last_name'];
if ( isset($_POST['Address'])) $Address = $_POST['Address'];
if ( isset($_POST['Country'])) $$Country = $_POST['Country'];
if ( isset($_POST['Ph_number'])) $Ph_number = $_POST['Ph_number'];
if ( isset($_POST['add'])) $add = $_POST['add'];
if ( isset($_POST['find'])) $find = $_POST['find'];
if ( isset($_POST['reset'])) $reset = $_POST['reset'];
if ( isset($_POST['delete'])) $delete = $_POST['delete'];
if ( isset($_POST['update'])) $update = $_POST['update'];
if ( isset($_POST['ID'])) $ID = $_POST['ID'];
if ( isset($_POST['next'])) $next = $_POST['next'];
if ( isset($_POST['previous'])) $previous = $_POST['previous'];
if ( isset($_POST['resultOffset'])) $resultOffset = $_POST['resultOffset'];
if ( isset($_POST['resultCount'])) $resultCount = $_POST['resultCount'];
if ( isset($_POST['query'])) $query = $_POST['query'];
// connecting to database
$connection = mysql_connect('localhost', 'root', '*******')
or die('Connection to MySql server failed');
$db = mysql_select_db('myaddressbook', $connection)
or die('Cannot connect');
//////////////////////////////////////////////////////////////////////////////
//BUTTONS
if ( $add ) {
if($First_name && $Last_name) {
echo 'Adding to database';
// adding records to database
$ins = mysql_query("INSERT INTO people SET First_name='$First_name',
Last_name='$Last_name',
Address='$Address', Country='$Country',Ph_number='$Ph_number'");
if($ins) echo "Your address book was updated succesfully";
else echo "Adding data failed";
initVars();
}
else echo 'First and last name needed';
}
////////////////////////////////////////////////////////////////////////
if ( $find ) {
if($First_name || $Last_name) {
$query = " SELECT ID, First_name, Last_name, Address , Ph_number FROM people WHERE
First_name LIKE '%{$First_name}%'";
$result = mysql_query($query);//1
$resultCount = mysql_num_rows($result);//2
$resultOffset= 0;
echo "FOUND ", $resultCount, " RESULTS";//3
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$ID=$rows['ID'];
$First_name = $rows['First_name'];
$Last_name = $rows['Last_name'];
$Address = $rows['Address'];
$Country= $rows['Country'];
$Ph_number = $rows['Ph_number'];
}
else echo "Enter required field";
}
/////////////////////////////////////////////////////////////////
if ($delete){
mysql_query(" DELETE FROM people WHERE ID = '{$ID}' ");
echo('Record deleted');
}
//////////////////////////////////////////////////////////////////////
if ($update){
mysql_query ("UPDATE people
SET First_name='$First_name',
Last_name='$Last_name',Address='$Address',Country='$Country', Ph_number='$Ph_number'
WHERE ID = '{$ID}'");
echo ('Update succesfull');
}
/////////////////////////////////////////////////////
if ($reset){
initVars();
}
/////////////////////////////////////////////////////////////
if($next){
if ($resultOffset < $resultCount - 1){
$resultOffset= $resultOffset + 1;
$resultCount=getRecord($query,$resultOffset);
}
echo "next ", $resultOffset, " of ", $resultCount;
}
if ($previous){
if ($resultOffset > 0){
$resultOffset= $resultOffset - 1;
$resultCount=getRecord($query,$resultOffset);
}
echo "prev ", $resultOffset, " of ", $resultCount;
}
mysql_close($connection);
?>
And my piece of HTML:
<html>
<head><h1 align="center">
My Address Book </h1>
<hr></br></br></br>
</head>
<body style="color: #000; background-color:#e0e2e6; background-image:url(C:/Users/Marta
/Desktop/old.jpg);">
<form action="people.php" method="post">
<table "width="500"; border="0"; align="center">
<tr>
<th>ID:</th>
<td><label for="textfield"></label>
<input type="text"name="ID" size="50" value="<?php echo $ID ?>"/></td>
<tr><th>First Name:</th>
<td><input type="text" name="First_name" size="50" value="<?php echo $First_name ?>"
/></td></tr>
<tr><th>Last Name:</th>
<td><input type="text" name="Last_name" size="50" value="<?php echo $Last_name ?>"
/></td></tr>
<tr><th>Address:</th>
<td><input type="text" name="Address" size="50" value="<?php echo $Address ?>" /></td>
</tr>
<tr><th>Country:</th>
<td><input type="text" name="Country" size="50" value="<?php echo $Country ?>" /></td>
</tr>
<tr><th>Ph number:</th>
<td><input type="text" name="Ph_number" size="50" value="<?php echo $Ph_number ?>"
/></td></tr></table>
<hr>
<table align="center">
<tr><td>
<td><input type="submit" name="find" value="Find" /></td>
<td><input type="submit" name="add" value="Add" /></td>
<td><input type="submit" name="delete" value="Delete"/></td>
<td><input type="submit" name="update" value="Update"/></td>
<td><input type="submit" name="previous" value="<< Previous" /></td>
<td><input type="submit" name="next" value="Next >>" /></td>
<td align="center" colspan="3"><input type="submit" name="reset" value="Reset"/>
</td></tr></table>
<input type="hidden" id="resultOffset" name="resultOffset" value="<?php echo
$resultOffset ?>">
<input type="hidden" id="resultCount" name="resultCount" value="<?php echo
$resultCount ?>">
<input type="hidden" id="query" name="query" value="<?php echo $query ?>">
</form>
</body>
</html>