Page 1 of 1

Unable to Post to MySQL

Posted: Thu Feb 12, 2004 3:14 pm
by eletrium
I am trying to post data to a simply MySQL database (the mysql side runs fine).

I bought O'Reilley's book on MySQL and PHP, and tried an example in his book and its not working for squat...

Here is the code:

Code: Select all

<HTML>
<HEAD>
<TITLE>Bob</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<?php

//  include 'db.inc';
//  include 'error.inc';

  if(empty($firstName) || empty($lastName))
  &#123; 
?>
  <form method="GET" action="DatabaseInputTest.php">
    First_name:
    <br>
    <input type="text" name="firstName" size=80>
    <br>Last_name: 
    <br>
    <input type="text" name="lastName" size=80>
    <br>
    <input type="submit">
   </form>
<?php
  echo $lastName;
  &#125;
  else
  &#123;
    if (!($connection = @ mysql_connect( "localhost", "eletrium", "" )))
      die("could not connect to database");
      
    if (!mysql_select_db("Scheduler", $connection ))
      showerror();
    
    $insertQuery = "Insert into Customers values
                   (3, " . 
                   """ . $firstName . "", " .
                   """ . $lastName . "", " . ")";
                   
    if((@ mysql_query ($insertQuery, $connection )) && @ mysql_affected_rows() == 1)
      echo "<h3>Name Successfully Inserted</h3>";
    else
      showerror();
  &#125;  // if else empty()
?>
</BODY>
</HTML>
When I run this page, when I hit submit, it just brings be back to the original page with the form encoded data doing nothing whatsoever.

SOOOOOOOO, the issue is that I am having issues taking the form encoded data "?firstName=Bob&lastName=Robertson" and connecting it with the variables in PHP. $firstName and $lastName are ALWAYS blank atm.

Any ideas?

Thanks for the help in advance :)

Posted: Thu Feb 12, 2004 3:15 pm
by eletrium
Holy <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> formatting batman... I'll try to edit it to get it more readable... sorry


Edit: Fixed the dang thing with a silly "[code}" tag.... silly me

Posted: Thu Feb 12, 2004 3:16 pm
by markl999
This script requires register_globals to be On, and it's Off by default as of PHP 4.2.0, so rather than turn globals On it's 'better' IMHO to use $_GET['firstName'] and $_GET['lastName'] wherever it currently uses $firstName and $lastName.

THANKS!!!

Posted: Thu Feb 12, 2004 3:21 pm
by eletrium
Thanks a LOT, nice to see this working and such a fast reply.

Maybe we could sticky a few templates for simple stuff that could be kept current... like when someone tries it and it no longer works, they can post a working version that an admin can update...

I hate asking ppl to answer such simple questions, but all of the stuff I have read so far is out of date.

php.net docs seem to be very lacking in this department atm... just too new maybe.

Thanks again :)

Posted: Thu Feb 12, 2004 3:23 pm
by markl999
I should have posted a relevant link to read ;)
http://php.net/variables.predefined details the various register_global issues.

Posted: Thu Feb 12, 2004 3:28 pm
by eletrium
Bah, sok. I need to do oh so much reading all over anyways...

I'm an old fart Delphi/C++ programmer who lost his job a year ago in the tech blood letting... Used to do a lot of database programming, and my old company had a clunky old client server program. Had 1.5 million lines of code, and was EXTREMELY <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>.

Now I see you can get 65% or more of the functionality of a typical client server setup using PHP... at 1% of the cost. So I wrote a nice sort of mini-PowerBuilder program I use to do my database design and it outputs scripts nicely. Now I am learning the PHP side so I can start gettin some true thin-client stuff written.

I figure with the industry moving to India, if I get a business model based on PHP and a fast data modeling tool, it might be doable.