2017-12-26 35 views
-1

이것은 edit.php의 코드입니다. 편집을 누르면이 페이지가 열리고 해당 줄이 편집됩니다.편집 기능이 작동하지 않습니다. PHP MySql

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<?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, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, $error){ 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
    <head> 
     <title>Edit Entries</title> 
    </head> 
    <body><?php // if there are any errors, display them 
     if ($error != ''){echo ' 
     <div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
     } 
    ?> 
     <div class="maindiv"> 
      <?php include("includes/head.php");?> 
      <?php include("menu.php");?> 
      <div class="form_div"> 
       <div class="title"><h2>Updating Report for ID: <?php echo $id;?></p></h2> </div> 
       <form action="" method="post"> 
        <link rel="stylesheet" href="css\insert.css" type="text/css" /> 
        <link rel="stylesheet" href="css\navcss.css" type="text/css" /> 
        <input type="hidden" name="id" value="<?php echo $id; ?>"/> 
        <label>Name:</label><b><label style="margin-left:24em">الاسم</b></label><br /> 
        <input class="input" type="text" name="name" value="<?php echo $name; ?>" /><br /> 
        <label>Telephone Number:</label><b><label style="margin-left:15em">رقم الهاتف</b><br /> 
        <input class="input" type="text" name="telephone_number" value="<?php echo $telephone_number; ?>" /><br /> 
        <label>Email:</label></label><b><label style="margin-left:20em">البريد الإلكتروني</b></label> 
        <input class="input" type="text" name="email" value="<?php echo $email; ?>" /><br /> 
        <label>Job Title:</label></label><b><label style="margin-left:19em">المسمى الوظيفي</b></label> 
        <input class="input" type="text" name="job_title" value="<?php echo $job_title; ?>" /><br /> 
        <label>Work Place:</label></label><b><label style="margin-left:19em">جهه العمل</b></label> 
        <input class="input" type="text" name="workplace" value="<?php echo $workplace; ?>" /><br /> 
        <label>Country:</label></label><b><label style="margin-left:23em">الدولة</b></label> 
        <input class="input" type="text" name="country" value="<?php echo $country; ?>" /><br /> 
        <label>Nationality:</label></label><b><label style="margin-left:21em">الجنسية</b></label> 
        <input class="input" type="text" name="nationality" value="<?php echo $nationality; ?>" /><br /> 
        <p>* Required</p> 
        <input class="submit" type="submit" name="submit" value="Update Record" /> 
        <button class="btnSubmit" type="submit" value="Submit" onclick="history.back();return false;">Return to previous page</button> 
       </form> 
      </div> 
     </div> 
    </body> 
</html> 

<?php } // connect to the database 
    include('connect.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']; 
      $name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); 
      $telephone_number = mysql_real_escape_string(htmlspecialchars($_POST['telephone_number'])); 
      $email = mysql_real_escape_string(htmlspecialchars($_POST['email'])); 
      $job_title = mysql_real_escape_string(htmlspecialchars($_POST['job_title'])); 
      $workplace = mysql_real_escape_string(htmlspecialchars($_POST['workplace'])); 
      $country = mysql_real_escape_string(htmlspecialchars($_POST['country'])); 
      $nationality = mysql_real_escape_string(htmlspecialchars($_POST['nationality']));// check that firstname/lastname fields are both filled in 
      if ($name == ''){// generate error message 
       $error = 'ERROR: Please fill in all required fields!';//error, display form 
       renderForm($id, $name, $telephone_number, $email, $job_title, $workplace, $country, $nationality, $error); 
      } 
      else{// save the data to the database 
       $link->query("UPDATE conf SET name='$name', telephone_number='$telephone_number',email='$email',job_title='$job_title',workplace='$workplace',country='$country',nationality='$nationality' WHERE id=$id");// 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 = $link->query("SELECT * FROM conf WHERE id=$id"); 
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);// check that the 'id' matches up with a row in the databse 
      if($row){// get data from db 
       $name=$row['name']; 
       $telephone_number = $row['telephone_number']; 
       $email = $row['email']; 
       $job_title = $row['job_title']; 
       $workplace = $row['workplace']; 
       $country = $row['country']; 
       $nationality = $row['nationality'];// show form //renderForm($id, $first_name,$emp_number,$department,$email, ''); 
       renderForm($id, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, ''); 
      } 
      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!'; 
     } 
    } 
?>  

그것은 MySQL은 내가 구문 아래 사용하므로 사용되지 않습니다 첫 번째 경고를 제공하지만 여전히 오류를 제공합니다 :

mysqli_real_escape_string(htmlspecialchars($link,$_POST['name'])); 

두 번째 주요 오류의 기부는이 오류 메시지에 저를 받아 모든 수 있다는 것입니다 양식 필드가 비어 있습니다. 표시되는 행은 항상 다음과 같습니다.

ERROR: Please fill in all required fields!

Please Guide!

+0

'$의 link'가 mysqli_real_escape_string'의 첫 번째 인수해야한다()',하지 'htmlspecialchars()'에 추가하십시오. 또한 데이터베이스에 데이터를 저장할 때'htmlspecialchars() '를 호출 할 이유가 없습니다. 웹 페이지에 데이터를 표시 할 때만 사용해야합니다. – Barmar

답변

