下面为您介绍的数组插入mysql表的方法用于PHP开发,如果您之前在使用mysql表时遇到过类似的方面,不妨一看。
环境:PHP 5.0 + MYSQL
第一步:我们先来创建一个表格来存储数组变量的值,就是用mysql自带的test数据库吧
Use test; Creat table ‘NEIL’ ( ‘ID’ int(20) primary key not null, ‘Major’ varchar(25) not null);
第二步:我们来建立一个复选框页面用来模拟一个实例PHP数组 index.html
<body> <form method="post" action="show.php"> ID: <input type=”text” name=”id”><br> <input type="checkbox" name="teach[0]" value="online">Online<br> <input type="checkbox" name="teach[1]" value="video">video<br> <input type="checkbox" name="teach[2]" value="face">Face-to-Face<br> <input type="submit" value="submit"> </form>
第三步:我们来描述show.php Index.html通过post 将变量提交给show.php来处理,我们先来写show.php代码 < php $db_name=”test”; $table_name=”neil”; $connection= @mysql_connect(“localhost”,”root”,”root”) or die(mysql_error()); $db= @mysql_select_db($db_name,$connection) or die (mysql_error()); /*这里我们来进行数组变量的处理*/ $value=’’; //定义一个变量value初始化为空 Foreach($_POST[“teach”] as $key) { $value.=$key.’,’;} /*将数组值传递给中间变量key, 由key 将值传依次递给变量value */ /*插入语句*/ $sql=”insert into neil values (‘$_POST[id]’,’$value’)”; $query= @mysql_query($sql,$connection) or die(mysql_error()); >
到这里这个php中数组插入mysql表的方法就结束了。