0

我的脚本需要你的帮助。我正在尝试将值插入 mysql,同时输入 $row['senatorial'] 和 $constituencys。下面的 coe 是我正在努力工作,但它不起作用。我在屏幕上没有显示任何错误,也没有任何内容输入到数据库中。请问我哪里错了

<?php

if (isset($_POST['action']) && $_POST['action'] == "submit") {

  $state  = mysql_real_escape_string(trim($_POST['state']));

  $query = mysql_query("SELECT senatorial              
                    FROM state
                    WHERE state = '".$state."'") or die (mysql_error());

  $duplicates = mysql_num_rows($query);

  if (isset($_POST['constituency']) && $_POST['action'] == "create") {


    $constituencys = $_POST['constituency'];
    foreach($constituencys as $constituency) {
      $query = "
                    INSERT INTO  `state` (
                    `state_id`,
                    `state`,
                    `senatorial`
                    `constituency`
                    ) VALUES (
                    NULL,
                    '{$state}',
                    '{$row['senatorial']}',
                    '" . mysql_real_escape_string(trim($constituency)) . "'
                    )
                    ";
      mysql_query($query) or die (mysql_error());

    }
  } ?>

<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
  <?php

  while($row=mysql_fetch_assoc($query)){
    echo strtoupper($row['senatorial']) ;?>
    <input type='text' name='constituency[]' title='Federal Constituency' />
    <?php  } ;?>
  <input type="hidden" name="create" value="create" />
  <input type='submit' name='create' value='Create' />
</form>
<?php } ?>

<h2>Select State To show Senatorial Districts</h2>
<form  action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
  State:<select name="state" title='State'  class='OpenInput_Select'>
  <option value ="">    </option>
  <option value ="state1">state1</option></select>
  <input type="hidden" name="action" value="submit" />
  <input type='submit' name='submit' value='SHOW' />
</form>
4

2 回答 2

1

这个 if 语句:

if (isset($_POST['constituency']) && $_POST['action'] == "create")

在这个 if 语句中:

if (isset($_POST['action']) && $_POST['action'] == "submit") 

所以,$_POST['action'] 不能同时是“提交”和“创建”,所以你永远不会进入你的 INSERT。

于 2012-06-21T12:57:49.280 回答
0

试试这个代码:

if (isset($_POST['action']) && $_POST['action'] == "submit") {
    $state  = mysql_real_escape_string(trim($_POST['state']));
    $query = mysql_query("SELECT senatorial              
                    FROM state
                    WHERE state = '".$state."'") or die (mysql_error());

    $duplicates = mysql_num_rows($query);
}

if (isset($_POST['constituency']) && $_POST['action'] == "create") {
    $constituencys = $_POST['constituency'];
    foreach($constituencys as $constituency) {
        $query = "INSERT INTO  `state` (
                `state_id`,
                `state`,
                `senatorial`
                `constituency`
                ) VALUES (
                NULL,
                '{$state}',
                '{$row['senatorial']}',
                '" . mysql_real_escape_string(trim($constituency)) . "'
                )";
        mysql_query($query) or die (mysql_error());
    }
}

我建议开始使用mysqli_*函数或 PDO 扩展而不是mysql_*...

于 2012-06-21T13:05:28.263 回答