Jump to content

Can't post to mysql Database


nexro

Recommended Posts

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*)

[code]
<?
$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();
?>
[/code]
Link to comment
Share on other sites

Change the query to this:
[code]$query = "INSERT INTO contacts VALUES ('','$Name','$Company','','$Address1','$Address2','$City','$Province','$PostalCode',
'$PhoneH','$PhoneW','$PhoneM','$Fax','$Email','$Website','$Comments')";[/code]
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.
Link to comment
Share on other sites

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:

[code]
<?
$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();
?>[/code]

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.
Link to comment
Share on other sites

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

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

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?
Link to comment
Share on other sites

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? :
[code]
$Name=$_POST['Name'];
[/code]

I noticed that the $Name variable is the same both before and after this command is issued, so what is it doing?
Link to comment
Share on other sites

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

so in ur insert u wouldnt have to go

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

instead you would just do

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

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

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.