您的位置:首页 > 运维架构 > Apache

零基础建站教程(html+js+php+mysql+ftp+apache)

2014-01-19 12:11 549 查看
序言:
因为公司需求,我不得不去学习建站,以前都是做后台的,没有接触这些东西,所以也是从0开始研究的,大概半个月时间,学了三门语言加一些工具捣鼓出了一个小站,在此分享,也可以说是我自己的总结和心得,希望对想建站的道友们有所帮助。
本文中提到的一些资源我都会上传的,到时大家直接下载就行了。这是在windows下建的。

part one:安装
(mysql+apache+php)我选的是这个,或者(mysql+nginx+php)貌似这个搭配更强大,毕竟是俄罗斯人花6年时间写的。懒人可以选择一件安装,找一个叫appserv的东东就行,怎么装我就不说了,不过最好不要是php6,应为同事说很多语法都变了,我不怎么清楚,不过我一开始是php6然后连接数据库各种出错,后来还是重装的,大家要注意这个问题。装软件我就不教了。装完后在咱的机器上就有了AppServ这个文件夹,咱进去看看有都有什么。



其实就是把你装好了这三个东东,上面三个咱不关心,进入www文件夹,我们写的php文件都放这里就行啦。
写个php文件,丢到里面通过http就可以访问了,再啰嗦一句,就是在你的浏览器输入http://localhost/***.php (***可以包含路径,比如我的项目就会放在一个目录下面)再再啰嗦一句,要开启你的服务。


(Apache Start)

part two:学习的次序
建站包括前台后台,我觉得比较好的学习次序是,html(两天)css(一天)java script(三天)php(三天)这当然是入门级的教程,任何语言要精通肯定不是这几天就能完成的了的,我们的目的就是能解决实际问题。html不难,但是东西还是比较多的,我们有侧重点的学,觉得需要用哪些东西就多注意一下,很多东西过一遍有印象,用的时候再去查。然后我学完js感觉学的时候很简单,学完了什么都忘记了,但后来也不影响我建站,用的时候查。php也一样。我们的重点放在这些语言的交互,这个对于初学者来说比较重要。了解一些jQuery的框架就更好了,可以使工作效率更高,哪些东西js会了以后就自己去琢磨去吧。

part three:数据的流入
我的意思是页面上的数据->mysql的这个过程怎么实现。咱从form表单开始!
<div class="regist">
<form action="registDB.php" method="post">
出版社名称: <input type = "text" name = "name" /><br /><br />
密码: <input type="password" name = "password" /><br /><br />
邮箱: <input type="text" name = "mailaddress" /><br /><br />
<input type="submit" value="注册" id="regist"/>
</form>
</div>
这样数据就被丢到“信封里”寄出去了,下面看谁来收:registDB.php
<?php
$name = $_POST["name"];
$password = $_POST["password"];
$mailaddress = $_POST["mailaddress"];
?>
这么简单就收到了,下面你爱干嘛就干嘛了,然后看看数据库操作,这php就很简单了:
<?php
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("imras",$con);
//TODO判断用户是否存在
$res = mysql_query("select name from regist where name = '$name'");
//if 存在  注册失败
if(mysql_num_rows($res))
{
echo '用户已经存在!';
}
//else 插入数据
else{
mysql_query("insert into regist (name,password,mailaddress)
values('$name','$password','$mailaddress')");
}
//关闭数据库连接
mysql_close($con);
?>
当然我是在mysql里面已经把表建好了,同样可以在程序中进行建表操作。

part three:数据的吐出
这是个mysql--->页面的过程,我们用php取数据,然后用js把它写到页面上,写到页面的过程是一个用js改变html的过程。这就体现了三者“复杂”的三角恋关系。
<?php
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die('could not connect:'.mysql_error());
}
mysql_select_db("imras",$con);
$res = mysql_query("select * from books");
while($row = mysql_fetch_array($res))
{
//echo $row['press'].' '.$row['book_name'].' '.$row['vedio_num'];
?>
<script>
var form_child_tr = document.createElement("tr");
var form = document.getElementById("all_book");
form = form.lastChild;
form.appendChild(form_child_tr);
var tr_child_th = document.createElement("th");
var th_context = document.createTextNode('<?=$row['press']?>');
tr_child_th.appendChild(th_context);
form_child_tr.appendChild(tr_child_th);
tr_child_th = document.createElement("th");
th_context = document.createTextNode("<?echo $row['book_name']?>");
tr_child_th.appendChild(th_context);
form_child_tr.appendChild(tr_child_th);
tr_child_th = document.createElement("th");
th_context = document.createTextNode("<?print $row['vedio_num']?>");
tr_child_th.appendChild(th_context);
form_child_tr.appendChild(tr_child_th);
tr_child_th = document.createElement("th");
var th_child_a = document.createElement("a");
th_child_a.href = "login.php";
tr_child_th.appendChild(th_child_a);
var a_child_img = document.createElement("img");
a_child_img.src="img/edit.png";
a_child_img.width="15";
a_child_img.height="15";
th_child_a.appendChild(a_child_img);
a_child_img = document.createElement("img");
a_child_img.src="img/delete.png";
a_child_img.width="15";
a_child_img.height="15";
th_child_a.appendChild(a_child_img);
form_child_tr.appendChild(tr_child_th);
tr_child_th = document.createElement("th");
th_child_a = document.createElement("a");
th_child_a.href = "login.php";
tr_child_th.appendChild(th_child_a);
a_child_img = document.createElement("img");
a_child_img.src="img/download.jpg";
a_child_img.width="15";
a_child_img.height="15";
th_child_a.appendChild(a_child_img);
form_child_tr.appendChild(tr_child_th);
</script>
<?php
}
mysql_close($con);
?>
这里有个很有意思的地方,也是js和php完美融合的地方,php的代码,咔嚓从12行被截断,下面全是js对html的操作(这里是个添加表格的代码)。但是js又需要php取出来的数据,怎么办?就像这样var
th_context = document.createTextNode(
'<?=$row['
press
']?>'
);

在里面再嵌php,其实这是php的一种属性,但是我为了把他们语法突出出来所以分开讲。至此,数据可以被取出到页面了。在这里我想顺便提一句,<table>标签下面其实还有个<tbody>的默认标签,所以在添加子节点的时候应该插到<tbody>下面,如果插到<table>标签下就会可能出问题。

本文出自 “viMer” 博客,请务必保留此出处http://fangnux.blog.51cto.com/7902770/1352893
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: