954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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>
$dbcnx = mysql_connect("localhost","user_name","password");
if (!$dbcnx){
exit('<p>Unable to connect to the database</p>');
}
If (isset($_POST['submit']))
{
$SchoolName = $_POST['SchoolName'];
$SchoolEmail = $_POST['SchoolEmail'];
$UserName = $_POST['UserName'];
$pscode = $_POST['pscode'];

$sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode) 
VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')"); 
}
echo "Values successfully submitted to database. Thank you" ;
?>

Is the issue really with the script or with my mysql database table structure?

emiola
Junior Poster in Training
51 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

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.,

$sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode) 
VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')") or die (mysql_error());

Also check if error reporting is turned on on your server.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

yes do what nav33n says. Also check that the table and db actual exist.

jakx12
Junior Poster
125 posts since Jan 2009
Reputation Points: 10
Solved Threads: 5
 

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.

alexgv14
Light Poster
49 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

yes i too agree with nav33n.....

satyaseo
Newbie Poster
19 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 

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
$dbcnx = mysql_connect("localhost","user_name","password");
if (!$dbcnx){
exit('<p>Unable to connect to the database</p>');
}
if (isset($_POST['submit']))
{
$SchoolName = mysql_real_escape_string($_POST['SchoolName']);
$SchoolEmail = mysql_real_escape_string($_POST['SchoolEmail']);
$UserName = mysql_real_escape_string($_POST['UserName']);
$pscode = mysql_real_escape_string($_POST['pscode']);
 
$sql = mysql_query('INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode) 
VALUES ("'.$SchoolName.'", "'.$SchoolEmail.'", "'.$UserName.'", "'.$pscode.'")') or die(mysql_error()); 
}
echo 'Values successfully submitted to database. Thank you';
?>


Just a note, it has also been suggested on another topic that using single quotes wherever possible will speed up execution time.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

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>
$dbcnx = mysql_connect("localhost","lsschnln_emiolab","bembestica");
if (!$dbcnx){
exit('<p>Unable to connect to the database</p>');
}
$dbselector = mysql_select_db('lsschnln01');
if (!$dbselector){
echo('sorry database not connected');
}
If (isset($_POST['validate']))
{
$SchoolName = mysql_real_escape_string($_POST['SchoolName']);
$SchoolEmail = mysql_real_escape_string($_POST['SchoolEmail']);
$UserName = mysql_real_escape_string($_POST['UserName']);
$pscode = mysql_real_escape_string($_POST['pscode']);

$sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode) 
VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')")or 
die (mysql_error()); 
}
echo "Values successfully submitted to database. Thank you" ;
?>

nav33, will you look into this as you are immediately online now.

emiola
Junior Poster in Training
51 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

Ah! How could we miss that in your first post :icon_surprised:
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 :icon_confused: you better check if the database really exist.

P.S. Never give out your database username/password in a forum.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

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

$dbcnx


parameter but I still got the same response. Take a look please

<?php>

$dbcnx = mysql_connect("localhost","username","password");
if (!$dbcnx){
exit('<p>Unable to connect to the database</p>');
}
$dbselector = mysql_select_db('lsschnln01', $dbcnx);
if (!$dbselector){
echo('sorry database not connected');
}
If (isset($_POST['validate']))
{
$SchoolName = mysql_real_escape_string($_POST['SchoolName']);
$SchoolEmail = mysql_real_escape_string($_POST['SchoolEmail']);
$UserName = mysql_real_escape_string($_POST['UserName']);
$pscode = mysql_real_escape_string($_POST['pscode']);

$sql = mysql_query("INSERT INTO accesscarduse (SchoolName, SchoolEmail, UserName, pscode) 
VALUES ('$SchoolName', '$SchoolEmail', '$UserName', '$pscode')")or 
die (mysql_error()); 
}
echo "Values successfully submitted to database. Thank you" ;
?>

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.

emiola
Junior Poster in Training
51 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

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 :S
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.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You