Web Programming Languages

PHP Source Code: dbwrite.php

<?php
//----------------------------------------------------------
//   The Web Language Project
//   Mark Brautigam
//   May-June 2015
//   http://www.mixed-up.com/markb/
//----------------------------------------------------------

include "common.php";
headers1();
headers2();
echo sidebar("php", "dbwrite", "php");
?>
  
  <div id="content">
    <h2>PHP: Write to MySQL Database</h2>

<?php
  draw_form();

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

    // special handling for integers
    if (!isset($_POST['lat']) || $_POST['lat'] == "" || !is_numeric($_POST['lat']))
      $_POST['lat'] = 0;
    if (!isset($_POST['lon']) || $_POST['lon'] == "" || !is_numeric($_POST['lon']))
      $_POST['lon'] = 0;
    if (strlen($_POST['state']) > 4)
      $_POST['state'] = substr ($_POST['state'], 0, 4);

    draw_feedback($_POST);

    // database handling
    include "./connect.php";
    $dbh = my_connect ();
    $sql = "INSERT INTO parks VALUES ('{$_POST['site']}', " . 
      " '{$_POST['state']}', '{$_POST['city']}', {$_POST['lat']}, {$_POST['lon']}, null);";
    echo "<p>The database query is: $sql</p>\n";

    if (function_exists('mysqli_query')) {
      $qresult = $dbh->query ($sql);
      if ($dbh->error)
        echo "<p>Error: $dbh->error</p>";
    }
    else {
      $qresult = mysql_query ($sql);
      if (mysql_error())
        echo "<p>Error: " + mysql_error() + "</p>";
    }
  }

    ?>
    <p><a href='dbread.php'>Show the contents of this database table »</a></p>
    <p><a href='source.php?f=8'>Show PHP source code »</a>
  </div>

<?php 
footers(); 

  //-------------------------------------------------------------
  function formHasData ($p)
  //-------------------------------------------------------------
  {
    $formFields = array ( 'site', 'city', 'state', 'lat', 'lon' );
    for ($i=0, $n=count($formFields); $i<$n; $i++) {
      if (isset($p[$formFields[$i]]) && trim($p[$formFields[$i]]) != "")
        return true;
    }
    return false;
  }

  //-------------------------------------------------------------
  function draw_form ()
  //-------------------------------------------------------------
  { ?>
  <form name='parksform' action='dbwrite.php' method='POST'>
    <fieldset class='db'>
    <legend>Add a park</legend>
    <table>
      <tr><td>Park:</td><td><input type='text' name='site' /></td></tr>
      <tr><td>City:</td><td><input type='text' name='city' /></td></tr>
      <tr><td>State:</td><td><input type='text' name='state' /> <span>(4 characters max)</span></td></tr>
      <tr><td>Latitude:</td><td><input type='text' name='lat' /></td></tr>
      <tr><td>Longitude:</td><td><input type='text' name='lon' /></td></tr>
      <tr><td></td><td><input type='submit' id='submit' name='submit' value='Add' /></td></tr>
    </table>
    </fieldset>
  </form>
  <?php
  }

  //-------------------------------------------------------------
  function draw_feedback ($post)
  //-------------------------------------------------------------
  {
    // draw the HTML table that shows them what they entered
    //
    echo "<p>The following data was entered in the data file: </p>\n";
    echo "<table class='results'>\n";
    print ("  <tr><td>Park</td><td>{$post['site']}</td></tr>\n");
    print ("  <tr><td>City</td><td>{$post['city']}</td></tr>\n");
    print ("  <tr><td>State</td><td>{$post['state']}</td></tr>\n");
    print ("  <tr><td>Latitude</td><td>{$post['lat']}</td></tr>\n");
    print ("  <tr><td>Longitude</td><td>{$post['lon']}</td></tr>\n");
    echo "</table>\n";
  }

?>