0
$servername = "localhost:3306"; 
     $username = "root"; 
     $password = "<Password here>"; 
     $dbname = "TUTORIALS"; 

     // Create connection 
     $conn = new mysqli($servername, $username, $password, $dbname); 

     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 
     $sql = "INSERT INTO tutorials_inf(name)VALUES ('".$_POST["name"]."')"; 

     if (mysqli_query($conn, $sql)) { 
      echo "New record created successfully"; 
     } else { 
      echo "Error: " . $sql . "" . mysqli_error($conn); 
     } 
     $conn->close(); 
    } 
+0

무슨 일 이니? @bmodanwal 내가 편집을 요구하지 않았다. –

0
$link->query($conn,"UPDATE conf SET name='$name', telephone_number='$telephone_number',email='$email',job_title='$job_title',workplace='$workplace',country='$country',nationality='$nationality' WHERE id=$id"); 
+0

같은 오류 .. 오류 : 모든 필수 입력란을 채워주세요! –

0
내가 내-자체를 해결

... 아래

코드 ...

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<?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, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, $error) 

{ 

?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

<html> 

<head> 

<title>Edit Entries</title> 

</head> 

<body> 

<?php 

// if there are any errors, display them 

if ($error != '') 

{ 

echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 

} 

?> 

<div class="maindiv"> 
<?php include("includes/head.php");?> 
<?php include("menu.php");?> 
     <!--HTML form --> 
      <div class="form_div"> 
      <div class="title"><h2>Updating Report for ID: <?php echo $id;?></p></h2> </div> 
<form action="" method="post"> 

<link rel="stylesheet" href="css\insert.css" type="text/css" /> 
<link rel="stylesheet" href="css\navcss.css" type="text/css" /> 
<input type="hidden" name="id" value="<?php echo $id; ?>"/> 

        <label>Name:</label><b><label style="margin-left:24em">الاسم</b></label> 
        <br /> 
        <input class="input" type="text" name="name" value="<?php echo $name; ?>" /> 
        <br /> 
        <label>Telephone Number:</label><b><label style="margin-left:15em">رقم الهاتف</b> 
        <br /> 
        <input class="input" type="text" name="telephone_number" value="<?php echo $telephone_number; ?>" /> 
        <br /> 
        <label>Email:</label></label><b><label style="margin-left:20em">البريد الإلكتروني</b></label>  
        <input class="input" type="text" name="email" value="<?php echo $email; ?>" /> 
        <br /> 
        <label>Job Title:</label></label><b><label style="margin-left:19em">المسمى الوظيفي</b></label>  
        <input class="input" type="text" name="job_title" value="<?php echo $job_title; ?>" /> 
        <br /> 
        <label>Work Place:</label></label><b><label style="margin-left:19em">جهه العمل</b></label>  
        <input class="input" type="text" name="workplace" value="<?php echo $workplace; ?>" /> 
        <br /> 
        <label>Country:</label></label><b><label style="margin-left:23em">الدولة</b></label>  
        <input class="input" type="text" name="country" value="<?php echo $country; ?>" /> 
        <br /> 
        <label>Nationality:</label></label><b><label style="margin-left:21em">الجنسية</b></label>  
        <input class="input" type="text" name="nationality" value="<?php echo $nationality; ?>" /> 
        <br /> 
<p>* Required</p> 
<input class="submit" type="submit" name="submit" value="Update Record" /> 
<button class="btnSubmit" type="submit" value="Submit" onclick="history.back(); return false;">Return to previous page</button> 


</form> 
</div> 
</div> 


</body> 

</html> 

<?php 

} 

// connect to the database 

$mysqli = new mysqli("sql213.byethost7.com", "b7_21234466", "mazhar2012", "b7_21234466_conference"); 



// 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']; 

$name = $mysqli->real_escape_string($_POST['name']); 

//$name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); 

//$last_name = mysql_real_escape_string(htmlspecialchars($_POST['last_name'])); 

$telephone_number = $mysqli->real_escape_string($_POST['telephone_number']); 

$email = $mysqli->real_escape_string($_POST['email']); 

$job_title = $mysqli->real_escape_string($_POST['job_title']); 

$workplace = $mysqli->real_escape_string($_POST['workplace']); 

$country = $mysqli->real_escape_string($_POST['country']); 

$nationality = $mysqli->real_escape_string($_POST['nationality']); 


// check that firstname/lastname fields are both filled in 

if ($name == '') 

{ 

// generate error message 

$error = 'ERROR: Please fill in all required fields!'; 

//error, display form 


renderForm($id, $name, $telephone_number, $email, $job_title, $workplace, $country, $nationality, $error); 

} 

else 

{ 

// save the data to the database 

$mysqli->query("UPDATE conf SET name='$name', telephone_number='$telephone_number',email='$email',job_title='$job_title',workplace='$workplace',country='$country',nationality='$nationality' WHERE id=$id"); 



// 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 = $mysqli->query("SELECT * FROM conf WHERE id=$id"); 



$row = mysqli_fetch_array($result,MYSQLI_ASSOC); 



// check that the 'id' matches up with a row in the databse 

if($row) 

{ 

// get data from db 

     $name=$row['name']; 
     $telephone_number = $row['telephone_number']; 
     $email = $row['email']; 
     $job_title = $row['job_title']; 
     $workplace = $row['workplace']; 
     $country = $row['country']; 
     $nationality = $row['nationality']; 


// show form 

//renderForm($id, $first_name,$emp_number,$department,$email, ''); 
renderForm($id, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, ''); 

} 

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!'; 

} 

} 

?>