簡體   English   中英

對於同一頁面上生成的變量,插入語句在MYSQL中不起作用

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

我的腳本需要您的幫助。 我想做的是在mysql中插入值,同時輸入$ row ['senatorial']和$選區。 下面的提示是wa,我正在嘗試工作,但不起作用。 屏幕上未顯示任何錯誤,並且沒有任何內容輸入數據庫。 請問我要去哪里錯了

<?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>

這個if語句:

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

在此if語句中:

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

因此,$ _ POST ['action']不能同時“提交”和“創建”,因此您將永遠無法使用INSERT。

試試這個代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM