php 往mysql中插入数据

php 往mysql中插入数据

<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<?php
	if(isset($_POST['add']))
	{
	$dbhost = 'localhost:3306';
	$dbuser = 'root';
	$dbpass = 'root';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass);
	if(! $conn )
	{
	  die('Could not connect to mysql');
	}

	if(! get_magic_quotes_gpc() )
	{
	   $emp_name = addslashes ($_POST['emp_name']);
	   $emp_address = addslashes ($_POST['emp_address']);
	}
	else
	{
	   $emp_name = $_POST['emp_name'];
	   $emp_address = $_POST['emp_address'];
	}
	$emp_salary = $_POST['emp_salary'];

	$sql = "INSERT INTO employee ".
		   "(emp_id, emp_name,emp_address, emp_salary) ".
		   "VALUES(1, '$emp_name','$emp_address',$emp_salary)";
	mysql_select_db('test_db');
	$retval = mysql_query( $sql, $conn );
	if(! $retval )
	{
	  die('Could not enter data: ');
	}
	echo "Entered data successfully\n";
	mysql_close($conn);
	}
	else
	{
?>
	<form method="post" action="<?php $_PHP_SELF ?>">
	<table width="400" border="0" cellspacing="1" cellpadding="2">
	<tr>
	<td width="100">Employee Name</td>
	<td><input name="emp_name" type="text" id="emp_name"></td>
	</tr>
	<tr>
	<td width="100">Employee Address</td>
	<td><input name="emp_address" type="text" id="emp_address"></td>
	</tr>
	<tr>
	<td width="100">Employee Salary</td>
	<td><input name="emp_salary" type="text" id="emp_salary"></td>
	</tr>
	<tr>
	<td width="100"> </td>
	<td> </td>
	</tr>
	<tr>
	<td width="100"> </td>
	<td>
	<input name="add" type="submit" id="add" value="Add Employee">
	</td>
	</tr>
	</table>
	</form>
<?php
}
?>
</body>
</html>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值