简单插入到MySQL数据库

In previous articles I’ve covered the creation of a MySQL database, and how to select information from it. In this article, I’ll cover insertion of user data into a table.

在先前的文章中,我介绍了MySQL数据库的创建以及如何从中选择信息 。 在本文中,我将介绍如何将用户数据插入表中。

Insertion of data is usually done via a web form. There are many reasons we might want to do this: the obvious cause is to record input from a user, such as a comment in a blog, or an order for a product. Here, we’ll create a simple version of the former.

数据插入通常是通过Web表单完成的。 我们可能要这样做的原因有很多:显而易见的原因是记录用户的输入,例如博客中的评论或产品的订单。 在这里,我们将创建前者的简单版本。

First, we’ll create a simple HTML5 form on a page:

首先,我们将在页面上创建一个简单HTML5 表单

<form action="<?=$_SERVER['PHP_SELF']?>" method="post" role="form">
	<fieldset>
		<legend>Please enter your comment</legend>
		<label for="name" accesskey="n">
			Your name
		</label>
		<input type="text" size="38" maxlength="36" name="name" id="name">
		<label for="email" accesskey="e">
			Your eMail address
		</label>
		<input type="email" size="68" maxlength="66" name="email" id="email">
		<label for="comment" accesskey="c">
			Comment
		</label>
		<textarea name="comment" id="comment" rows="5" columns="66">
		</textarea>
		<input type="submit" value="Post" name="submit" id="submit">
	</fieldset>
</form>

This form submits to itself; unlike previous examples, we’ll make every action take place on this page, rather than splitting it across several pages (a form.html and formhandler.php, for example).

该表格提交给自己; 与前面的示例不同,我们将在此页面上执行所有操作,而不是将其拆分到多个页面上(例如, form.htmlformhandler.php )。

Note that we have provided our submit input with a name. This is one way of testing that the form has been submitted: as a named input, the submit button will be converted into a variable, one that we can test. (Obviously, the variable will not exist if the form has not been submitted).

请注意,我们为submit输入提供了名称。 这是测试表单已提交的一种方法:作为命名输入, submit按钮将转换为变量,我们可以测试该变量。 (显然,如果尚未提交表单,则该变量将不存在)。

At the top of the form page we’ll write a connection to a database, and connect to an existing comments table within it.

在表单页面的顶部,我们将写一个到数据库的连接,并连接到其中的现有注释表。

<?php $mysql_connection = mysql_connect("server", "user", "password") or 	
	trigger_error(mysql_error(),E_USER_ERROR);
	mysql_select_db("comments", $mysql_connection);?>

The comments table has fields that reflect those of the form (name and email as VARCHAR fields with the same character limits as the matching form fields, plus two more: an INT field named commentno that will automatically record the comment number via auto increment, and act as a primary key for the table, and a commentdate field set as TIMESTAMP that will record the moment that the comment is made, named joindate.)

注释表中的字段反映了表单的字段( nameemail作为VARCHAR字段,具有与匹配表单字段相同的字符数限制),外加两个字段:一个名为commentnoINT字段,它将通过自动递增自动记录注释编号;以及用作表的主键,并将commentdate字段设置为TIMESTAMP ,该字段将记录进行注释的时间,名为joindate 。)

We’ll test that the form has been submitted, convert the variables within it, help make them safe with mysql_real_escape_string, and insert the data:

我们将测试表单是否已提交,转换其中的变量,使用mysql_real_escape_string帮助使其安全并插入数据:

<?php if (isset($_POST['submit'])) {
	$name = mysql_real_escape_string($_POST['name']);
	$comment = mysql_real_escape_string($_POST['comment']);
	$email = mysql_real_escape_string($_POST['email']);
	$insert = "INSERT INTO comments (name, email, comment)
VALUES ( '$name', '$email', '$comment') ";
	mysql_query($insert);
}

Note that we match the data we are going to insert to its appropriate database table field. (For simplicity’s sake I am ignoring any validation that should take place.)

请注意,我们将要插入的数据与相应的数据库表字段进行匹配。 (为简单起见,我忽略了应该进行的任何验证。)

We’ll show the comments recorded in the database next; because of the order in which we execute this page, it should also show the comment that has just been inserted:

接下来,我们将显示记录在数据库中的注释; 由于我们执行该页面的顺序,它还应该显示刚刚插入的注释:

<? $query = "SELECT * FROM comments ORDER BY commentate";
	$comments = mysql_query($query, $mysql_connection) or die(mysql_error());
	$comment = mysql_fetch_assoc($comments);
	do { ?>
		<p>Comment by <?=$comment['name']?> on 
		<?=$comment['date']?> </p>
		<p><?=$comment['comment']?></p>
		<?php } while ($comment = mysql_fetch_assoc($comments)); ?>

The form will go just below this, so that the user can see the most recent comments and reply to them.

该表格将位于此表格的正下方,以便用户可以查看最新评论并进行回复。

翻译自: https://thenewcode.com/447/Simple-INSERT-Into-A-MySQL-Database

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值