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?

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.

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

Also, make sure that the variables are receiving the data. Do an echo on $_POST; to make sure that there is something there and that the post is actually passing the value.

yes i too agree with nav33n.....

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.

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.

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.