2016-09-09 6 views
0

나는 데이터베이스에 생성 된 모든 사용자를 유지하는 등록 양식을 만들었습니다. 자, 각각의 생성 된 사용자 옆에 버튼 (링크, 무엇이든)을 넣으려고합니다. 버튼을 클릭하면, 새로운 로그인 폼이 나타납니다. 필요한 필드를 모두 채우고 업데이트 버튼을 누르면, 편집 된 사용자가 데이터베이스에 표시됩니다. 어떤 도움이 필요합니까? 미리 감사드립니다.데이터베이스에서 파일을 편집하는 방법은 무엇입니까?

코드 :

등록 양식 : 등록 된 사용자에 대한

<?php 
$errors = []; 
$missing = []; 
?> 

<html> 
<head> 
<meta charset="utf-8"> 
<title>Facebook login</title> 
<link href="styles.css" rel="stylesheet" type="text/css"> 
</head> 

<body> 

<?php if(isset($_POST['send']) && count($missing) == 0){ 
    echo "Thank you for register on our site! You profile details are listed bellow:"; 
    ?> 
     <p>Your name: <?php echo $_POST['name']; ?></p> 
     <p>Lastname name: <?php echo $_POST['lastName']; ?></p> 
    <?php 
}else{ 
?> 

<h1>Registration</h1> 


<form method="post" action="register_user.php"> 

    <?php if ($errors || $missing) : ?> 
     <p class="warning"> Please fill all of the empty items 

     </p> 

    <?php endif; ?> 

    <p> 
    <label for="username">Username 
    <?php 
    if ($missing && in_array('username', $missing)) : ?> 
     <span class="warning"> Please enter your first name</span> 
    <?php endif; ?> 
    </label> 
    <input type="text" name="username" id="username"> 
    </p> 


    <p> 
    <label for="password">Enter your password 
    <?php 
    if ($missing && in_array('password', $missing)) : ?> 
     <span class="warning"> Please enter your password</span> 
    <?php endif; ?> 
    </label> 
    <input type="password" name="password" id="password"> 


    <p> 
    <label for="re-password">Re-Enter your password 
    <?php 
    if ($missing && in_array('re-password', $missing)) : ?> 
     <span class="warning"> Please re-enter your password</span> 
    <?php endif; ?> 
    </label> 
    <input type="re-password" name="re-password" id="re-password"> 
    </p> 



    <p> 
    <label for="name">First Name 
    <?php 
    if ($missing && in_array('name', $missing)) : ?> 
     <span class="warning"> Please enter your first name</span> 
    <?php endif; ?> 
    </label> 
    <input type="text" name="name" id="firstName"> 
    </p> 

    <p> 
    <label for="lastName">Last Name 
    <?php 
    if ($missing && in_array('lastName', $missing)) : ?> 
     <span class="warning"> Please enter your last name</span> 
    <?php endif; ?> 
    </label> 
    <input type="name" name="lastName" id="lastName"> 
    </p> 

    <p> 
    <label for="email">Enter your email adress 
    <?php 
    if ($missing && in_array('email', $missing)) : ?> 
     <span class="warning"> Please enter your email adress</span> 
    <?php endif; ?> 

    </label> 
    <input type="email" name="email" id="email"> 
    </p> 

    <h3>Birthday</h3> 
    <p> 
    <label for="Select Month"> Select Month 
    <?php 
    if ($missing && in_array('month', $missing)) : ?> 
     <span class="warning"> Please select month</span> 
    <?php endif; ?> 
    </label> 
    <select name="month" id="month" > 
    <option value="Month"> Month </option> 
    <option value="Jan"> January </option> 
    <option value="Feb"> February </option> 
    <option value="Mar"> March </option> 
    <option value="Apr"> April </option> 
    <option value="May"> May </option> 
    <option value="June"> June </option> 
    <option value="July"> July </option> 
    <option value="Aug"> August </option> 
    <option value="September"> September </option> 
    <option value="Oct"> October </option> 
    <option value="Nov"> November </option> 
    <option value="Dec"> December </option> 
    </select> 
    </p> 

    <p> 
    <label for="Select Day"> Select Day 
    <?php 
    if ($missing && in_array('day', $missing)) : ?> 
     <span class="warning"> Please select Day</span> 
    <?php endif; ?> 
    </label> 
    <select name="day" id="day" > 
    <option value="Day"> Day </option> 
    <option value="1"> 1 </option> 
    <option value="2"> 2 </option> 
    <option value="3"> 3 </option> 
    <option value="4"> 4 </option> 
    <option value="5"> 5 </option> 
    <option value="6"> 6 </option> 
    <option value="7"> 7 </option> 
    <option value="8"> 8 </option> 
    <option value="9"> 9 </option> 
    <option value="10"> 10 </option> 
    <option value="11"> 11 </option> 
    <option value="12"> 12 </option> 
    <option value="13"> 13 </option> 
    <option value="14"> 14 </option> 
    <option value="15"> 15 </option> 
    <option value="16"> 16 </option> 
    <option value="17"> 17 </option> 
    <option value="18"> 18 </option> 
    <option value="19"> 19 </option> 
    <option value="20"> 20 </option> 
    <option value="21"> 21 </option> 
    <option value="22"> 22 </option> 
    <option value="23"> 23 </option> 
    <option value="24"> 24 </option> 
    <option value="25"> 25 </option> 
    <option value="26"> 26 </option> 
    <option value="27"> 27 </option> 
    <option value="27"> 27 </option> 
    <option value="28"> 28 </option> 
    <option value="29"> 29 </option> 
    <option value="30"> 30 </option> 
    <option value="31"> 31 </option>  
    </select> 
    </p> 

    <p> 
    <label for="Select Year"> Select Year 
    <?php 
    if ($missing && in_array('year', $missing)) : ?> 
     <span class="warning"> Please select year</span> 
    <?php endif; ?> 
    </label> 
    <select name="year" id="year" > 
    <option value="Year"> Year </option> 
    <option value="1990"> 1990 </option> 
    <option value="1991"> 1991 </option> 
    <option value="1992"> 1992 </option> 
    <option value="1993"> 1993 </option> 
    <option value="1994"> 1994 </option> 
    <option value="1995"> 1995 </option> 
    </select> 
    </p> 

    <p> 
    <label for="Select gender"> Select gender 

    <?php 
    if ($missing && in_array('Select gender', $missing)) : ?> 
     <span class="warning"> Please select your gender</span> 
    <?php endif; ?> 
    </label> 
    <input type="radio" name="gender" id="malegender"> 
    <label for="gender" > Male</label> 
    <input type="radio" name="gender" id="femalegender"> 
    <label for="gender"> Female</label> 
    </p> 

    <input type="submit" name="send" id="send" value="send"> 

</form> 
<?php } ?> 
</body> 
</html> 

코드 :

Register user: 

<?php 
$errors = []; 
$missing = []; 

if($_SERVER['REQUEST_METHOD'] == "POST"){ 

    $username = $_POST["username"]; 
    $password = $_POST["password"]; 
    $firstname = $_POST["name"]; 
    $lastName = $_POST["lastName"]; 
    $email = $_POST["email"]; 
    $month = $_POST["month"]; 
    $day = $_POST["day"]; 
    $year = $_POST["year"]; 
    $gender = $_POST["gender"]; 

    if (!isset($_POST['username']) || strlen($_POST['username']) < 3) { 
     $missing[] = "username"; 
    }else{ 
    } 

    if (!isset($_POST['name']) || strlen($_POST['name']) < 3) { 
     $missing[] = "name"; 
    }else{ 
    } 

    if (!isset($_POST['lastName'])|| strlen($_POST['lastName']) < 3) { 
     $missing[] = "lastName"; 
    } 

    if (!isset($_POST['email']) || strlen($_POST['email']) < 3) { 
     $missing[] = "email"; 
    } 

    if (!isset($_POST['re-password']) || strlen($_POST['re-password']) < 3) { 
     $missing[] = "re-password"; 
    } 

    if (!isset($_POST['password']) || strlen($_POST['password']) < 3) { 
     $missing[] = "password"; 
    } 

    if (!isset($_POST['month']) || $_POST['month'] == "Month") { 
     $missing[] = "month"; 
    } 

    if (!isset($_POST['year']) || $_POST['year'] == "Year") { 
     $missing[] = "year"; 
    } 

if (!isset($_POST['day']) || $_POST['day'] == "Day") { 
    $missing[] = "day"; 
} 

if (!isset($_POST['gender'])) { 
    $missing[] = "Select gender"; 
} 


if(count($missing) > 0){ 
    echo "validation error!!!"; 
}else{ 


    $dsn = "mysql:host="; 
    $host = "localhost"; 
    $db = "registration"; 

    $dsn .= $host.";"; 
    $dsn .= "dbname=".$db; 

    $user = 'root'; 

    $dbh = new PDO($dsn, $user); 

    $selectQuery = "SELECT * FROM register_table WHERE username = :username OR email_adress = :email"; 
    $stmt = $dbh->prepare($selectQuery); 

    $stmt->bindParam(':username', $username); 
    $stmt->bindParam(':email', $email); 

    $stmt->execute(); 

    if($stmt->rowCount() > 0){ 

     echo "User exists!!!"; 
    }else{ 
     $stmtInsert = $dbh->prepare("INSERT INTO register_table (username, password, first_name, last_name, email_adress, month, day, year, gender) VALUES (:username, :password, :name, :lastName, :email, :month, :day, :year, :gender)"); 

     $stmtInsert->bindParam(':username', $username); 
     $stmtInsert->bindParam(':password', $password); 
     $stmtInsert->bindParam(':name', $firstname); 
     $stmtInsert->bindParam(':lastName', $lastName); 
     $stmtInsert->bindParam(':email', $email); 
     $stmtInsert->bindParam(':month', $month); 
     $stmtInsert->bindParam(':day', $day); 
     $stmtInsert->bindParam(':year', $year); 
     $stmtInsert->bindParam(':gender', $gender); 

     $stmtInsert->execute(); 

     $arr = $stmtInsert->errorInfo(); 

     print_r($arr); 

     echo "New records created successfully!!!"; 
    } 


    $editQuery = "SELECT username FROM register_table"; 
    $editResult = mysql_query($editQuery); 

    while($user = mysql_fetch_array($editQuery)) 


    $dbh = null; 
} 

} 
+0

