본문 바로가기

php

주소록

contacts.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
  $mysqli = new mysqli('localhost:3307','phpuser','1234','phpuser_db');
  if($mysqli->connect_errno){
    die("Failed connect to mysql server : (".$mysqli_errno.") ".$mysqli_error);
  }
  if($_GET['mode']=='by_name')
    $sql = 'select * from contacts order by binary(name)';
  elseif($_GET['mode']=='by_addr')
    $sql = 'select * from contacts order by binary(addr)';
  else
    $sql = 'select * from contacts';
 
  $result = $mysqli->query($sql);
?>
<h2>주소록</h2>
 
<form action='add_contacts.php' method='post'>
이름 : <input type=text size=12 name=name><br>
주소 : <input type=text size=20 name=addr><br>
전화번호 : <input type=text size=12 name=phone><br>
<input type=submit value='추가'>
</form>
 
정렬기준 : <a href=<?=$_SERVER['PHP_SELF']?>>저장된 순서대로</a>
          <a href=<?=$_SERVER['PHP_SELF']?>?mode=by_name>이름순</a>
          <a href=<?=$_SERVER['PHP_SELF']?>?mode=by_addr>주소순</a>
<br><br>
 
  <table width=400>
    <tr>
      <th>번호</th><th>이름</th><th>주소</th><th>전화번호</th><th>삭제</th>
    </tr>
<?php
  $count = 1;
  while($row = $result->fetch_row())
      {
          echo "<tr>";
          echo "<td>$count</td>";
          echo "<td>$row[1]</td>";
          echo "<td>$row[2]</td>";
          echo "<td>$row[3]</td>";
          echo "<td><a href=del_contacts.php?num=$row[0]>X</a></td>";
          echo "</tr>";
      $count ++;
      }
 
      $result->free();
      $mysqli->close();
?>
  </table>
 
 
cs


add_contacts.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
  $mysqli = new mysqli('localhost:3307','phpuser','1234','phpuser_db');
  if($mysqli->connect_errno){
    die("Failed connect to mysql server : (".$mysqli_errno.") ".$mysqli_error);
  }
 
  $sql = 'insert into contacts (name, addr, phone)';
  $sql.= " values ('$_POST[name]', '$_POST[addr]', '$_POST[phone]')";
  $mysqli->query($sql);
  $mysqli->close();
 
  header("Location:contacts.php")
?>
 
cs


del_contacts.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php
  $mysqli = new mysqli('localhost:3307','phpuser','1234','phpuser_db');
  if($mysqli->connect_errno){
    die("Failed connect to mysql server : (".$mysqli_errno.") ".$mysqli_error);
  }
 
  $sql = "delete from contacts where num='$_GET[num]'";
  $mysqli->query($sql);
  $mysqli->close();
 
  header("Location:contacts.php")
?>
cs


'php' 카테고리의 다른 글

$_SERVER['REMOTE_ADDR']가 ::1 로 뜰때  (0) 2018.03.29
간이 웹하드  (0) 2018.03.27
설문조사  (0) 2018.03.27
방문 카운터  (0) 2018.03.27
Warning: mysqli_connect(): (HY000/1045): Access denied for user 오류  (0) 2018.03.27