Jump to content

problem with php execution .,... i think?


twinytwo

Recommended Posts

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();
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

post-129942-13482403124988_thumb.png

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.