Jump to content

Creating a database and a table?


eduard

Recommended Posts

You've already created the database using the wizard, all you need to do is use mysql_connect to connect to the server, use mysql_select_db to select the database you created, and then start running CREATE TABLE queries to set up the tables you want in that database.
Ok, thanks!
Link to comment
Share on other sites

  • Replies 74
  • Created
  • Last Reply
Try using the MySQL username and password you created in the wizard to log in to phpMyAdmin, those might work also.If that doesn't work, you can just create a script to set up the tables and run it once. All it needs is the code to connect, select the database, and then queries to create the tables. You can post your code for that if you need help with it.
What do you mean the MySQL username and password?
Link to comment
Share on other sites

You've already created the database using the wizard, all you need to do is use mysql_connect to connect to the server, use mysql_select_db to select the database you created, and then start running CREATE TABLE queries to set up the tables you want in that database.
I understand it! But how do I do it? Can I make 1 php script (creating a table).with mysql connect to my database and upload this file?
Link to comment
Share on other sites

Try using the MySQL username and password you created in the wizard to log in to phpMyAdmin, those might work also.If that doesn't work, you can just create a script to set up the tables and run it once. All it needs is the code to connect, select the database, and then queries to create the tables. You can post your code for that if you need help with it.
But I´ll keep the problem with phpMyAdmin?
Link to comment
Share on other sites

I checked this file and it´s ok!<php$con = mysql_connect("localhost","eduardli_company","******")mysql_select_db("eduardli_company", $con);$sql = "CREATE TABLE Products"(Description varchar (50),Price int,Quantity int) " ;mysql_query($sql,$con));mysql_close($con);?>So just uploading?

Link to comment
Share on other sites

that file is definitely not OK (as you've posted it anyway). There is one huge error in your opening php tag, and there are two syntax errors.edit: your username is also the name of your database? :)

Link to comment
Share on other sites

What do you mean the MySQL username and password?
The username and password you created in the database wizard inside cPanel.
I understand it! But how do I do it? Can I make 1 php script (creating a table).with mysql connect to my database and upload this file?
The script you posted is the right idea, minus a few small errors. You're missing a semicolon on the first line, your SQL query has a quote in the wrong place, and your call to mysql_query has too many parentheses. You should also always check for MySQL errors:mysql_query($sql,$con) or exit(mysql_error($con));
But I´ll keep the problem with phpMyAdmin?
If you've tried all of the usernames and passwords you can think of that may work on phpMyAdmin, and none of them work, then you still need to figure out how to log in there. You really need to be writing down these usernames and passwords if you aren't going to remember them.
Link to comment
Share on other sites

that file is definitely not OK (as you've posted it anyway). There is one huge error in your opening php tag, and there are two syntax errors.edit: your username is also the name of your database? :)
Ok, thanks! (the checker said that it was ok!)
Link to comment
Share on other sites

<html><body><?phpvar_dump($_POST);if (isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){echo "succesful form submission. initialize DB connection .....";$con = mysql_connect("localhost","eduardli_company","-z.x,c");if (!$con){die('Could not connect: ' . mysql_error());};echo "connection initialized.....";mysql_select_db("eduardli_company", $con);$sql="INSERT INTO (description, price, quantity) VALUES ('$_POST[description]','$_POST[price]','$_POST[quantity]')";echo 'prepare for INSERT: ' . $sql;if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}else{echo "1 record added";};mysql_close($con);}else{echo "one or more form fields missing";}?></body></html>This is my insert file! But I´ve to select a db! How do I select the table?

Link to comment
Share on other sites

The username and password you created in the database wizard inside cPanel.The script you posted is the right idea, minus a few small errors. You're missing a semicolon on the first line, your SQL query has a quote in the wrong place, and your call to mysql_query has too many parentheses. You should also always check for MySQL errors:mysql_query($sql,$con) or exit(mysql_error($con));If you've tried all of the usernames and passwords you can think of that may work on phpMyAdmin, and none of them work, then you still need to figure out how to log in there. You really need to be writing down these usernames and passwords if you aren't going to remember them.
That isn´t the problem! I did there something in ´Privileges´ and since that moment I can´t log in anymore!
Link to comment
Share on other sites

With SQL queries, the table names go in the queries. Check the INSERT tutorial to see some examples.
Sorry
Link to comment
Share on other sites

That isn´t the problem! I did there something in ´Privileges´ and since that moment I can´t log in anymore!
Is that on your hosted server or your local server? If it's on your hosted server, tell your host that you changed the privileges and now you can't log in. If it's on your local server, it's probably easiest to just reinstall MAMP than try to figure out what you did and undo it. In the future, you probably shouldn't make changes to things like that unless you know what you're doing.
Link to comment
Share on other sites

Is that on your hosted server or your local server? If it's on your hosted server, tell your host that you changed the privileges and now you can't log in. If it's on your local server, it's probably easiest to just reinstall MAMP than try to figure out what you did and undo it. In the future, you probably shouldn't make changes to things like that unless you know what you're doing.
It´s on my local machine!I downloaded phpMyAdmin another time, but I don´t understand so far how to install it!
Link to comment
Share on other sites

Until you can understand and follow the installation instructions, it's probably easiest to just uninstall MAMP and install it again. It probably doesn't matter if you install phpMyAdmin again, because the changes were made in MySQL. You need to reinstall MySQL, so it's probably easiest to use MAMP.

Link to comment
Share on other sites

This is the error message of my INSERT file!array(3) { ["description"]=> string(1) "w" ["price"]=> string(1) "1" ["quantity"]=> string(1) "2" } succesful form submission. initialize DB connection .....connection initialized.....prepare for INSERT: INSERT INTO (description, price, quantity) VALUES ('w','1','2')Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(description, price, quantity) VALUES ('w','1','2')' at line 1And this is the file!?<html><body><?phpvar_dump($_POST);if (isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){echo "succesful form submission. initialize DB connection .....";$con = mysql_connect("localhost","eduardli_company","-z.x,c");if (!$con){die('Could not connect: ' . mysql_error());};echo "connection initialized.....";mysql_select_db("eduardli_company", $con);$sql="INSERT INTO (description, price, quantity) VALUES ('$_POST[description]','$_POST[price]','$_POST[quantity]')";echo 'prepare for INSERT: ' . $sql;if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}else{echo "1 record added";};mysql_close($con);}else{echo "one or more form fields missing";}?></body></html>

Link to comment
Share on other sites

I don´t get it!If I wriite this file (with localhost, username (of the server or of the db?, of the server or of the db?) I get this error message:array(3) { ["description"]=> string(1) "w" ["price"]=> string(1) "1" ["quantity"]=> string(1) "2" } succesful form submission. initialize DB connection .....Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'eduardli'@'localhost' (using password: YES) in /home/eduardli/public_html/web_designer/insert.php on line 10Could not connect: Access denied for user 'eduardli'@'localhost' (using password: YES)And how can I do in the best way-because now it doesn´t work? Now the problem is that data of my html form not goes in a table (Products) of my db (eduardli_company)!

Link to comment
Share on other sites

what's not to get?

You don't have the table name in the query.
$sql="INSERT INTO (description, price, quantity) VALUES ('$_POST[description]','$_POST[price]','$_POST[quantity]')";

you're not telling it which TABLE in the DATABASE to INSERT to.edit: the username/password applies to the same thing whether locally or live, and that's the user profile to access MySQL (the database). The only thing that should change (if needed) are the username and password, depending on what the username/password is for your local database, or your live database.it's ALWAYS trying to connect to the database, you just need to know which username and password to use depending on the context (local vs. live). This is why it helps to write these things down.

Link to comment
Share on other sites

You must be getting used to these error messages by now, Eduard. This error:Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'eduardli'@'localhost'means that your username and password are not correct.I recommend you stick to one server that works until you get a working site, then you can worry about moving it to a different server.

Link to comment
Share on other sites

You must be getting used to these error messages by now, Eduard. This error:Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'eduardli'@'localhost'means that your username and password are not correct.I recommend you stick to one server that works until you get a working site, then you can worry about moving it to a different server.
I only have 1 host server! The problem was which dates do I have to write in the msql_connect: those of my host sever or of my db? Now I know: DB
Link to comment
Share on other sites

This is the error message:array(3) { ["description"]=> string(1) "w" ["price"]=> string(1) "1" ["quantity"]=> string(1) "2" } succesful form submission. initialize DB connection .....connection initialized.....prepare for INSERT: INSERT INTO Product (description, price, quantity) VALUES ('w','1','2')Error: Table 'eduardli_company.Product' doesn't existTis i the table file:<?php$con = mysql_connect("localhost","eduardli_user","-z.x,c")mysql_select_db("eduardli_company", $con);$sql = "CREATE TABLE Products(Description varchar (50),Price int,Quantity int) " ;mysql_query($sql,$con) or exit(mysql_error($con));mysql_close($con);?>This is the insert file (html form is ok!):<html><body><?phpvar_dump($_POST);if (isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){echo "succesful form submission. initialize DB connection .....";$con = mysql_connect("localhost","eduardli_user","-z.x,c");if (!$con){die('Could not connect: ' . mysql_error());};echo "connection initialized.....";mysql_select_db("eduardli_company", $con);$sql="INSERT INTO Product (description, price, quantity) VALUES ('$_POST[description]','$_POST[price]','$_POST[quantity]')";echo 'prepare for INSERT: ' . $sql;if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}else{echo "1 record added";};mysql_close($con);}else{echo "one or more form fields missing";}?></body></html>What do I wrong?

Link to comment
Share on other sites

I only have 1 host server! The problem was which dates do I have to write in the msql_connect: those of my host sever or of my db? Now I know: DB
I don't know what dates you are talking about. When mysql_connect is used on your host, it is also connecting to a database, just the database on your host. It's still a database, because that's all mysql_connect does, connect to database. You will also have passwords for logging into phpMyAdmin(s), as well as your host. Do yourself a favor and write all the profiles down, and whether it's for your host or for your local computer. The whole idea is that you have a website locally, and you also have a database. You then want to move it online. The concept we're trying to stress to you is that you have to get it working rock solid on your local machine first, and you know exactly why it works, and what username/passwords (if any) that make it work. So make sure you can login to phpMyAdmin on your computer, and that you can connect to the your local database in your script. Write down all these username and passwords and make that this is what works on your local computer.Then, once that is all set, it's time to move it online. You and your host will both have a webroot; know how to find them. So boom, that's where are all (and only) your web pages, images, stylesheets, scripts go. No databases, no phpMyAdmin files, nothing but pages and scripts.Now you need to upload your database (or start a new one) online. You have one locally. Locally, you can login with phpMyAdmin, and you can also do so on your host. Login to phpMyAdmin or contact your host if you have problems, and write down the username and password that made it work, and write a note saying that it's for phpMyAdmin on your host.So, at this point you can use your hosts phpMyAdmin to create your database and table, or you can export the local one and import it. Either way is fine, but if you have data on the local one that you want to been seen on your website right off the bat, then you'll want to export your local one and import the database (or else you can just manually add all the records yourself). From phpMyAdmin you can manage privileges to the database. Check to see the users there and make sure you know the username and password of a user (or make one) who has full privileges to the database you are going to use. Once you have that, then that will be the username and password you use when it's time to upload your script to your host from your local computer in order to make mysql_connect. After you get all this working, you could consider having parallel users on both your local and hosted databases, so that you only need one username and password so that you don't have to switch anything when you upload files to your host.
Link to comment
Share on other sites

This is the error message:array(3) { ["description"]=> string(1) "w" ["price"]=> string(1) "1" ["quantity"]=> string(1) "2" } succesful form submission. initialize DB connection .....connection initialized.....prepare for INSERT: INSERT INTO Product (description, price, quantity) VALUES ('w','1','2')Error: Table 'eduardli_company.Product' doesn't existTis i the table file:<?php$con = mysql_connect("localhost","eduardli_user","-z.x,c")mysql_select_db("eduardli_company", $con);$sql = "CREATE TABLE Products(Description varchar (50),Price int,Quantity int) " ;mysql_query($sql,$con) or exit(mysql_error($con));mysql_close($con);?>
this files still has the same syntax errors as in your previous code post. It will never run, so the error couldn't have come from this file. However, since you have a script like this, you know it will only need to be run one time once you fix the errors. I don't see why you don't know there are errors, we taught you how to make sure you have no errors in your scripts. If you are back to these kind of mistakes, then you need to take this process slower.
This is the insert file (html form is ok!):<html><body><?phpvar_dump($_POST);if (isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){echo "succesful form submission. initialize DB connection .....";$con = mysql_connect("localhost","eduardli_user","-z.x,c");if (!$con){die('Could not connect: ' . mysql_error());};echo "connection initialized.....";mysql_select_db("eduardli_company", $con);$sql="INSERT INTO Product (description, price, quantity) VALUES ('$_POST[description]','$_POST[price]','$_POST[quantity]')";echo 'prepare for INSERT: ' . $sql;if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}else{echo "1 record added";};mysql_close($con);}else{echo "one or more form fields missing";}?></body></html>What do I wrong?
and you also have sytax errors here, so this script file would never run. Now to your error message. If you are testing this on your host, do you know for sure that you have a table called Products? I mean that's exactly what the error message is telling you.
Error: Table 'eduardli_company.Product' doesn't exist
So.... look at your script. Do you see what you are actually calling your database? I mean, do you not even read the error messages?
Link to comment
Share on other sites

I don't know what dates you are talking about. When mysql_connect is used on your host, it is also connecting to a database, just the database on your host. It's still a database, because that's all mysql_connect does, connect to database. You will also have passwords for logging into phpMyAdmin(s), as well as your host. Do yourself a favor and write all the profiles down, and whether it's for your host or for your local computer. The whole idea is that you have a website locally, and you also have a database. You then want to move it online. The concept we're trying to stress to you is that you have to get it working rock solid on your local machine first, and you know exactly why it works, and what username/passwords (if any) that make it work. So make sure you can login to phpMyAdmin on your computer, and that you can connect to the your local database in your script. Write down all these username and passwords and make that this is what works on your local computer.Then, once that is all set, it's time to move it online. You and your host will both have a webroot; know how to find them. So boom, that's where are all (and only) your web pages, images, stylesheets, scripts go. No databases, no phpMyAdmin files, nothing but pages and scripts.Now you need to upload your database (or start a new one) online. You have one locally. Locally, you can login with phpMyAdmin, and you can also do so on your host. Login to phpMyAdmin or contact your host if you have problems, and write down the username and password that made it work, and write a note saying that it's for phpMyAdmin on your host.So, at this point you can use your hosts phpMyAdmin to create your database and table, or you can export the local one and import it. Either way is fine, but if you have data on the local one that you want to been seen on your website right off the bat, then you'll want to export your local one and import the database (or else you can just manually add all the records yourself). From phpMyAdmin you can manage privileges to the database. Check to see the users there and make sure you know the username and password of a user (or make one) who has full privileges to the database you are going to use. Once you have that, then that will be the username and password you use when it's time to upload your script to your host from your local computer in order to make mysql_connect. After you get all this working, you could consider having parallel users on both your local and hosted databases, so that you only need one username and password so that you don't have to switch anything when you upload files to your host.
I don´t have to write (until now!) anything down, because I know them! Confusing them or not using them right could be a problem!I only have 1 database (eduardli_company) on line!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...