对于在同一页面上生成的变量,插入语句在 MYSQL 中不起作用


Insert statement not working in MYSQL for variables generated on the same page

我需要你的帮助来编写我的脚本。我尝试做的是将值插入 mysql,同时输入$row['参议院'] 和$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>

this if 语句:

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

位于此 if 语句内:

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

因此,$_POST['action'] 不能同时"提交"和"创建",因此您将永远无法进入插入。

试试这段代码:

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_*...