コード例 #1
0
 function displayMessage()
 {
     $conn = connectToDatabase();
     if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
     }
     $table = $_POST['table'];
     $id = $_POST['id'];
     // Send the update query, check to see if the update was successful.
     $columnNames = getColumnNames($table);
     $columns = array();
     // Array that holds the column names of the values to be inserted.
     $values = array();
     // Array that holds the values to be inserted.
     for ($i = 1; $i < count($columnNames); $i++) {
         if (isset($_POST[$columnNames[$i]])) {
             $value = trim($_POST[$columnNames[$i]]);
             if ($value != "") {
                 array_push($columns, $columnNames[$i]);
                 array_push($values, $value);
             }
         }
     }
     $query = "UPDATE {$table} SET ";
     if (count($columns) == count($values)) {
         for ($i = 0; $i < count($values); $i++) {
             $query .= "{$columns[$i]} = " . "'" . $values[$i] . "'";
             if ($i + 1 < count($columns)) {
                 $query .= ", ";
             }
         }
     }
     $query .= " WHERE " . $_POST['id_column'] . " = " . $_POST['id'];
     //echo $query;
     if (mysqli_query($conn, $query) == TRUE and count($values) > 0) {
         $result = retrieveRow($table, $id);
         echo "<p>The " . $_POST['table'] . " has been updated to</p>";
         echo "<table class='updated'>";
         if (count($columnNames) > 0 and $result->num_rows > 0) {
             while ($row = $result->fetch_assoc()) {
                 if (count($row) == count($columnNames)) {
                     for ($i = 0; $i < count($row); $i++) {
                         echo "<tr>";
                         echo "<td><b>" . $columnNames[$i] . "</b></td>";
                         echo "<td>" . $row[$columnNames[$i]] . "</td>";
                         echo "</tr>";
                     }
                 }
             }
         } else {
             echo "0 results";
         }
         echo "</table>";
         echo "<form action='runAudit.php' method='post'>\n                <input type='hidden' name='bowlingAudit' value='" . $_POST['table'] . "'>\n                <input type='submit' value='Back to table'>\n            </form>";
     } else {
         if (count($values) < 1) {
             $error = "<br>No fields have been updated.";
             echo $error;
             echo "<br><br><form action='runAudit.php' method='post'>\n                <input type='hidden' name='bowlingAudit' value='" . $_POST['table'] . "'>\n                <input type='submit' value='Back to table'>\n            </form>";
         } else {
             $error = "Unable to update.\n\nError: " . $conn->error;
             //echo $error;
             $error = json_encode($error);
             echo "\n            <script type='text/javascript'>\n                alert({$error});\n                history.go(-1);\n            </script>";
         }
     }
     $conn->close();
 }
コード例 #2
0
 function displayMessage()
 {
     $conn = connectToDatabase();
     if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
     }
     $table = $_POST['table'];
     // Send the update query, check to see if the update was successful.
     $columnNames = getColumnNames($table);
     //INSERT INTO bowling.Ball (Color, Weight, Size) VALUES ('Black', 7, 2);
     $query = "INSERT INTO bowling.{$table} (";
     $columns = array();
     // Array that holds column names of the values to be inserted
     $values = array();
     // Array that holds values to be inserted
     for ($i = 1; $i < count($columnNames); $i++) {
         if ($columnNames[$i] == "Date_Added" || $columnNames[$i] == "Last_Date_Modified" || $columnNames[$i] == "Date_Joined" || $columnNames[$i] == "Date_Deleted") {
             continue;
         } else {
             if ($_POST[$columnNames[$i]] == "") {
                 continue;
             } else {
                 array_push($columns, $columnNames[$i]);
                 array_push($values, $_POST[$columnNames[$i]]);
             }
         }
     }
     for ($i = 0; $i < count($columns); $i++) {
         $query .= "{$columns[$i]}";
         if ($i + 1 < count($columns)) {
             $query .= ", ";
         }
     }
     $query .= ") VALUES (";
     for ($i = 0; $i < count($values); $i++) {
         $query .= "'" . $values[$i] . "'";
         if ($i + 1 < count($values)) {
             $query .= ", ";
         }
     }
     $query .= ");";
     //echo $query;
     if (mysqli_query($conn, $query) == TRUE) {
         $id = $conn->insert_id;
         $result = retrieveRow($table, $id, $columnNames[0]);
         echo "<p>A new  " . $_POST['table'] . " has been added with the following values </p>";
         echo "<table>";
         if (count($columnNames) > 0 and $result->num_rows > 0) {
             while ($row = $result->fetch_assoc()) {
                 if (count($row) == count($columnNames)) {
                     for ($i = 0; $i < count($row); $i++) {
                         echo "<tr>";
                         echo "<td><b>" . $columnNames[$i] . "</b></td>";
                         echo "<td>" . $row[$columnNames[$i]] . "</td>";
                         echo "</tr>";
                     }
                 }
             }
         } else {
             echo "0 results";
         }
         echo "</table>";
         echo "<form action='runAudit.php' method='post'>\n                <input type='hidden' name='bowlingAudit' value='" . $_POST['table'] . "'>\n                <input type='submit' value='Back to table'>\n            </form>";
     } else {
         $error = "<br>Unable to update. Error: " . $conn->error;
         echo $error . "<br><br>";
         echo "<form action='runAudit.php' method='post'>\n                <input type='hidden' name='bowlingAudit' value='" . $_POST['table'] . "'>\n                <input type='submit' value='Back to table'>\n            </form>";
     }
     $conn->close();
 }