MNSarahG Posted January 28, 2008 Share Posted January 28, 2008 Hi, I'm trying to run this script (the values come from an HTML form on another page, I put that code below): <?php $username="xxx"; $password="xxx"; $database="xxx"; $firstName=$_POST['firstName']; $lastName=$_POST['lastName']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO employees VALUES ('','$firstName','$lastName')"; mysql_query($query); mysql_close(); ?> On my actual script, I've got my username/pw/etc filled in, so that's not it... am I missing something else really simple? All this yields is a blank page. When I look at the employees table w/ MySQL Query Browser, I see that no entries are added. Here's my HTML form... <form action="insert.php" method="post"> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> <input type="Submit"> </form> On the same site, I setup a Phorum (PHP/MySQL based) message board that works with my database, so I'm sure that my server is setup properly. I know this is ridiculously basic, but it's been driving me crazy for at least a week now and, again, help is extremely appreciated. If you live in Minnesota, I'll buy you a beer. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/ Share on other sites More sharing options...
prime Posted January 28, 2008 Share Posted January 28, 2008 Try changing the query to something like this $query = "INSERT INTO employees (firstname,lastname) VALUES ('$firstname','$lastname')"; does that work? Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451653 Share on other sites More sharing options...
MNSarahG Posted January 28, 2008 Author Share Posted January 28, 2008 No luck... but thanks for the reply. I'm running PHP 5 here - not sure what sort of difference that makes - but maybe it's relevant. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451663 Share on other sites More sharing options...
revraz Posted January 28, 2008 Share Posted January 28, 2008 It should be blank after you hit submit because your .php page doesn't do anything after the insert. Also, change mysql_query($query); to mysql_query($query) or die (mysql_error()); And report the error. Hi, I'm trying to run this script (the values come from an HTML form on another page, I put that code below): <?php $username="xxx"; $password="xxx"; $database="xxx"; $firstName=$_POST['firstName']; $lastName=$_POST['lastName']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO employees VALUES ('','$firstName','$lastName')"; mysql_query($query); mysql_close(); ?> On my actual script, I've got my username/pw/etc filled in, so that's not it... am I missing something else really simple? All this yields is a blank page. When I look at the employees table w/ MySQL Query Browser, I see that no entries are added. Here's my HTML form... <form action="insert.php" method="post"> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> <input type="Submit"> </form> On the same site, I setup a Phorum (PHP/MySQL based) message board that works with my database, so I'm sure that my server is setup properly. I know this is ridiculously basic, but it's been driving me crazy for at least a week now and, again, help is extremely appreciated. If you live in Minnesota, I'll buy you a beer. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451665 Share on other sites More sharing options...
nafetski Posted January 28, 2008 Share Posted January 28, 2008 <?php $username="root"; $password=""; $database="test"; $firstName=$_POST['firstName']; $lastName=$_POST['lastName']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO employee VALUES ('$firstName','$lastName')"; mysql_query($query); mysql_close(); ?> <form action="formtest.php" method="post"> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> <input type="Submit"> </form> That works for me. DB name of test, database called employee, and the 2 fields as firstname and lastname. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451669 Share on other sites More sharing options...
MNSarahG Posted January 28, 2008 Author Share Posted January 28, 2008 In response to the suggestions above... I ran this: <?php $username="xxx"; $password="xxx"; $database="xxx"; $firstName=$_POST['firstName']; $lastName=$_POST['lastName']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO employees (firstName,lastName) VALUES ('$firstName','$lastName')"; mysql_query($query) or die (mysql_error()); mysql_close(); ?> And - again - got a blank page and nothing new in the DB. No errors or anything. This is probably significant, but I just haven't got a clue what it means and am double-confused since my message board works. Any idea what to troubleshoot next? Thanks again guys. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451676 Share on other sites More sharing options...
revraz Posted January 28, 2008 Share Posted January 28, 2008 Is the table name employees? Are the fieldnames firstName and lastName? Are the fieldnames set to VARCHAR? Remove the @ before mysql_select_db Verify the DB name as well as the username, pw And to restate, even if it works you'll get a blank screen because there is nothing after the query that will give you any output. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451680 Share on other sites More sharing options...
MNSarahG Posted January 28, 2008 Author Share Posted January 28, 2008 I got rid of the @ sign and double-checked my username/PW/database name. Those were all correct, and the script still didn't update the employees table. The account I'm using has permission to Select, Delete, Update, Insert, Create and Drop stuff in the DB. PHP and the MySQL are running on a machine here in my office (I'm working on a company intranet site). Again, not sure if it's relevant, but maybe it makes a difference. And thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451689 Share on other sites More sharing options...
revraz Posted January 28, 2008 Share Posted January 28, 2008 Post the DB structure. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451690 Share on other sites More sharing options...
nethnet Posted January 28, 2008 Share Posted January 28, 2008 Try something like this: $bool = mysql_query($query); if ($bool == TRUE) echo "The query was executed successfully."; else echo "There was an error with the MySQL query."; That way we at least have some output to help us see if the query is being executed properly. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451700 Share on other sites More sharing options...
revraz Posted January 28, 2008 Share Posted January 28, 2008 I would also tack on echo "There was an error with the MySQL query." .$query; Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451704 Share on other sites More sharing options...
MNSarahG Posted January 28, 2008 Author Share Posted January 28, 2008 My DB looks like this in the query browser - the DB is called greenspring and the table I'm working with is employees. Nethnet... I tried running that and got a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451706 Share on other sites More sharing options...
nethnet Posted January 28, 2008 Share Posted January 28, 2008 Do you have error reporting turned off? If you ran that script, you should have an output no matter what, since both the if and else statements output text. If you have error reporting turned off then perhaps your script is encountering a fatal error and not executing fully, and you aren't being told of it. Check your error logs or set error reporting to on. Quote Link to comment https://forums.phpfreaks.com/topic/88264-getting-started-with-phpthe-easiest-question-in-this-forum-probably/#findComment-451714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.