는 한 번 더 시간 PLZ 설명 할 수 – Laith

+0

을 mysqli로 변환하고 - 내가 원하는 그 옆에있는 모든 사용자를위한 편집 버튼을 넣으십시오. 편집 버튼을 클릭하면 해당 사용자에 대한 정보를 편집하고 싶습니다. – Tomi

+0

주어진 사용자에 대한 업데이트 쿼리를 수행하기 만하면됩니다. 귀하의 질문에 대한 코드를 게시하지 않았기 때문에 귀하를 도울 수 없습니다. 또한 여기에서했던 것처럼 일반 텍스트로 암호를 저장하지 마십시오. –

답변

0

이것은 내가 내 데이터베이스 코드의 2 세트에서 사용하는 코드입니다 - view.php 표시 목록의 각 레코드 옆에 편집 옵션이있는 레코드를 선택한 다음 편집을 클릭하면 edit.php가 열리고 세부 사항을 변경하고 저장할 폼이 표시됩니다. 필요한 경우 테이블 구조와 내용을 보낼 수 있습니다. 꽤 많은 코드가 있지만 나는 원하는대로 정확하게 작동하는 시스템을 전체적으로 보여주고 싶습니다. 그것은 MySQL을하지만, 나는) =

view.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<?php 
/* 
VIEW.PHP 
Displays all data from 'players' table 
*/ 
// connect to the database 
include('connect-db.php'); 
// get results from database 
$result = mysql_query("SELECT * FROM stats") 
or die(mysql_error()); 
// display data in table 
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>"; 
echo "<table border='1' cellpadding='10'>"; 
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> <th></th> <th></th> </tr>"; 
// loop through results of database query, displaying them in the table 
while($row = mysql_fetch_array($result)) { 
// echo out the contents of each row into a table 
echo "<tr>"; 
echo '<td>' . $row['date'] . '</td>'; 
echo '<td>' . $row['hometeam'] . '</td>'; 
echo '<td>' . $row['fthg'] . '</td>'; 
echo '<td>' . $row['ftag'] . '</td>'; 
echo '<td>' . $row['awayteam'] . '</td>'; 
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
echo "</tr>"; 
} 
// close table> 
echo "</table>"; 
?> 
<p><a href="new.php">Add a new record</a></p> 
</body> 
</html> 

