如何使用提交按钮更新仅填充的输入?

时间:2016-01-23 01:01:45

标签: php mysql sql database forms

问题:我在使用PHP更新输入sql时遇到问题,PHP将所有空值更新为sql,我不想这样做。

成就:所以我希望当用户提交他们的数据为空或填充时,PHP可能能够拾取并仅将填充的数据更新到我的sql中。我尝试使用值="> php echo here<"但是它不能与textarea一起工作,所以我找不到任何解决方案,因为我是PHP和SQL的新手。试图找到类似的帖子,但我不能让它们像我想的那样工作:(

<?php include 'config/sqlconnect.php'; ?>


      <form method="post" action"config/sqlconnect.php">
        </p>MainPage info</p>
        <input type="text" name="mainPageInfo"/>

        <br>
        </p>MiddlePage info</p>
        <textarea name="middlePageInfo"></textarea>

        <br>
        </p>Container info</p>
        <input type="text" name="containerInfo"/>

        <br>
        </p>Content</p>
        <input type="text" name="content"/>

        <br>
        </p>Second content</p>
        <input type="text" name="secondContent"/>


        <input type="submit" name="submit" class="btn-block"/>
        <br>
      </form>
PHP脚本中的

<?php
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "pagesDb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"utf8");
$sql = "SELECT * FROM myPages";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
    $mainPageInfo = $row['mainPageInfo'];
    $middlePageInfo = $row['middlePageInfo'];
    $containerInfo = $row['containerInfo'];
    $content = $row['content'];
    $secondContent = $row['secondContent'];
     }
} else {
     echo "0 results";
}

if (isset($_POST['submit'])) {
    $mainPageInfo = $_POST['mainPageInfo'];
    $middlePageInfo = $_POST['middlePageInfo'];
    $containerInfo = $_POST['containerInfo'];
    $content = $_POST['content'];
    $secondContent = $_POST['secondContent'];

    $sql = "UPDATE myPages SET mainPageInfo='$mainPageInfo', 
                               middlePageInfo='$middlePageInfo',
                               containerInfo='$containerInfo',
                               content='$content',
                               secondContent='$secondContent' 
                               WHERE id=0";

  if ($conn->query($sql) === TRUE) {
      echo "Record updated successfully";
  } else {
      echo "Error updating record: " . $conn->error;
  }

}

$conn->close();
?>

第二次尝试:它不会以某种方式更新我的数据...请帮助我尝试超过8小时但没有结果:(

if (isset($_POST['submit'])) {

  foreach($_POST as $name => $value) {

        $sql = "UPDATE myPages SET $name = '$value' WHERE id=1";

  }

  if ($conn->query($sql) === TRUE) {
      echo "Record updated successfully";
  } else {
      echo "Error updating record: " . $conn->error;
  }

}

感谢大家的帮助!

1 个答案:

答案 0 :(得分:1)

使用 Second Attempt 作为起点。

仅使用POST数组而不是特定的问题在于,在此示例中,您将尝试更新名为submit的数据库上的列,即您的提交按钮。稍后,页面上可能存在属于2个或更多表的数据。

因此,创建一个控制数组,其中包含您要处理到表格中的表单中的所有字段名称。

$db_fields = array('mainPageInfo', 'middlePageInfo', 'containerInfo',
                   'content', 'secondContent');

$sql = '';   // will hold the query we build dynamically

// was this a user sending data
if ( $_SERVER['REQUEST_METHOD' == 'POST' ) {

    foreach($db_fields as $fieldname) {

        if ( ! empty($_POST[$fieldname] ) {
            $sql .= "$fieldname = '{$_POST[$fieldname]}', ";
        }
    }
}

$sql = rtrim($sql, ',');    // remove the trailing comma

$sql = "UPDATE myPages SET $sql WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}
相关问题