Help! my script couldn't submit form data to mysql database

Reply

Join Date: Jul 2008
Posts: 39
Reputation: emiola is an unknown quantity at this point 
Solved Threads: 1
emiola emiola is offline Offline
Light Poster

Help! my script couldn't submit form data to mysql database

 
0
  #1
Mar 3rd, 2009
Despite the fact that my script runs perfectly well on the server, it fails to submit form data into mysql database. Kindly look into this for me. see code below:
  1. <?php>
  2. $dbcnx = mysql_connect("localhost","user_name","password");
  3. if (!$dbcnx){
  4. exit('<p>Unable to connect to the database</p>');
  5. }
  6. If (isset($_POST['submit']))
  7. {
  8. $SchoolName = $_POST['SchoolName'];
  9. $SchoolEmail = $_POST['SchoolEmail'];
  10. $UserName = $_POST['UserName'];
  11. $pscode = $_POST['pscode'];
  12.  
  13. $sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode)
  14. VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')");
  15. }
  16. echo "Values successfully submitted to database. Thank you" ;
  17. ?>
Is the issue really with the script or with my mysql database table structure?
O God! He that teaches man what he knows not, please grant us the solution.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #2
Mar 3rd, 2009
Nothing wrong with the script. Umm.. Check if it enters the loop,
If (isset($_POST['submit'])) { .
Try mysql_error to know what exactly is the error message. ie.,
  1. $sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode)
  2. VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')") or die (mysql_error());
Also check if error reporting is turned on on your server.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 103
Reputation: jakx12 is an unknown quantity at this point 
Solved Threads: 4
jakx12's Avatar
jakx12 jakx12 is offline Offline
Junior Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #3
Mar 3rd, 2009
yes do what nav33n says. Also check that the table and db actual exist.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 28
Reputation: alexgv14 is an unknown quantity at this point 
Solved Threads: 0
alexgv14 alexgv14 is offline Offline
Light Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #4
Mar 4th, 2009
Also, make sure that the variables are receiving the data. Do an echo on $_POST['SchoolEmail']; to make sure that there is something there and that the post is actually passing the value.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 19
Reputation: satyaseo is an unknown quantity at this point 
Solved Threads: 1
satyaseo satyaseo is offline Offline
Newbie Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #5
Mar 4th, 2009
yes i too agree with nav33n.....
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,479
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Help! my script couldn't submit form data to mysql database

 
0
  #6
Mar 4th, 2009
In addition you have an extra closing bracket on the first line and your mysql query's need escaping. Try making your code the following:
  1. <?php
  2. $dbcnx = mysql_connect("localhost","user_name","password");
  3. if (!$dbcnx){
  4. exit('<p>Unable to connect to the database</p>');
  5. }
  6. if (isset($_POST['submit']))
  7. {
  8. $SchoolName = mysql_real_escape_string($_POST['SchoolName']);
  9. $SchoolEmail = mysql_real_escape_string($_POST['SchoolEmail']);
  10. $UserName = mysql_real_escape_string($_POST['UserName']);
  11. $pscode = mysql_real_escape_string($_POST['pscode']);
  12.  
  13. $sql = mysql_query('INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode)
  14. VALUES ("'.$SchoolName.'", "'.$SchoolEmail.'", "'.$UserName.'", "'.$pscode.'")') or die(mysql_error());
  15. }
  16. echo 'Values successfully submitted to database. Thank you';
  17. ?>
Just a note, it has also been suggested on another topic that using single quotes wherever possible will speed up execution time.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: emiola is an unknown quantity at this point 
Solved Threads: 1
emiola emiola is offline Offline
Light Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #7
Mar 4th, 2009
Thanks for the help fellas. After going through your suggestions, I restructure the script using the recommendations you gave me. I also noticed that I didn't select my database in the mysql query. So I rewrote the code as below and got the message sorry database not connectedNo database selected
  1. <?php>
  2. $dbcnx = mysql_connect("localhost","lsschnln_emiolab","bembestica");
  3. if (!$dbcnx){
  4. exit('<p>Unable to connect to the database</p>');
  5. }
  6. $dbselector = mysql_select_db('lsschnln01');
  7. if (!$dbselector){
  8. echo('sorry database not connected');
  9. }
  10. If (isset($_POST['validate']))
  11. {
  12. $SchoolName = mysql_real_escape_string($_POST['SchoolName']);
  13. $SchoolEmail = mysql_real_escape_string($_POST['SchoolEmail']);
  14. $UserName = mysql_real_escape_string($_POST['UserName']);
  15. $pscode = mysql_real_escape_string($_POST['pscode']);
  16.  
  17. $sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode)
  18. VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')")or
  19. die (mysql_error());
  20. }
  21. echo "Values successfully submitted to database. Thank you" ;
  22. ?>
nav33, will you look into this as you are immediately online now.
O God! He that teaches man what he knows not, please grant us the solution.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #8
Mar 4th, 2009
Ah! How could we miss that in your first post
Even after specifying the database, it said "sorry database not connected No database selected" ? Hmm.. Strange..
Try this.. The 2nd parameter is optional, but, lets see if this can do the trick.
$dbselector = mysql_select_db('lsschnln01',$dbcnx); If it still says no database selected, then you better check if the database really exist.

P.S. Never give out your database username/password in a forum.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: emiola is an unknown quantity at this point 
Solved Threads: 1
emiola emiola is offline Offline
Light Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #9
Mar 4th, 2009
Thanks for your advice nav33. I had posted before realizing my folly. Immediately after posting, I visited w3schools and noticed what you too sugessted and addede the
  1. $dbcnx
parameter but I still got the same response. Take a look please
  1. <?php>
  2.  
  3. $dbcnx = mysql_connect("localhost","username","password");
  4. if (!$dbcnx){
  5. exit('<p>Unable to connect to the database</p>');
  6. }
  7. $dbselector = mysql_select_db('lsschnln01', $dbcnx);
  8. if (!$dbselector){
  9. echo('sorry database not connected');
  10. }
  11. If (isset($_POST['validate']))
  12. {
  13. $SchoolName = mysql_real_escape_string($_POST['SchoolName']);
  14. $SchoolEmail = mysql_real_escape_string($_POST['SchoolEmail']);
  15. $UserName = mysql_real_escape_string($_POST['UserName']);
  16. $pscode = mysql_real_escape_string($_POST['pscode']);
  17.  
  18. $sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode)
  19. VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')")or
  20. die (mysql_error());
  21. }
  22. echo "Values successfully submitted to database. Thank you" ;
  23. ?>
I've gone into my database to check things up. lsschnln01 is available for sure. Should I drop the database and recreate it? Please advise.
Also, I have no idea about how to script a site search engine for my website without the option of google search. Please if you have any code guide, bless me with it.
Thanks.
O God! He that teaches man what he knows not, please grant us the solution.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Help! my script couldn't submit form data to mysql database

 
0
  #10
Mar 4th, 2009
Darn! I typed a long paragraph and just before I clicked 'Post' I lost my internet connection.
Does it still say No database selected ? You only get "No database selected" if you don't have mysql_select_db or if you have specified wrong database name.
Anyways, Try using another database to see if that works. If it doesn't work, then you have to contact your system admin
Sorry. I don't have any guide w.r.t site search engine. Well, maybe someone else from this forum can help you with it.
Last edited by nav33n; Mar 4th, 2009 at 5:15 pm.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC