Web Programming Languages

Ruby Source Code: dbedit.rb

#!/usr/bin/ruby
require 'cgi'
#require 'mysql'
#require './connect'
require './common'

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

cgi = CGI.new
id   = cgi['id']
if (id == '') 
  print "Location: dbread.rb\n\n";
end
id = id.to_i

# see if this database entry exists
#con = my_connect()
#sql = sprintf("SELECT * FROM parks WHERE id=%d", id)
#rs = con.query(sql);
#if (rs.num_rows < 1)
#  print "Location: dbread.rb\n\n";
#end
#row = rs.fetch_row

puts cgi.header
headers1()
headers2()
print sidebarX("ruby", "dbedit", "rb")

print %q(
  <div id="content">
    <h2>Ruby: Write to MySQL Database</h2>
    <form name='parksform' action='dbedit.rb' method='POST'>
      <fieldset class='db'>
      <legend>Edit a park</legend>
      <table>
)
printf("<tr><td>Park:</td><td><input type='text' name='site' value='%s' /></td></tr>", row[0])
printf("<tr><td>City:</td><td><input type='text' name='city' value='%s' /></td></tr>", row[2])
printf("<tr><td>State:</td><td><input type='text' name='state' value='%s' /></td> <span>(4 characters max)</span></tr>", row[1])
printf("<tr><td>Latitude:</td><td><input type='text' name='lat' value='%s' /></td></tr>", row[3])
printf("<tr><td>Longitude:</td><td><input type='text' name='lon' value='%s' /></td></tr>", row[4])
printf("<input type='hidden' name='id' value='%s' />", id)
print %q(
        <tr><td></td><td><input type='submit' id='submit' name='submit' value='Edit' /></td></tr>
      </table>
      </fieldset>
    </form>
)

#----------------------------------------------------
def is_number (s)
#----------------------------------------------------
  #http://stackoverflow.com/questions/5661466/test-if-string-is-a-number-in-ruby-on-rails
  true if Float(s) rescue false
end

#----------------------------------------------------
#  The main Ruby code is here.
#----------------------------------------------------
site   = cgi['site']
city   = cgi['city']
state  = cgi['state']
lat    = cgi['lat']
lon    = cgi['lon']
submit = cgi['submit']

#if (submit == "Edit" && formHasData([site, city, state, lat, lon]))

  # Integer considerations
#  if (lat == '')
#    lat = 0
#  end
#  if (lon == '')
#    lon = 0
#  end
#  if (!is_number (lat))
#    lat = 0
#  end
#  if (!is_number (lon))
#    lon = 0
#  end
#  if (state.length > 4)
#    state = state[0..3]
#  end

  print "<p>The following data was edited in the database: </p>\n";
  print "<table class='results'>\n";
  print "<tr><th>Field</th><th>Old data</th><th>New data</th></tr>"
  printf("  <tr><td>Park</td><td>%s</td><td>%s</td></tr>\n", row[0], site);
  printf("  <tr><td>City</td><td>%s</td><td>%s</td></tr>\n", row[2], city);
  printf("  <tr><td>State</td><td>%s</td><td>%s</td></tr>\n", row[1], state);
  printf("  <tr><td>Latitude</td><td>%s</td><td>%s</td></tr>\n", row[3], lat);
  printf("  <tr><td>Longitude</td><td>%s</td><td>%s</td></tr>\n", row[4], lon);
  print "</table>\n";

  # The acutal database connection happens here
#  sql = sprintf("UPDATE parks SET site='%s', city='%s', state='%s', " +
#        "latitude=%s, longitude=%s WHERE ID=%d;", site, city, state, lat, lon, id);
#  print sql;
#  rs = con.query(sql);
#end
#----------------------------------------------------

print %q(
    <p><a href='dbread.rb'>Show the contents of this database table »</a></p>
    <p><a href='source.rb?f=9'>Show Ruby source code »</a>
  </div>
)

footers()