943,785 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1699
  • PHP RSS
Mar 3rd, 2009
0

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

Expand Post »
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:
php Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
emiola is offline Offline
49 posts
since Jul 2008
Mar 3rd, 2009
0

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

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.,
php Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 3rd, 2009
0

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

yes do what nav33n says. Also check that the table and db actual exist.
Reputation Points: 10
Solved Threads: 5
Junior Poster
jakx12 is offline Offline
123 posts
since Jan 2009
Mar 4th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
alexgv14 is offline Offline
49 posts
since Feb 2008
Mar 4th, 2009
0

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

yes i too agree with nav33n.....
Reputation Points: 10
Solved Threads: 1
Newbie Poster
satyaseo is offline Offline
19 posts
since Mar 2009
Mar 4th, 2009
0

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

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:
php Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Mar 4th, 2009
0

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

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
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Light Poster
emiola is offline Offline
49 posts
since Jul 2008
Mar 4th, 2009
0

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

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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 4th, 2009
0

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

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
php Syntax (Toggle Plain Text)
  1. $dbcnx
parameter but I still got the same response. Take a look please
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Light Poster
emiola is offline Offline
49 posts
since Jul 2008
Mar 4th, 2009
0

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

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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP MYSQL split results?
Next Thread in PHP Forum Timeline: mysql_fetch_array failure?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC