Help with MySQL Connectivity, Insert Into, Reading from Database

Okay, so this is the first time ever that I have posted to get help with code so I am kind of shy to display my work. Here is my problem that I am having. I am NEW to php and I am creating a website. Eventually, I want to be able to to have a login system in which users have to login to view the site. I started tonight by learning about the databases and how they can be manipulated to add data from a user input. I use cPanel, myPHPadmin and have created the database, a user for the database with all privileges granted.

  1. I have created the form in a .html page with the following code:


For testing purposes, please enter in your information Your Name: Your Email: [/code] The submit button on this page, sends the user to insert.php which has this code: [php]<?php

mysql_connect(“localhost”, “daddyhog_first”, “kt041609”) or die(mysql_error());
mysql_select_db(“daddyhog_firstTesting”) or die(mysql_error());

$sql=“INSERT INTO People(name, email)VALUES(’$name’, ‘$email’)”;
echo “Successful”;
echo “
echo “Back to main page”;

else {
echo “ERROR”;

<?php // close connection mysql_close(); ?>[/php]

Now in the mysql_connect(“localhost”, “daddyhog_first”, “kt041609”) I have a few questionis please.
The localhost. Does this have to be localhost or where is this supposed to get directed from.
The user name is daddyhog_first which has all permissions
kt041609 is the password for the user.
Now when I go to the index.html and enter information in the form, i’m redirected to the .php page with the word ERROR, as suggested in the echo call out if something is going wrong. I have been at this for a few hours, what is it that i’m not understanding.
Please and thank you for your time in this matter.
I look forward to actively learning more

Hi DaddyHoggens, welcome to the forum and please, don’t be shy. Everyone here was like you one day. :slight_smile:

First and foremost, you should edit your post and **** over your mysql details, they are private and should remain that way for security reasons…

Right, now we got that out the way, you have a typo? in your script which means the query will never be true, thus you see the error message…

You labelled this variable: $username;

Here your referencing a variable that doesn’t exist! $name
[php]$sql=“INSERT INTO People(name, email)VALUES(’$name’, ‘$email’)”;[/php]

Change one or the other and you’ll be good to go. :wink:

Oh, and ‘localhost’ is the name of the server where your mysql resides and is normal - sometimes can be different, but usually localhost.

Hope that helps,
Red :wink:

Thank you for returning my post with some information. I didn’t know about the personal information since I figure no one knows my website but just to be on the safe side I will go ahead and do that now.
Thank you for the warm introduction as well. Now onto the code in which I am still struggling with after reading your reply;

You said to change “on or the other”
Here is the problem that I am having with that. the $username is stated like that to read from the database with the column name username. the $_POST(‘name’) should be getting the information from the form on the previous page since it’s being redirected. (Perhaps, maybe all of this needs to be on the same page, maybe in php form? )

[php]$sql=“INSERT INTO People(name, email)VALUES(’$name’, ‘$email’)”;[/php]
I canged the $name to match the beginning variable of ‘name’ which should match my form id name for when a user enters in his or her name. I also changed email to match accordingly since after reading yours, my didn’t make sense. Would have I have to enter into the Values $_POST[‘name’] so that my code knows where exactly it should be getting the information from instead of a just a random lucky entry in code?

My goal is as stands so i can make it make sense

  1. Page 1 > User enters in information in HTML Form then hits Submit
  2. Page is redirected to a .php page with my above code listed, page should gather all the information
  3. If this was sucessful, I then want to build a page that will display the database to make sure that it was added correctly.

Thank you again for your time, Red. I look forward to hearing from you again at your earliest convenience!

$sql=“INSERT INTO People VALUES(’$username’, ‘$email’)”; [/php]

Okay Red, so I have changed up my code again and this time, I deleted the beginning (name, email) and then it seemed to work. I was beyond excited that I got it to get sent to my database. Now that this is working, is where my next error shows up.
I have sent in MySQL datbase the following:
1 username varchar(255) latin1_swedish_ci
2 email varchar(255) latin1_swedish_ci
Also at the bottom it should be noted that I see an error Message with the following:
No Index Defined!

Could all of this have an impact on how this is getting err’d?

Sponsor our Newsletter | Privacy Policy | Terms of Service