Can't update mysql db from php web form

software development

#1

Hi,

I’ve recently migrated to dreamweaver & I’m having problems migrating my databases, or rather migrating my php form interfaces. The database itself loaded just fine & updates fine from shell mysql and from phpmyadmin. The only problem is with the php web forms. My php forms that do adds or updates simply don’t work. Here’s a simplified version of the form:

<?php include ("passwordfile"); $db = mysql_connect($hostname, $username, $password); mysql_select_db($databasename,$db); if ($submit) { $result = mysql_query("INSERT INTO quotes (lname) VALUES ('$lname')",$db); printf ("Last inserted record id %d\n", mysql_insert_id()); printf ("$lname"); } else{ ?> lname

<?php } // end if ?>

But, simple queries via the php web forms work just fine – for instance, a page that has a URL input to generate a query, or a page that just selects and displays all records, both work fine.

The forms were moved without changing anything except the server/db variables.

I wonder about:

  • I verified that my permissions for this dbuser include add, delete, etc.
  • The statement but it looks okay to me.
  • Possibly my code doesn’t work because in migrating from old host to dreamhost, a shift in php or mysql wasn’t accounted for? I’m not knowledgeable about the diffs b/w versions of php & mysql.
  • Some issue with enabling php form inputs ?

Any thoughts? I feel sure it’s something quite obvious & stupid – especially since I can do queries but not adds or updates (or perhaps it’s the form input that’s not working) – but I’m just not seeing it.


#2

Which version of PHP are you using?


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#3

php 5, as far as i know; i didn’t specify any different variables, except when i initiated the account & specified php 5. a version thing seems completely possible, but why would the select scripts work and not the update scripts?


#4

In that case, your problem is probably related to register_globals. Many responsible webhosts are disabling the register_globals directive in the php.ini file for security reasons. DreamHost have chosen to do this only on PHP5 installations (along with disabling “magic quotes”), to help people get used to the new system.

It means that global variables are not automatically being created from superglobal variables like $_POST and $_GET. You need to create the global variables yourself. In the script you posted, you need to create the two $_POST variables like this:

$submit = $_POST['submit']; $lname = $_POST['lname'];It looks like a pain in the ass, but it makes your scripts more secure, and also more portable.


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#5

Beautiful; that was it. Thanks so much! I throw many good karma points your way.