Phty5h Posted July 25, 2006 Share Posted July 25, 2006 Hi guys, i'm in the process of practising with mysql, to try and learn a few things.Ive got wamp installed and have used phpmyadmin to create a database called : mydatabaseinside this database i have created a table called : name which has 3 fields : ID, First & SecondId is set to length = 6, auto increment and index, INTThe first and second name fields are set to VARCHAR and length 15I have then loaded Dreamweaver, defined a new site, set the testing server to "C:\wamp\www\" and created a new file called "form.php"At this moment in time form.php looks like this:[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body><?$database="mydatabase";mysql_connect(localhost);@mysql_select_db($database) or die( "Unable to select database");?><form name="form1" id="form1" method="post" action=""> <p>First Name <input type="text" name="textfield" /></p> <p>Surname <input type="text" name="textfield2" /></p> <p> <input type="submit" name="Submit" value="Submit" /> </p></form></body></html>[/code]My first probelm is that when i test this page i get the following error message and i cant figure out why;[code]Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\form.php on line 11Unable to select database[/code]now i know this is very basic stuff....... but im trying to learn here, and all of your help is appreciated, im sure i will have more problems as i progress, and would like to thank you in advance ;D Quote Link to comment Share on other sites More sharing options...
fenway Posted July 25, 2006 Share Posted July 25, 2006 Looks like you're missing valid user credentials. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 I am thinking he left those off to avoid showing people, but if not then that's the problem, another thing,the @ symbol, I am not sure if I am 100% correct, but during one of my questions wildteen said it was best not to use that for live websites, I have been avoiding it ever since, just some advice, that and die, I was told are good to use during testing phases, and building, but change it when the site goes live, atleast that was the advice I was given. also if you echo mysql_error()likeecho mysql_error();after each call during creation 90% of the time it will give you detailed explanationsit might sayyou did not choose a passwordor no username was listedor could not select databaseor something specific giving you a better idea on where to launch your search at. Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 Thanks for the reply, i think ive just figured it out.I had to change some privlage's settings within phpmyadmin, and include them in the code, my .php file now looks like this:[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body><?$database="mydatabase";$user="user10";$password="password"; $connect = mysql_connect('localhost', $user, $password)or die ('Could Not Connect: ' . mysql_error());echo '<b>Step 1 :</b> Connected Successfully! <br>';$db = 'contacts';mysql_select_db($db) or die('Could Not Select Database ('.$db.') Because of: '.mysql_error());echo '<b>Step 2:</b> Connected to ('.$db.') successful!<BR>';mysql_close($connect);?><form name="form1" id="form1" method="post" action=""> <p>First Name <input type="text" name="textfield" /></p> <p>Surname <input type="text" name="textfield2" /></p> <p> <input type="submit" name="Submit" value="Submit" /> </p></form></body></html>[/code]It connects correctly, and displays the form, im now looking at how i get the form to pass information into the database. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 look up update, alter, or insertQuick run over, there are 3 elements to a basic querythe php querythe mysql queryand then getting the data ready to displayyou create a variable with then line, for instance if you are just inserting a new rowtable name is userinfo columns are username password and email[code]$instert = "INSERT username, password, email INTO userinfo VALUES $username, $password, $email;";// then you query the database, this is the basis for all database work$query = mysql_query($insert);// see it runs the query into the database.// doing whatever commands you instructed it to do// if you were just inserting data, on a basic level, that's all you really need. If you were// trying to select the data, you would do the same thing, but you have to get that first// then youw ould use SELECT at the beginning and specify what to select. // then you would run that through below.// then you have another command, that doesn't really DO anything, but return as a result the number of row //Mostly just for listing the number of rows that you found, if you wanted to do that.$num_rows = mysql_num_rows($query);//if it found 10 rows, the $num_rows would now contain the number 10 and so forth, if it didn't find any it returns 0 //thereby also returning false.//in php 0=false//which you probably already knew//now if you wanted to get it formatted//you use while, or foreachforeach($row = mysql_fetch_array($query)) {//statements}//Or while//foreach and while seem mostly the same, but they function a little differently, if it doesn't work right with one try it //on another, I tried running while before, couldn't get it to work, so defaulted to foreach, and vice versa, it has //happened a few times, fi you need any more advice just ask.[/code] Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 that just went straight over my head....... sorry mate ...... i still require a helping hand with this Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 That was what I was attempting to do, those are all the steps needed to do what you said, as far as inserting data, if you need examples, just show table information, and ask me what you need, I can give you some copy and paste code, look at each function, break them down, learn how to use each function and what they do, the sql statements are straightforward, let those come naturall, if you tackle the sql without knowing how the php comes together around that you end up learning 3 things instead of 2, for now focus on the basics, I can provide you with the sql you need to do what you need, play with them and learn how to do the php functions first, get a good feel for the order of how they go, basic entering data, inserting, updating, ex ceterasome you will probably never need but there good to know, like creating a database, or table through php, the table you might need, just tell me what you are focusing on, and when and when I check my email again, I will check the post and come and see if I can help you sort it out and slowly catch on. It will only take you a few days to learn the basics, if you have a helping hand. Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 thankyou, i really appreciate the help ;DMy form is very simple at the moment... just a practice one, as you can see from my above code, it has 2 text box's... one for firstname & one for surname, and a submit button.I want to be able to enter a name into the text box's and have that information sent to the relevant fields in my database.Later i will want to create a membership system that will display info that was wentered previously by any given member once they log in.But for now i just need to get these two text fields passing data to the sql database. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 [code]$textfield = $_POST['textfield'];$textfield2 = $_POST['textfield2'];// for not until you can get to where you can pass them straight into, you have to understand how the queries work first. name which has 3 fields : ID, First & Second$insert = "INSERT INTO name (first, second) VALUES $textfield, $textfield2;";mysql_query($insert);[/code]Play with that a few hours, see what you can do, if it doesn't work try to debug it, through trial and error you will learn, that should work directly but look up each part on your own, play with that some, and get a good feel for that. Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 I really do appreciate your help.And i understand the way your trying to help me, it is not good enough for me to simply get it to work without understanding...... but please understand that at 9am this morning i couldnt even create a database in phpmyadmin.... nevermind get my webpage to connect to it correctly.I think i have acheived both of these things today, but in all honesty i could spend the next year playing with that code above and still not get it in the correct place, I understand how the above code works in principle, what i cant do is get it in the right place to work.At the moment my code (the important bit anyway) looks like this;[code]<?$database="mydatabase";$user="user1";$password="phobos"; $connect = mysql_connect('localhost', $user, $password)or die ('Could Not Connect: ' . mysql_error());echo '<b>Step 1 :</b> Connected Successfully! <br>';$db = 'mydatabase';mysql_select_db($db) or die('Could Not Select Database ('.$db.') Because of: '.mysql_error());echo '<b>Step 2:</b> Connected to ('.$db.') successful!<BR>';mysql_close($connect);$textfield = $_POST['firstname'];$textfield2 = $_POST['surname'];// for not until you can get to where you can pass them straight into, you have to understand how the queries work first. name which has 3 fields : ID, First & Second$insert = "INSERT INTO name (first, second) VALUES $textfield, $textfield2;";mysql_query($insert); //This is line 27?><form name="form1" id="form1" method="post" action=""> <p>First Name <input type="text" name="firstname" /></p> <p>Surname <input type="text" name="surname" /></p> <p> <input type="submit" name="Submit" value="Submit" /> </p></form>[/code]you can see where ive placed the code you provided, and when it is placed in this position i get connected to the database as before, but with an error on line 27line 27 is the line that i have commented above.I understand how forms work with php, and have created several using hiddent fields and POST to send the info from multiple pages to an email inbox...... this however seems to work differently, and im afraid i need to ask you for more help.Apoligies for not getting this, and taking up your time ..... i really appreciate it. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 ok, first of all change your username and password, forget the variable part, and put the information straight into the query, just for practice it saves coding room, as well you never should put your username and password in a post put #### #### for hte username and password or something similar to keep people from seeing them, you have to change your password, and username now or someone could get into your database, try this out.[code]<?if (isset($formsubmit) {$connect = mysql_connect("localhost", "username", "password")$selectdb = mysql_select_db("databasename");if (!connect || !selectdb) {echo "There was an error connecting to, or select the database";echo mysql_error(); // remove this after you get it working properly}$textfield = $_POST['firstname'];$textfield2 = $_POST['surname'];$insert = "INSERT INTO name(first, second) VALUES $textfield, $textfield2;";if ($query = mysql_query($insert)) {echo "The data was inserted into the database correctly";}else {echo mysql_error(); // take this out when it starts working.echo "There was a problem inserting the data into the database"; }}?><form name="form1" id="form1" method="post" action=""> <p>First Name <input type="text" name="firstname" /></p> <p>Surname <input type="text" name="surname" /></p> <p> <input type="submit" name="formsubmit" value="Submit" /> </p></form>[/code]I made some modifications and where you had[code]mysql_close($connect);[/code]Those closes the connection you had closed the connection before the query.Read over it carefully, copy and paste the entire code, copy the EXACT output, and let me know what it says. I don't think I missed anything there but I was in a little bit of a rush, copy and paste the exact output and let me know how it works. Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 ok i replaced my code with the code you just supplied and i get the following error:[code]Parse error: syntax error, unexpected '{' in C:\wamp\www\forumsugest.php on line 10[/code]Allthough if i move the close statement on my origional code to after the query it removes the errors i was recieving, so thanks for that :) Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 [code]<?if (isset($formsubmit)) {$connect = mysql_connect("localhost", "username", "password")$selectdb = mysql_select_db("databasename");if (!connect || !selectdb) {echo "There was an error connecting to, or select the database";echo mysql_error(); // remove this after you get it working properly}$textfield = $_POST['firstname'];$textfield2 = $_POST['surname'];$insert = "INSERT INTO name(first, second) VALUES $textfield, $textfield2;";if ($query = mysql_query($insert)) {echo "The data was inserted into the database correctly";}else {echo mysql_error(); // take this out when it starts working.echo "There was a problem inserting the data into the database"; }}?><form name="form1" id="form1" method="post" action=""> <p>First Name <input type="text" name="firstname" /></p> <p>Surname <input type="text" name="surname" /></p> <p> <input type="submit" name="formsubmit" value="Submit" /> </p></form>[/code]That should fix it, but if it works on your other script you shouldn't need it, tell me if you need anything else? Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 my other script displays properly, but i still dont think its passing data into the database.when i run your ammended script i get the following now:[code]Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\forumsugest.php on line 12[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 [code]<?if (isset($formsubmit)) {$connect = mysql_connect("localhost", "username", "password");$selectdb = mysql_select_db("databasename");if (!connect || !selectdb) {echo "There was an error connecting to, or select the database";echo mysql_error(); // remove this after you get it working properly}$textfield = $_POST['firstname'];$textfield2 = $_POST['surname'];$insert = "INSERT INTO name(first, second) VALUES $textfield, $textfield2;";if ($query = mysql_query($insert)) {echo "The data was inserted into the database correctly";}else {echo mysql_error(); // take this out when it starts working.echo "There was a problem inserting the data into the database"; }}?><form name="form1" id="form1" method="post" action="page.php"> <p>First Name <input type="text" name="firstname" /></p> <p>Surname <input type="text" name="surname" /></p> <p> <input type="submit" name="formsubmit" value="Submit" /> </p></form>[/code]I had a few errors, I was in a hurry, that should work, don't forget to replace the form part, where it says action, to the name of the page that you are working on, whatever page the script is on, needs to be set to that page name. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 25, 2006 Share Posted July 25, 2006 Shouldn't it be:[code]$insert = "INSERT INTO name(first, second) VALUES ('$textfield', '$textfield2')";[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 yes it can be, but it doesn't have to be typed out that way, that would be the way that it's supposed to be done but the other way works as well, thanks for pointing that out. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 There are always things that can be done one way or another for instance with variables, when extrapolating you can just put$variableor you can put {$variable}I use to use $variable, but instead decided to start using {$variable}but with query's I use the short way because I type queries so much I have be able to do them, quickly. But I suppose it would be a good habit to get into doing it like that from the very beginning. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 25, 2006 Share Posted July 25, 2006 It's very important never to "take shortcuts", especially while learning. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 Your right and I apologize, that is something to do when you know the difference, when I teach I will teach the correct way, and allow them to branch off if they choose to do so later on. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 25, 2006 Share Posted July 25, 2006 Nothing to apologize for... just a general remark; shortcuts are an easy way to "miss the point", which defeats the entire purpose. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 Thanks for the advice, I appreciate and for the author of the post, if you needed any more help feel free to ask. Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 that seems to work better.but ... i dont get a confirmation echo'd out that the info has been placed in the database correctly.... it just seems to refresh the screen ..... tbh im not sure how id go about prooving to myself that the info is in the database when it does work... i assume ill be able to see the entries in phpmyadmin somewhere.Am i supposed to be using 2 pages here?.... i have all the code on a single page at the moment?[me=Phty5h]feels stupid[/me] Quote Link to comment Share on other sites More sharing options...
fenway Posted July 25, 2006 Share Posted July 25, 2006 You don't need to have two pages... the same script can either insert or select. Quote Link to comment Share on other sites More sharing options...
Phty5h Posted July 25, 2006 Author Share Posted July 25, 2006 then i dont understand why this doesnt seem to work.... i must be missing something .... i understand what its doing... and logically this should work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.