twinytwo Posted December 7, 2011 Share Posted December 7, 2011 hey guys... below is my code... when i hit the "submit" button the browser displays the php file rather than writing to the db... can anyone see what im doing wrong .. thanks <table width="300" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td><form name="form1" method="post" action="insert_ac.php"> <table width="100%" border="0" cellspacing="1" cellpadding="3"> <tr> <td colspan="3"><strong>Insert Data Into mySQL Database </strong></td> </tr> <tr> <td width="71">Name</td> <td width="6">:</td> <td width="301"><input name="name" type="text" id="name"></td> </tr> <tr> <td>Lastname</td> <td>:</td> <td><input name="lastname" type="text" id="lastname"></td> </tr> <tr> <td>Email</td> <td>:</td> <td><input name="email" type="text" id="email"></td> </tr> <tr> <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </form> </td> </tr> </table> <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Get values from form $name=$_POST['name']; $lastname=$_POST['lastname']; $email=$_POST['email']; // Insert data into mysql $sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; echo "<BR>"; echo "<a href='insert.php'>Back to main page</a>"; } else { echo "ERROR"; } // close connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/ Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 What do you mean by " displays the php file"? What exactly does it output to the browser? Can you attach a pic? Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295512 Share on other sites More sharing options...
twinytwo Posted December 7, 2011 Author Share Posted December 7, 2011 What do you mean by " displays the php file"? What exactly does it output to the browser? Can you attach a pic? it displays the second block of code exactly.. ie the PHP stuff.... Could it be something with apache? Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295515 Share on other sites More sharing options...
twinytwo Posted December 7, 2011 Author Share Posted December 7, 2011 What do you mean by " displays the php file"? What exactly does it output to the browser? Can you attach a pic? it displays the second block of code exactly.. ie the PHP stuff.... Could it be something with apache? SO what im getting is this.... Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295521 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Then Apache is not configured to parse php files. It could be as simple as adding this to your apache config AddType application/x-httpd-php .php Or you may not even have php installed. What steps did you take to install Apache/PHP? Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295548 Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2011 Share Posted December 8, 2011 Based on the address bar in your browser's screen shot, I would say that you didn't actually browse to the URL of the form, so when the form was submitted, it didn't actually request the .php file through the web server on your computer, and the php code wasn't parsed and interpreted as php code. Use: http://localhost/name_of_your_form.php Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295560 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Based on the address bar in your browser's screen shot, I would say that you didn't actually browse to the URL of the form, so when the form was submitted, it didn't actually request the .php file through the web server on your computer, and the php code wasn't parsed and interpreted as php code. Use: http://localhost/name_of_your_form.php Ah, good eye. Me thinks that would be the problem. Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295562 Share on other sites More sharing options...
twinytwo Posted December 8, 2011 Author Share Posted December 8, 2011 Based on the address bar in your browser's screen shot, I would say that you didn't actually browse to the URL of the form, so when the form was submitted, it didn't actually request the .php file through the web server on your computer, and the php code wasn't parsed and interpreted as php code. Use: http://localhost/name_of_your_form.php Ah, good eye. Me thinks that would be the problem. Thanks for that guys.... but where/how would i change that? Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295706 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Open your web browser, and go to http://localhost Any PHP files will need to be in the server document root. This is defined in Apache's virtual host config file. So if you put "insert_ac.php" in the document root (which appears to be C:\wamp\www in your case) then you would then type http://localhost/insert_ac.php into your web browser. Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295714 Share on other sites More sharing options...
twinytwo Posted December 8, 2011 Author Share Posted December 8, 2011 Open your web browser, and go to http://localhost Any PHP files will need to be in the server document root. This is defined in Apache's virtual host config file. So if you put "insert_ac.php" in the document root (which appears to be C:\wamp\www in your case) then you would then type http://localhost/insert_ac.php into your web browser. ok.. have done that.... now i get "Notice: Undefined index: name in C:\wamp\www\insert_ac.php on line 14" screenshot below Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295718 Share on other sites More sharing options...
ddubs Posted December 8, 2011 Share Posted December 8, 2011 Undefined index "name" means your code is written to assume that $_POST['name'] will exist. The notice is telling you that it doesn't exist. So you didn't POST the proper data that you were expecting to see. Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295740 Share on other sites More sharing options...
twinytwo Posted December 8, 2011 Author Share Posted December 8, 2011 Undefined index "name" means your code is written to assume that $_POST['name'] will exist. The notice is telling you that it doesn't exist. So you didn't POST the proper data that you were expecting to see. See this may be my problem. On the html page i have the text boxes and the "submit" calling the php function that will add the data to the database. But I dont get the error when i click the button, just the display showing the actual script. I only get the error when i go to localhost/###_ac.php or whatever it is. Im thinking when the php script is called it isnt actually "running" Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295756 Share on other sites More sharing options...
ddubs Posted December 8, 2011 Share Posted December 8, 2011 It runs, it just doesn't have the data since you are navigating directly to it instead of POSTing data to it from a form submission. Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295758 Share on other sites More sharing options...
twinytwo Posted December 8, 2011 Author Share Posted December 8, 2011 It runs, it just doesn't have the data since you are navigating directly to it instead of POSTing data to it from a form submission. Ok... so what would i change in the code? Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295826 Share on other sites More sharing options...
kicken Posted December 8, 2011 Share Posted December 8, 2011 See this may be my problem. On the html page i have the text boxes and the "submit" calling the php function that will add the data to the database. But I dont get the error when i click the button, just the display showing the actual script. I only get the error when i go to localhost/###_ac.php or whatever it is. Make sure your loading the HTML file through the server as well, that way when your form posts it will post to the server. Is if your form was say insert.html, you load http://localhost/insert.html and you'd set your form's action as action="insert_ac.php". When your working with PHP you have to make sure it is always run through a server. So if you have a separate html file, don't just double-click it to open it in a browser. Open your browser and then go to the URL for that file. Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295831 Share on other sites More sharing options...
twinytwo Posted December 8, 2011 Author Share Posted December 8, 2011 See this may be my problem. On the html page i have the text boxes and the "submit" calling the php function that will add the data to the database. But I dont get the error when i click the button, just the display showing the actual script. I only get the error when i go to localhost/###_ac.php or whatever it is. Make sure your loading the HTML file through the server as well, that way when your form posts it will post to the server. Is if your form was say insert.html, you load http://localhost/insert.html and you'd set your form's action as action="insert_ac.php". When your working with PHP you have to make sure it is always run through a server. So if you have a separate html file, don't just double-click it to open it in a browser. Open your browser and then go to the URL for that file. Thanks guys.... Kicken you may have just saved my life Quote Link to comment https://forums.phpfreaks.com/topic/252706-problem-with-php-execution-i-think/#findComment-1295837 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.