DagicCross
DagicCross

Reputation: 401

How to UPDATE only filled input with a submit button?

PROBLEM: I got a problem updating my input into sql using PHP, the PHP updates all empty values into sql which I don't want to.

ACHIEVEMENT: So I hope to achieve when user submit their data either empty or filled then PHP might be able to pickup and update only filled data into my sql. I tried using input with value=">php echo here<" but it won't work with textarea, so I couldn't find any solution since I'm new to PHP and SQL. Tried to find similar posts but I couldn't make them work like I wanted to :(

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

in PHP script

<?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();
?>

Second Attempts: It doesn't update my data somehow... please help I tried more than 8 hours with no results :(

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;
  }

}

Help would be appreciated, thanks everyone!

Upvotes: 0

Views: 1963

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

Using your Second Attempt as a starting point.

The problem with just using the POST array without being specific is that, in this example you are going to try an update a column on the database called submit i.e. your submit button. Later there may be data on the page that belongs in 2 or more tables.

So create an controlling array containing all the field names from the form that you want to process onto your table.

$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;
}

Upvotes: 1

Related Questions