Jump to content

[SOLVED] Script wont post in database


mydownfall00

Recommended Posts

First post, doesn't mean anything. There's many who know more, but that's not the point.

 

So, im attempting to create a sign up form and i have the script working.. it's running and i don't get any errors. Although it doesn't want to insert the information into the database. The database is ran through the site where I have my server and I set the database up through there.

 

username  varchar(25) No     

firstname  varchar(25) No     

lastname  varchar(25) No     

email  varchar(30) No     

sex  varchar(7) No     

age  tinyint(3) No 

(the no's mean not null... just saying)

 

 

this is how my database table is setup. i don't have a primary key.. which im pretty sure that I should but I know how to auto_increment I just didnt know how to include it with the form in html.

 

now here is my php code

 

 

 

 

$username=$_POST['username'];

$firstname=$_POST['firstname'];

$lastname=$_POST['lastname'];

$email=$_POST['email'];

$sex=$_POST['sex'];

$age=$_POST['age'];

 

 

        $query = "INSERT INTO member(username, firstname, lastname, email, sex, age) VALUES ('$username', '$firstname', 'lastname', 'email', 'sex', 'age')";

 

        $result = ($query);

        if (!$result) {

            $errormessage = pg_last_error();

            echo "Error with query: " . $errormessage;

            exit();

        }

        printf ("These values were inserted into the database - %s %s %s", $username, $firstname, $lastname);

       

        ?>

 

 

if anyone could possibly help me it would be great or if you see an error in my code or how i have my table setup.. that is where the error is i just don't know how to fix it.

 

any help would be greatly appreciated.

Link to comment
Share on other sites

$query = "INSERT INTO member(username, firstname, lastname, email, sex, age) VALUES ('$username', '$firstname', 'lastname', 'email', 'sex', 'age')";

 

your last 4 variables lastname,email etc are missing the $

 

 

what database are you using??

 

Link to comment
Share on other sites

Fatal error: Call to undefined function pg_query()

 

is the error i get now that i changed the result query, and yeah i put the $ back where they belong.. i cant believe i missed them.

 

What database are you using?

 

 

Most likely mysql

 

 

$result = mysql_query($query);

 

pg_query() isnt a mysql function

Link to comment
Share on other sites

Fatal error: Call to undefined function pg_query()

 

is the error i get now that i changed the result query, and yeah i put the $ back where they belong.. i cant believe i missed them.

 

What database are you using?

 

 

Most likely mysql

 

 

$result = mysql_query($query);

 

pg_query() isnt a mysql function

 

 

I know. But, did you look at pg_last_error() statement in the code? Its PostgreSQL database statement. So, I took it as he is using PostgreSQL database.

 

Hope, I am making sense.

 

Link to comment
Share on other sites

phpMyAdmin - 2.11.9.5

MySQL client version: 5.0.67

 

okay.. i dont know all that much of sql.. i did it back in high school and i was just playing around to see what i could still possibly do since im paying for a host. i have a php for dummies book i have to find.. but i like talking to atleast someone about it.

 

i have no idea what a PostgreSQL database is.. but that's what my host uses. im not too sure about the differences in the code, but if someone could just fill me in a little bit.. that would be cool.

Link to comment
Share on other sites

Ok, try this:

 

<?php
$username=$_POST['username'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname']; 
$email=$_POST['email']; 
$sex=$_POST['sex']; 
$age=$_POST['age']; 

$query = "INSERT INTO `member`(`username`, `firstname`, `lastname`, `email`, `sex`, `age`) VALUES ('$username', '$firstname', '$lastname', '$email', '$sex', '$age')";

$result = mysql_query($query); 
if (!$result) { 
    $errormessage = mysql_error(); 
    echo "Error with query: " . $errormessage; 
    exit(); 
} 
printf ("These values were inserted into the database - %s %s %s", $username, $firstname, $lastname);          
?>

 

Link to comment
Share on other sites

Okay, so the code im pretty sure is going to work except now I am having a problem with my connection and it's saying that

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'shopceme'@'localhost' (using password: NO) in /home/shopceme/public_html/mydownfall/add2.php on line 35

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/shopceme/public_html/mydownfall/add2.php on line 35

Error with query: Access denied for user 'shopceme'@'localhost' (using password: NO)

 

and im not exactly sure which username and password I have to use, when I create a new database through phpmyadmin it makes me create a user to login and access it. im thinking that is the one that you would have to use but it doesnt seem to be trying. im going to attempt to make a new table really quick and see what I can do incase I have the password wrong. One question though, how do I set up an auto_increment to give as a user id so say each time someone signs up they will each be given a number such as #1, #2, #3 and so on.. and how do I set that up with the html form.. or dont I even have to include it because of the auto_increment..

 

just checking.. i really appreciate all this help.

Link to comment
Share on other sites

this is how i have my connection setup..

 

$host="localhost";

$username="myusername"; //

$password="password"; //

$db_name="shopceme_mydownfall";

 

 

now the database I have created is called shopceme_mydownfall and then the table I have created inside shopceme_mydownfall is called user...

 

 

so...

 

$query = "INSERT INTO `user`

i changed that.. but it wont let me connect.

im not sure if i have to change anything in the settings of my server or not.

(btw i made a new table so there is only username firstname lastname and email.)

 

 

EDIT!!!!

 

Okay, I get the error message on line 34:

this is what line 34 consists of...

$result = mysql_query($query);

Link to comment
Share on other sites

Okay add this, right after <?php

 

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$DBname = "MyDatabase";
$db_selected = mysql_select_db($DBname, $link);
if (!$db_selected) {
    die ("Can't use $DBname: " . mysql_error());
}

 

but change the $DBname = "MyDatabase"; to your database name (the name of the database on the left in PMA)

Link to comment
Share on other sites

Man im sorry that this is so hard and i dont freakin know what's wrong because now

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'admin'@'localhost' (using password: YES) in /home/shopceme/public_html/mydownfall/add2.php on line 20

Could not connect: Access denied for user 'admin'@'localhost' (using password: YES)

 

i think since it's saying yes that the password is working but the user I don't know why it's not working.

 

When I just created this table.. it asked me to setup a username and a password.. which I believe would be the reason to connect to the database.. unless it's something else but there aren't that many choices especially since i think the password is right..

 

sorry im trying guys..

 

EDIT!!!

 

also I just went and changed the password so password = yes must not mean that I have my password correct.

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.