我再次必须处理 PHP 和 MySQL :) 所以,我有一个 mysql 表并希望有“添加”功能。所以我可以轻松地从浏览器编辑我的表格。到目前为止,我几乎完成了所有工作,但问题是包含我的表(或 id)中的“数字”的变量不会 _POST 到其他页面。它不会直接从 textarea _POST 或者即使我把它放在一个隐藏的字段中(嗯,刚刚明白这几乎是一样的)。
让我给你看一些例子:
$a = mysql_query("SELECT number FROM peoples ORDER BY number DESC LIMIT 1");
$number_a = mysql_fetch_assoc($a);
$number = $number_a['number']+1;`
这就是我得到这个变量的方式。
echo '<input name="id" type="hidden" value="'.$number.'" />';
我就是这样通过的。
echo $number = mysql_escape_string( $_POST['id'] );
这就是我试图在其他 php 文件中获取它的方式。
来自其他文本区域的所有内容都可以通过。
根据要求提供完整代码。添加.PHP:
<?php
$dblocation = "127.0.0.1";
$dbname = "tvp";
$dbuser = "root";
$dbpasswd = "";
$dbcnx = @mysql_connect($dblocation,$dbuser,$dbpasswd);
if (!$dbcnx)
{
echo( "<P>В настоящий момент сервер базы данных не доступен, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
if (!@mysql_select_db($dbname, $dbcnx))
{
echo( "<P>В настоящий момент база данных не доступна, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
echo '<form name="editform" action="adder.php" method="POST">';
echo '<table>';
echo '<tr>';
echo '<td>Номер</td>';
$a = mysql_query("SELECT number FROM peoples ORDER BY number DESC LIMIT 1");
$number_a = mysql_fetch_assoc($a);
$number = $number_a['number']+1;
echo $number;
var_dump($number);
print_r($number);
echo '<td><textarea name="number" >'.$number.'</textarea></td>';
echo '<input name="id" type="hidden" value="'.$number.'" />';
echo '</tr>';
echo '<tr>';
echo '<td>Имя</td>';
echo '<td><textarea name="givenName">'.$man['givenName'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Инициалы</td>';
echo '<td><textarea name="middleInitial">'.$man['middleInitial'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Фамилия</td>';
echo '<td><textarea name="surname">'.$man['surname'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Пол</td>';
echo '<td> <input type="radio" name="gender" value=1 >Man<Br>
<input type="radio" name="browser" value=0>Woman<Br> </td>';
echo '</tr>';
echo '<tr>';
echo '<td>Город</td>';
echo '<td><textarea name="city">'.$man['city'].'</textarea></td>';
echo '</tr>';
echo '<input name="id" type="hidden" value="'.$id.'" />';
echo '<input name="statee" type="hidden" value="'.$man['state'].'" />';
echo '<tr>';
echo '<td>Штат</td>';
?>
<td><select size="3" name="state">
<option disabled>Выберите штат</option>
<option value="AL"
<?php
if($man['state']=="AL"){
echo "selected";
}?>
>Alabama
</option>
//...and so on...
</select></td>
<?php
echo '</tr>';
echo '<tr>';
echo '<td>Телефон</td>';
echo '<td><textarea name="telephone">'.$man['telephone'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>E-mail</td>';
echo '<td><textarea name="emailAddress">'.$man['emailAddress'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Дата</td>';
echo '<td><textarea name="birthday">'.$man['birthday'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Место работы</td>';
echo '<td><textarea name="occupation">'.$man['occupation'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Компания</td>';
echo '<td><textarea name="company">'.$man['company'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Вес</td>';
echo '<td><textarea name="weight">'.$man['weight'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Рост</td>';
echo '<td><textarea name="length">'.$man['length'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Адрес</td>';
echo '<td><textarea name="streetAddress">'.$man['streetAddress'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Почтовый индекс</td>';
echo '<td><textarea name="zipCode">'.$man['zipCode'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Страна</td>';
echo '<td><textarea name="country">'.$man['country'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" value="Сохранить"></td>';
echo '<td><button type="button" onClick="history.back();">Отменить</button></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
?>
和加法器.PHP:
<?php
$dblocation = "127.0.0.1";
$dbname = "tvp";
$dbuser = "root";
$dbpasswd = "";
$dbcnx = @mysql_connect($dblocation,$dbuser,$dbpasswd);
if (!$dbcnx)
{
echo( "<P>В настоящий момент сервер базы данных не доступен, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
if (!@mysql_select_db($dbname, $dbcnx))
{
echo( "<P>В настоящий момент база данных не доступна, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
$number=$_POST["id"]; echo '<br>';
var_dump($_POST['id']);
print_r($POST['id']);
echo $number;
echo $givenName = mysql_escape_string( $_POST['givenName'] ); echo '<br>';
echo $middleInitial = mysql_escape_string( $_POST['middleInitial'] ); echo '<br>';
echo $surname = mysql_escape_string( $_POST['surname'] ); echo '<br>';
echo $gender = $_POST['gender'] ; echo '<br>';
echo $city = mysql_escape_string( $_POST['city'] ); echo '<br>';
echo $state = mysql_escape_string( $_POST['state'] );echo '<br>';
echo $emailAddress = mysql_escape_string( $_POST['emailAddress'] ); echo '<br>';
echo $telephone = mysql_escape_string( $_POST['telephone'] ); echo '<br>';
echo $birthday = mysql_escape_string( $_POST['birthday'] ); echo '<br>';
echo $occupation = mysql_escape_string( $_POST['occupation'] );echo '<br>';
echo $company = mysql_escape_string( $_POST['company'] ); echo '<br>';
echo $weight = mysql_escape_string( $_POST['weight'] ); echo '<br>';
echo $length = mysql_escape_string( $_POST['length'] ); echo '<br>';
echo $streetAddress = mysql_escape_string( $_POST['streetAddress'] ); echo '<br>';
echo $zipCode = mysql_escape_string( $_POST['zipCode'] ); echo '<br>';
echo $country = mysql_escape_string( $_POST['country'] ); echo '<br>';
$query = "INSERT INTO peoples (number,givenName, middleInitial, surname, gender, city, state, emailAddress, telephone, birthday, occupation, company, weight, length, streetAddress, zipCode, country) VALUES ( '".$number."', '".$givenName."', '".$middleInitial."', '".$surname."', '".$gender."', '".$city."', '".$state."', '".$emailAddress."', '".$telephone."', '".$birthday."', '".$occupation."', '".$company."', '".$weight."', '".$length."', '".$streetAddress."', '".$zipCode."', '".$country."');";
mysql_query ( $query );
?>
提前致谢!