edit.php 난 정말 캔트 당신을 이해

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<?php 
/* 
EDIT.PHP 
Allows user to edit specific entry in database 
*/ 
// creates the edit record form 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 
function renderForm($id, $hometeam, $awayteam, $error) 
{ 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 
<form action="" method="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>"/> 
<div> 
<p><strong>ID:</strong> <?php echo $id; ?></p> 
<strong>First Name: *</strong> <input type="text" name="hometeam" value="<?php echo $hometeam; ?>"/><br/> 
<strong>Last Name: *</strong> <input type="text" name="awayteam" value="<?php echo $awayteam; ?>"/><br/> 
<p>* Required</p> 
<input type="submit" name="submit" value="Submit"> 
</div> 
</form> 
</body> 
</html> 
<?php 
} 
// connect to the database 
include('connect-db.php'); 
// check if the form has been submitted. If it has, process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
// confirm that the 'id' value is a valid integer before getting the form data 
if (is_numeric($_POST['id'])) 
{ 
// get form data, making sure it is valid 
$id = $_POST['id']; 
$hometeam = mysql_real_escape_string(htmlspecialchars($_POST['hometeam'])); 
$awayteam = mysql_real_escape_string(htmlspecialchars($_POST['awayteam'])); 
// check that hometeam/awayteam fields are both filled in 
if ($hometeam == '' || $awayteam == '') 
{ 
// generate error message 
$error = 'ERROR: Please fill in all required fields!'; 
//error, display form 
renderForm($id, $hometeam, $awayteam, $error); 
} 
else 
{ 
// save the data to the database 
mysql_query("UPDATE stats SET hometeam='$hometeam', awayteam='$awayteam' WHERE id='$id'") 
or die(mysql_error()); 
// once saved, redirect back to the view page 
header("Location: view.php"); 
} 
} 
else 
{ 
// if the 'id' isn't valid, display an error 
echo 'Error!'; 
} 
} 
else 
// if the form hasn't been submitted, get the data from the db and display the form 
{ 
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) 
{ 
// query db 
$id = $_GET['id']; 

$result = mysql_query("SELECT * FROM stats WHERE id=$id") 

or die(mysql_error()); 
$row = mysql_fetch_array($result); 
// check that the 'id' matches up with a row in the databse 
if($row) 
{ 
// get data from db 
$hometeam = $row['hometeam']; 
$awayteam = $row['awayteam']; 
// show form 
renderForm($id, $hometeam, $awayteam, ''); 
} 
else 
// if no match, display result 
{ 
echo "No results!"; 
} 
} 
else 
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error 
{ 
echo 'Error!'; 
} 
} 
?> 
</body> 
</html> 
관련 문제