唯一的int变量不会_POST

时间:2013-05-09 03:39:07

标签: php mysql variables post

我再次处理PHP和MySQL :)所以,我有一个mysql表,想要“添加”功能。所以我可以轻松地从浏览器编辑我的表格。到目前为止,我已经做了几乎所有事情,但问题是我的表(或id)中包含“number”的变量不会_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文件中获取它的方式。

其他textareas的一切都很顺利。

根据要求提供完整代码。 ADD.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>'; 

  ?>

和ADDER.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 );

    ?>

提前致谢!

3 个答案:

答案 0 :(得分:1)

首先要做的事情。确保你将数字回显到屏幕上。你写的代码看起来是正确的,但它只是一个片段。

在分配号码后添加var_dump($ number),看看它是否显示了号码。如果它不是那么你的SQL有问题

答案 1 :(得分:0)

你是echo ing!

echo $number = mysql_escape_string( $_POST['id'] );

用以下内容改变:

$number = mysql_escape_string( $_POST['id'] );
echo $number;

如果你想echo它。

答案 2 :(得分:0)

问题是您在表单底部重新分配隐藏字段ID

这里

  echo '<td><textarea name="city">'.$man['city'].'</textarea></td>'; 
  echo '</tr>';     
    echo '<input name="id" type="hidden" value="'.$id.'"  />';     <------- HERE
    echo '<input name="statee" type="hidden" value="'.$man['state'].'"  />';
  echo '<tr>'; 
  echo '<td>Штат</td>';

我假设您从未将$ id分配给数字,因此,在您的表单顶部,您将隐藏的字段ID分配给“$ number”,但在表单的底部,您将隐藏的字段ID分配给$ id 。从表单顶部覆盖原始id字段