Help with writing data to MySQL database using PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan

    #1

    Help with writing data to MySQL database using PHP

    Hello all,

    Can anyone tell me why the following code isn't working?

    $dbh = mysql_connect($ host,$user,$pas s) or die("Couldn't connect...");
    $selected = mysql_select_db ($db,$dbh);
    $name = $_GET['name'];
    $state = $_GET['state'];
    $desc = $_GET['desc'];
    $lat = $_GET['lat'];
    $lng = $_GET['lng'];
    $result = mysql_query("IN SERT INTO re_cities (name, state, description,
    lat, lng) VALUES ('$name','$stat e','$desc','$la t','$lng')");

    The script doesn't die when I attempt to connect so I'm assuming that
    isn't the problem. I'm pretty sure I also get a result because the
    following code echoes "City Add Successful".

    if ($result) {
    echo "City Add Successful";
    } else {
    echo "City Add Unsuccessful";
    }

    However, the data doesn't show up in the database. Any suggestions?

  • Toby Inkster

    #2
    Re: Help with writing data to MySQL database using PHP

    Bryan wrote:
    re_cities
    What is the schema for this table like? Any columns that are "NOT NULL"?
    Any that need to be unique? Any other constraints?

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact

    Comment

    • Johnny

      #3
      Re: Help with writing data to MySQL database using PHP


      "Bryan" <BTRichardson@g mail.comwrote in message
      news:1158977744 .845188.65700@b 28g2000cwb.goog legroups.com...
      Hello all,
      >
      Can anyone tell me why the following code isn't working?
      >
      $dbh = mysql_connect($ host,$user,$pas s) or die("Couldn't connect...");
      $selected = mysql_select_db ($db,$dbh);
      $name = $_GET['name'];
      $state = $_GET['state'];
      $desc = $_GET['desc'];
      $lat = $_GET['lat'];
      $lng = $_GET['lng'];
      $result = mysql_query("IN SERT INTO re_cities (name, state, description,
      lat, lng) VALUES ('$name','$stat e','$desc','$la t','$lng')");
      >
      The script doesn't die when I attempt to connect so I'm assuming that
      isn't the problem. I'm pretty sure I also get a result because the
      following code echoes "City Add Successful".
      >
      if ($result) {
      echo "City Add Successful";
      } else {
      echo "City Add Unsuccessful";
      }
      >
      However, the data doesn't show up in the database. Any suggestions?
      >
      works for me.

      Try doing a
      var_dump($_GET) ;
      exit;

      above your code and see if everything is there.
      What may be happening is that $_GET may be empty and you end up putting nul
      into the db or nothing at all if not using an autoincrement id in that
      table.


      Comment

      • Colin McKinnon

        #4
        Re: Help with writing data to MySQL database using PHP

        Bryan wrote:
        Hello all,
        >
        Can anyone tell me why the following code isn't working?
        >
        $dbh = mysql_connect($ host,$user,$pas s) or die("Couldn't connect...");
        $selected = mysql_select_db ($db,$dbh);
        $name = $_GET['name'];
        $state = $_GET['state'];
        $desc = $_GET['desc'];
        $lat = $_GET['lat'];
        $lng = $_GET['lng'];
        $result = mysql_query("IN SERT INTO re_cities (name, state, description,
        lat, lng) VALUES ('$name','$stat e','$desc','$la t','$lng')");
        print "error ='" . mysql_error() . "'<br />\n";

        C.

        Comment

        • william.clarke

          #5
          Re: Help with writing data to MySQL database using PHP


          Colin McKinnon wrote:
          Bryan wrote:
          >
          Hello all,

          Can anyone tell me why the following code isn't working?

          $dbh = mysql_connect($ host,$user,$pas s) or die("Couldn't connect...");
          $selected = mysql_select_db ($db,$dbh);
          $name = $_GET['name'];
          $state = $_GET['state'];
          $desc = $_GET['desc'];
          $lat = $_GET['lat'];
          $lng = $_GET['lng'];
          $result = mysql_query("IN SERT INTO re_cities (name, state, description,
          lat, lng) VALUES ('$name','$stat e','$desc','$la t','$lng')");
          >
          print "error ='" . mysql_error() . "'<br />\n";
          >
          C.
          Try storing the query in a variable and echoing/printing it, then you
          should be able to see what the query really looks like. Try running the
          resulting query through your SQL client of choice, hopefully it will
          give you some meaningful feedback. And on a security note you seem to
          be putting a lot of trust in those $_GET variables.

          Comment

          Working...