|
|
|
|
![]() ![]() |
Nov 17 2005, 04:26 AM
Post
#1
|
|
|
Member [ Level 2 ] Group: Members Posts: 71 Joined: 24-September 04 From: Los Angeles, CA Member No.: 830 |
hey well can some one helpme make this code work it won't INSERT INTO THE DATABSE
CODE <?php
# register1.php # common include file to MySQL include("DB.PHP"); $Username=$_POST['Username']; $Password=$_POST['Password']; $Name=$_POST['Name']; $Last=$_POST['Last']; $Sex=$_POST['Sex']; $Month=$_POST['Month']; $Day=$_POST['Day']; $Year=$_POST['Year']; $Adresse=$_POST['Adresse']; $City=$_POST['City']; $State=$_POST['State']; $Zipcode=$_POST['Zipcode']; $Country=$_POST['Country']; $Phone=$_POST['Phone']; $Email=$_POST['Email']; $Father_Name=$_POST['Father_Name']; $Mother_Name=$_POST['Mother_Name']; $Parent_Phone=$_POST['Parent_Phone']; $Parent_Email=$_POST['Parent_Email']; $Level=$_POST['Level']; $Academic=$_POST['Academic']; $Image_Link=$_POST['prevImage']; $sql9="INSERT INTO User SET id = 'NULL', Username = '$Username', Password = '$Password', Name='$Name', Last='$Last', Sex='$Sex', Month='$Month', Day='$Day', Year='$Year', Adresse='$Adresse', City='$City', State='$State', Zipcode='$Zipcode', Country='$Country', Phone='$Phone', Email='$Email', Father_Name='$Father_Name', Mother_Name='$Mother_Name', Parent_Phone='$Parent_Phone', Parent_Email='$Parent_Email', Level='$Level', $Academic='$Academic'"; $sql3="SELECT * FROM User WHERE Username='$Username' AND Password='$Password'"; $sql4="SELECT * FROM User WHERE Email='$Email'"; # insert login/password $result = mysql_query($sql); if (!$result) { echo "Please Try Again"; } else { echo"Thank you for sign up"; } mysql_close($connection); ?> This post has been edited by miCRoSCoPiC^eaRthLinG: Nov 17 2005, 06:14 AM |
|
|
|
Nov 17 2005, 11:44 AM
Post
#2
|
|
|
Super Member Group: Members Posts: 572 Joined: 25-April 05 From: Nashville Tennessee Member No.: 4,340 |
Did I miss something in the above or did you connect to the database somewhere else? It needs to connect first then it can insert, do you get an error with the above.
Usually you would start with CODE $connect = mysql_connect($host,$user,$password)
die ("Couldn't connect to server!"); $db = mysql_select_db($database,$connect) die("Couldn't select databse!"); |
|
|
|
Nov 22 2005, 01:48 AM
Post
#3
|
|
|
Member [ Level 2 ] Group: Members Posts: 71 Joined: 24-September 04 From: Los Angeles, CA Member No.: 830 |
QUOTE(Houdini @ Nov 17 2005, 03:44 AM) Did I miss something in the above or did you connect to the database somewhere else? It needs to connect first then it can insert, do you get an error with the above. Usually you would start with CODE $connect = mysql_connect($host,$user,$password) die ("Couldn't connect to server!"); $db = mysql_select_db($database,$connect) die("Couldn't select databse!"); CODE include("DB.PHP"); thats where that is
|
|
|
|
Nov 22 2005, 06:48 AM
Post
#4
|
|
|
Absolute Newbie Group: Admin Posts: 888 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
So Houdini's question was the first one I had too the first time I looked at this. Next time be sure to include any external code that we may need to help debug.
Next I found another problem. You run an mysql query on $sql but $sql doesn't exist. QUOTE(wannabeeaweak @ Nov 16 2005, 11:26 PM) CODE # insert login/password $result = mysql_query($sql); <--------------------------------- HERE!!!!!! if (!$result) { echo "Please Try Again"; } else { echo"Thank you for sign up"; } mysql_close($connection); ?> QUOTE(wannabeeaweak @ Nov 16 2005, 11:26 PM) hey well can some one helpme make this code work it won't INSERT INTO THE DATABSE CODE $sql9="INSERT INTO User ..... $sql3="SELECT * FROM User WHE..... $sql4="SELECT * FROM User WHE..... You actually need to run the seperate querries on $sql9, $sql3, and $sql4! I suggest writting a function that could be generic enough to handle any variable. Maybe: CODE function boogy_down($sql){ $result = mysql_query($sql); if (!$result) { echo "Please Try Again"; } else { echo"Thank you for sign up"; } } With the funtion call of: CODE boogy_down($sql9); boogy_down($sql3); boogy_down($sql4); Now keep in mind that that particular function will output "Thank you for sign up" 3 times if successful. It would be better to put the variables in an array and add a loop to the function then if all commands are successful, echo "Thank you for sign up"! I'm not going to write the whole thing for you but this should at least get you back on track. Hope This Helps! vujsa |
|
|
|
Nov 23 2005, 12:49 PM
Post
#5
|
|
|
Super Member Group: Members Posts: 595 Joined: 4-September 04 Member No.: 228 |
Yep, you're basically entering an empry query as $sql is not defined.
But a generic function for different types of queries might be a bit useless as the return values of mysql_query can be very different, ranging from simple boolean values of a say INSERT to big results of SELECT queries. I don't if you meant to do it but although otherwise a possible thing to do would be to send multiple queries tot the databse at once, separated by semicolons this is not possible with PHP as it is considered a security threat. You code would miss the appending of the quesries anyways so you probably weren't thinking about this. |
|
|
|
Nov 23 2005, 11:30 PM
Post
#6
|
|
|
Absolute Newbie Group: Admin Posts: 888 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
QUOTE(Hercco @ Nov 23 2005, 07:49 AM) Yep, you're basically entering an empry query as $sql is not defined. But a generic function for different types of queries might be a bit useless as the return values of mysql_query can be very different, ranging from simple boolean values of a say INSERT to big results of SELECT queries. I don't if you meant to do it but although otherwise a possible thing to do would be to send multiple queries tot the databse at once, separated by semicolons this is not possible with PHP as it is considered a security threat. You code would miss the appending of the quesries anyways so you probably weren't thinking about this. I didn't look closely enough to see that there was both INSERT and SELECT commands being sent as a result, the very least you should use a different function for each type unless you are pretty good with your function writing. vujsa |
|
|
|
Nov 29 2005, 09:17 PM
Post
#7
|
|
|
Super Member Group: Members Posts: 595 Joined: 4-September 04 Member No.: 228 |
QUOTE(vujsa @ Nov 24 2005, 01:30 AM) I didn't look closely enough to see that there was both INSERT and SELECT commands being sent as a result, the very least you should use a different function for each type unless you are pretty good with your function writing. vujsa I guess there could be alternative methods inside the function. It would check whether the query starts with SELECT or INSERT and the select the proper action. Would be a bit messy function but on the other hand the actual code would look neat with only single type of function calls for database queries. |
|
|
|
Nov 30 2005, 01:46 AM
Post
#8
|
|
|
Absolute Newbie Group: Admin Posts: 888 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
I tend to write a perfectly good function for one use. Then later when I need a similar function, I just modify the first function. Then later, I need yet another similar function and repeat the process. I tend to write very complex functions as a result which are able to handle several different requests and then the function call is very simple to write as a result.
Since most of my database functions are all-in-one, I just need to use the same call each time with different variables. Then I tend to name my function something like deal_with_the_db() which is what it does and I don't have to think about the DB anymore. LOL I tend to get frustrated with the mySQL commands so I remove myself a little. By the way, why is it so difficult to get information out of the database even if you have all of the required information and you only want one entry. vujsa |
|
|
|
May 6 2006, 12:30 AM
Post
#9
|
|
|
Super Member Group: [HOSTED] Posts: 768 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
hey well can some one helpme make this code work it won't INSERT INTO THE DATABSE CODE <?php # register1.php # common include file to MySQL include("DB.PHP"); $Username=$_POST['Username']; $Password=$_POST['Password']; $Name=$_POST['Name']; $Last=$_POST['Last']; $Sex=$_POST['Sex']; $Month=$_POST['Month']; $Day=$_POST['Day']; $Year=$_POST['Year']; $Adresse=$_POST['Adresse']; $City=$_POST['City']; $State=$_POST['State']; $Zipcode=$_POST['Zipcode']; $Country=$_POST['Country']; $Phone=$_POST['Phone']; $Email=$_POST['Email']; $Father_Name=$_POST['Father_Name']; $Mother_Name=$_POST['Mother_Name']; $Parent_Phone=$_POST['Parent_Phone']; $Parent_Email=$_POST['Parent_Email']; $Level=$_POST['Level']; $Academic=$_POST['Academic']; $Image_Link=$_POST['prevImage']; $sql9="INSERT INTO User SET id = 'NULL', Username = '$Username', Password = '$Password', Name='$Name', Last='$Last', Sex='$Sex', Month='$Month', Day='$Day', Year='$Year', Adresse='$Adresse', City='$City', State='$State', Zipcode='$Zipcode', Country='$Country', Phone='$Phone', Email='$Email', Father_Name='$Father_Name', Mother_Name='$Mother_Name', Parent_Phone='$Parent_Phone', Parent_Email='$Parent_Email', Level='$Level', $Academic='$Academic'"; Your SQL Insert statement is wrong, you confused it with the UPDATE SQL Statement, the correct way to write it is: CODE $sql9="INSERT INTO User( Username, Password, Name, Last, Sex, Month, Day, Year, Adresse, City, State, Zipcode, Country, Phone, Email, Father_Name, Mother_Name, Parent_Phone, Parent_Email, Level, Academic) Values('$Username', '$Password', '$Name', '$Last', '$Sex', '$Month', '$Day', '$Year', '$Adresse', '$City', '$State', '$Zipcode', '$Country', '$Phone', '$Email', '$Father_Name', '$Mother_Name', '$Parent_Phone', '$Parent_Email', '$Level', '$Academic')"; mysql_query($sql9) or die(mysql_errno(). ": " . mysql_error() ); I dont include the id field because i think it is an integer autonumeric field, let me know if im wrong about it, also you must verify all your data prior to your insertion, maybe using javascript in your forms page and another good practice to prevent sql injections is the use of the mysql_real_escape_string function if your system has the magic_quoutes_gpc off. Best regards, |
|
|
|
May 8 2006, 02:17 PM
Post
#10
|
|
|
Premium Member Group: Members Posts: 292 Joined: 15-December 04 Member No.: 1,768 |
Your SQL Insert statement is wrong, you confused it with the UPDATE SQL Statement, the correct way to write it is: CODE $sql9="INSERT INTO User( Username, Password, Name, Last, Sex, Month, Day, Year, Adresse, City, State, Zipcode, Country, Phone, Email, Father_Name, Mother_Name, Parent_Phone, Parent_Email, Level, Academic) Values('$Username', '$Password', '$Name', '$Last', '$Sex', '$Month', '$Day', '$Year', '$Adresse', '$City', '$State', '$Zipcode', '$Country', '$Phone', '$Email', '$Father_Name', '$Mother_Name', '$Parent_Phone', '$Parent_Email', '$Level', '$Academic')"; mysql_query($sql9) or die(mysql_errno(). ": " . mysql_error() ); I dont include the id field because i think it is an integer autonumeric field, let me know if im wrong about it, also you must verify all your data prior to your insertion, maybe using javascript in your forms page and another good practice to prevent sql injections is the use of the mysql_real_escape_string function if your system has the magic_quoutes_gpc off. Best regards, Actually you can do a traditional CODE INSERT INTO table(value1, value2) VALUES('SomeValue', 'SomeValue2') But I think in mysql it'll let you address an insert statement as if you're updating, which isn't the same for postgres. Try it and let me know what you find out, but I'm pretty sure it's allowed. In this case he's wating to insert, not update. I was looking at a previous script that one of my professors wrote usuing a mysql database and his insert scripts looked a lot like the update statements. When we switched over to postgres we had to redo the insert statements to match to the traditional insert statement. It would probably be wise to stick with traditional unless you plan on staying with mysql. Vujsa, you don't actually use a dbi? You just right your own code to supplement the use of db connections/queries? I was introduced to a dbi that's based on perl's standard dbi or something like that. I like it a lot better than all the php functions to deal with mysql/postgres. It standardizes everything, so whatever you want to do (use mysql, or postgres) you use same function call. You just change the inital connection's function to receieve a string argument of what database. I believe that's the way it's done, but with the script I'm working on there's about 10 different includes to every page in order to make it work right. Good experience tho. After thinking about it I can see the advantage of writing a script that does what you're talking about vujsa. Have a function like build_db_query or something that builds the actually query and returns it as a string perhaps. You pass a table name, and an array of values in an assoc array indexing the related tables in the fields. You could make it even more flexible and add flag to determine whether it's a select, insert, update, or delete statement. Just have a switch on the flag, then build the appropriate sql statement. I'm rather new to OOP and php, but can't this be done with polymorphism? (rather new to OOP in general) This post has been edited by minnieadkins: May 8 2006, 04:31 PM |
|
|
|
![]() ![]() |
Similar Topics