Infinite Loop

Would this send me into an infinite loop?
To execute it only once if you leave out the “return header” but how to get back to the htm file from the php?

simple.htm

...
<body>
    <div>
        <form name = "form1" action="simple.php" method="post">
        </form>
    </div>
</body>
</html>

simple.php

&lt;?php
	$conn = mysql_connect('db1000.website.com', 'user', 'pass');
	mysql_select_db('db1000');
	$sql = "INSERT INTO `allcount` (idc,datetime,count) VALUES (NULL, NOW(),'1')";
	$result = mysql_query($sql, $conn);
	mysql_close($conn);
        return header("Location: http://www.mysite.com/simple.htm");
?&gt;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html lang="EN" dir="ltr" xmin="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

Wrap the PHP code inside an if statement that tests if it was called with POST data.

If I understand your question correctly:

It won’t loop. The simple.htm will do nothing, unless there are some inputs and submit button then it will wait until the user submits the form, then it will go to simple.php, and the redirection ( header(“Location:”) ) will take the user back to simple.htm and wait for another input. And as fellgal said, you need to check the $_POST data in your simple.php, otherwise anyone can call simple.php directly from the browser which runs the process in it.

You can also merge them into one file.For example:
simple.php
<?php
$conn = mysql_connect(‘db1000.website.com’, ‘user’, ‘pass’);
mysql_select_db(‘db1000’);
$sql = “INSERT INTO allcount (idc,datetime,count) VALUES (NULL, NOW(),‘1’)”;
$result = mysql_query($sql, $conn);
mysql_close($conn);
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd”>
<html lang=“EN” dir=“ltr” xmin=“http://www.w3.org/1999/xhtml”>
<head>
</head>
<body>
<div>
<form name = “form1” action=“simple.php” method=“post”>
</form>
</div>
</body>
</html>

Nope thats a terrible terrible idea. What you are doing is mixing domain/business logic with presentation logic, it is considered one of the worst software/application design practices.

Agreed. I like the MVC pattern. Makes code a lot easier to write, read, debug and test.

Yeah MVC is a really good idea to go with, but even if you are using other architecture design patterns like MVP or MVVM you separate business logic from presentation as well. Combining HTML presentation with PHP mysql data access code is always a poor programming practice, even procedural programmers nowadays dont do this anymore.

Thank you for your advices.Well,I’m also using MVC pattern to develop PHP programs.My code is just for PHP beginners.

I dont think newbies should be treated that much differently, once a bad habit forms its difficult to change. A newbie who gets used to coding by mixing business logic and presentation will have hard time separating job in future. At least, there is absolutely no need to tell a newbie who already write his/her program with good habits to go back to the bad programming practices. I mean, why tell a newbie who knows the idea of separation of concerns/responsibility not to do this? Why hold them back just because they are newbies?

I dont think newbies should be treated that much differently, once a bad habit forms its difficult to change. A newbie who gets used to coding by mixing business logic and presentation will have hard time separating job in future. At least, there is absolutely no need to tell a newbie who already write his/her program with good habits to go back to the bad programming practices. I mean, why tell a newbie who knows the idea of separation of concerns/responsibility not to do this? Why hold them back just because they are newbies?

Agreed, separation of concerns is an important concept that can be applied to procedural programming so why not teach and enforce it early.

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.