Jump to content


Photo

Can't post to mysql Database


  • Please log in to reply
7 replies to this topic

#1 nexro

nexro
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 April 2006 - 03:37 PM

Alright, I'm completely new to php and this is the first script I've ever tried writing. In essence, it should be pretty simple.. I think?? Anyways, the purpose of the script (if it isn't obvious) is to post user information into a mysql database. I had recieved some syntax errors, which I fixed. And some 'cannot connect to server' errors which I also fixed. NOW, I log into my register.html page fillout the information, click submit and it loads my register.php script properly, and it doesn't show me any errors, just a blank screen. So I figured "hoorah!!! It's working!!!" Gleefully, I run off to my myphpadmin site and browse the database, to my dismay, there's no new row which has been added. SOOO, not knowing where to look next for the problem, I run to the net, find this site and post this message. So I hope somebody can help. Anyways, here's a copy of the script... (with a few changes to the top 4 lines *wink*)

<?
$host="db.doesntmatter.com";
$username="my_username";
$password="mypassword";
$database="my_database";

$Name=$_POST['Name'];
$Company=$_POST['Company'];
$Address1=$_POST['Address1'];
$Address2=$_POST['Address2'];
$City=$_POST['City'];
$Province=$_POST['Province'];
$PostalCode=$_POST['PostalCode'];
$PhoneH=$_POST['PhoneH'];
$PhoneW=$POST['PhoneW'];
$PhoneM=$_POST['PhoneM'];
$Fax=$_POST['Fax'];
$Email=$_POST['Email'];
$Website=$_POST['Website'];
$Comments=$_POST['Comments'];

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','Name','Company','','Address1','Address2','City','Province','PostalCode',
'PhoneH','PhoneW','PhoneM','Fax','Email','Website','Comments')";
mysql_query($query);

mysql_close();
?>


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 April 2006 - 03:43 PM

Change the query to this:
$query = "INSERT INTO contacts VALUES ('','$Name','$Company','','$Address1','$Address2','$City','$Province','$PostalCode',
'$PhoneH','$PhoneW','$PhoneM','$Fax','$Email','$Website','$Comments')";
While testing/debugging it never hurts to echo the query so you can see what it actually in it - which might well be different from what you expected.
Legend has it that reading the manual never killed anyone.
My site

#3 nexro

nexro
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 April 2006 - 04:48 PM

Thank you, I overlooked that. But if I understand correctly, even WITHOUT the $'s it should have created a new row, it just would have used the words and not the variables. So I would have had an entry where the name was 'name' and the company was 'company'. But it still didn't add that row.

I decided to play round with your comment on echoing things. And I'm really wondering if it is connecting to the database properly, even though I'm not getting the "unable to select database" error. So I tried this:

<?
$host="db.doesntmatter.com";
$username="my_username";
$password="password";
$database="my_database";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
echo "Connected to Database.<br>\n"; 

$query = "SELECT FROM contacts";
$result=mysql_query($query);

echo "<b>$result<br>";

mysql_close();
?>

Should this not echo all of the results from the query with no organization to it? I mean, it SHOULD return me with something shouldn't it? (There are two rows in the database which I created through myphpadmin) All I get as a result from this, is a blank page that says, "Connected to Database." without showing me anything from the content of the mysql table.

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 April 2006 - 05:14 PM

You forgot something, the query should be:
$query = "SELECT * FROM contacts";
And not:
$query = "SELECT FROM contacts";
;)

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 nexro

nexro
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 April 2006 - 05:58 PM

Yeah, I figured that one out. I was going to say I was a bit 'hasty' in that last post. Just kind of frustrated. But I've managed to connect to the database, read the entries which are there, so I know I'm connecting just fine. I've verified that the variables are being sent from my HTML page to the php script appropriately by having it echo the variables back to me. So it would seem, that the problem is back to the original one. And that's simply that

$query = "INSERT INTO contacts VALUES ('','$Name','$Company','','$Address1','$Address2','$City','$Province','$PostalCode',
'$PhoneH','$PhoneW','$PhoneM','$Fax','$Email','$Website','$Comments')";

Is not actually creating any new entries into the database. Is there a different way that this could be written that would create the same effect. Perhaps telling it which database field to put each of the variables into?

#6 nexro

nexro
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 April 2006 - 11:07 PM

Another question, in just trying to understand this. I'm basing this off a tutorial I found on the internet, and all the digging I've done says this should work. But while I understand most of the commands, I wonder....

What is the purpose of these? :
$Name=$_POST['Name'];

I noticed that the $Name variable is the same both before and after this command is issued, so what is it doing?

#7 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 19 April 2006 - 11:19 PM

its just assigning $_POST['name'] values to $name

so in ur insert u wouldnt have to go

INSERT INTO table ('', '$_POST[name]' ....)

instead you would just do

INSERT INTO table ('', '$name' .....)

less chances to make mistakes tht way ;) ........ also it doesnt have to be the same name i.e. you can have $anything = $_POST['name']

#8 nexro

nexro
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 20 April 2006 - 02:52 PM

Alright, next question. (trying to understand & eliminate possible problems)

I once played with a MSAccess DB with Frontpage authoring. And I found that when submitting information into the Database, EVERY field needed to have content. Is it the same with MySQL? If I fill out the form, and leave some fields blank, would it still create a new row in the database filling in what content it could?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users