SoFunction
Updated on 2025-03-09

PHP adds MySQL data logging code

First create a query page:
Copy the codeThe code is as follows:

<html> 
<head> 
</head> 
<body> 
<h3>Add record</h3>
<form action="add_finish.php" method="POST"> 
Employee name: <input type="text" size=25 name="ename" value=""><br><br>
PC name: <input type="text" size=25 name="pcname" value=""><br><br>
<input type="submit" name="submit" value="submit">
</form> 
</body> 
</html> 

Everyone should remember that the technology of how to transfer values ​​between PHP pages has been introduced in previous topics. Similarly, in this page, when we click Submit, we will pass the two parameters of name and pcname to the add_finish.php page through the post method.

Next is the most important thing. Let's take a look at how to write statements to add MySQL data in PHP:
Copy the codeThe code is as follows:

<?php 
$link =mysql_connect("localhost","root","administrator password");
mysql_select_db("infosystem", $link); 
$exec="insert into info (ename,pcname) values ('".$_POST["ename"]."','".$_POST["pcname"]."')"; 
mysql_query("SET NAMES GB2312"); 
mysql_query($exec, $link); 
mysql_close($link); 
echo "Add successfully!";
?> 
The red part is the key SQL statement for PHP to add MySQL data. Then use mysql_query to execute this SQL statement.

I want to remind everyone that you must pay attention to any symbol in the program, because this program has passed normally after my test. Everyone just needs to copy all of it when copying, but you must pay attention to every detail when writing it yourself.

Another point is that everyone should make subtle modifications to the program according to their own database structure to meet their specific needs.
Today's special topic is relatively brief, roughly introducing how to add MySQL data records using PHP. I will give a more detailed explanation in a future topic.