2014-02-15 53 views
-1

我一直在更新我的會員網站,它會與mysqli的工作。我對PHP和MySQL比較陌生。 我有一個頁面,用戶可以通過發送到send_post.php的表單編輯他們的信息。 任何人都可以告訴我我的代碼有什麼問題嗎?我只是在第7行的send_post.php中出現了一個白屏和語法錯誤'意外'。更新數據庫的mysqli

這是我的表單頁面。

<?php 
// See if they are a logged in member by checking Session data 
include_once("php_includes/check_login_status.php"); 
if (isset($_SESSION['username'])) { 
// Put stored session variables into local php variable 
$username = $_SESSION['username']; 
} 
//Connect to the database through our include 
include_once "php_includes/db_conx.php"; 
// Query member data from the database and ready it for display 
$sql = "SELECT * FROM members WHERE username='$username' AND activated='1' LIMIT 1"; 
$user_query = mysqli_query($db_conx, $sql); 
// Now make sure that user exists in the table 
$numrows = mysqli_num_rows($user_query); 
if($numrows < 1){ 
echo "That user does not exist or is not yet activated, press back"; 
exit(); 

} 
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) { 
$state = $row["state"]; 
$city = $row["city"]; 
$name = $row["name"]; 
} 
?> 
<!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> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<meta name="description" content=""> 
<meta name="author" content=""> 
<link rel="shortcut icon" href="../../assets/ico/favicon.png"> 

<title>Edit</title> 
</head> 
<body> 
<br> 
<div class = "container"> 
<div align="center"> 
    <h3><br /> 
    Edit your account info here<br /> 
    <br /> 
    </h3> 
<table align="center" cellpadding="8" cellspacing="8"> 
    <form action="send_post.php" method="post" enctype="multipart/form-data" name="form" 
    id="form"> 

    <tr> 
     <td><div align="right">Name:</div></td> 
     <td><input name="city" type="text" id="city" value="<?php echo "$name"; ?>"  
    size="30" maxlength="24" /></td> 
    </tr> 
    <tr> 
     <td><div align="right">State:</div></td> 
     <td><input name="state" type="text" id="state" value="<?php echo "$state"; ?>" 

    size="30" maxlength="64" /></td> 
    </tr> 
    <tr> 
     <td><div align="right">City:</div></td> 
     <td><input name="city" type="text" id="city" value="<?php echo "$city"; ?>" 
    size="30" maxlength="24" /></td> 
    </tr>    
    <tr> 
     <td>&nbsp;</td> 
     <td><input name="Submit" type="submit" value="Submit Changes" /></td> 
    </tr> 
    </form> 
    </table> 
</div> 
</div> 
</body> 
</html> 

這是表單處理頁面。 send_post.php

<?php 
    if ($_POST['state']) { 
$city = $_POST['city']; 
$name = $_POST['name']; 
//Connecting to sql db. 
$connect = mysqli_connect("localhost","username","password","database"); 
$mysqli_query=($connect,"UPDATE members (`state`, `city`, `name` WHERE  
username='$username'"); 
VALUES ('$state', '$city', '$name')"; 
mysqli_query($connect, $query); 
mysqli_close($connect); 
echo "Your information has been successfully added to the database."; 
?> 
+0

您在send_post.php中的更新查詢不正確,更新查詢應該是=>更新'tabelName' SET'field1' = [value-1],'field2' = [value-2],'field3' = [值-3],其中1 –

+0

嘗試編輯問題的標題有一個類似的問題會找到你的問題,以便其他用戶(並回答這裏) – rubo77

回答

0

在窗體中添加一個隱藏字段這樣

<input type="hidden" name="username" value="<?php echo $username; ?>"> 

變化send_post.php

<?php 
    //checking that all the fields have been entered 
    if(isset($_POST['state']) && !empty($_POST['state'])) 
    { 
     $state = $_POST['state']; 
    } 

    if(isset($_POST['city']) && !empty($_POST['city'])) 
    { 
     $city = $_POST['city']; 
    } 

    if(isset($_POST['name']) && !empty($_POST['name'])) 
    { 
     $name = $_POST['name']; 
    } 

    if(isset($_POST['username']) && !empty($_POST['username'])) 
    { 
     $username = $_POST['username']; 
    } 

    //Connecting to sql db. 
    $mysqli = new mysqli("localhost","username","password","database"); 

    //updating database 
    $query = $mysqli->query("UPDATE members SET `state` = '$state', `city` = '$city', `name` = '$name' WHERE `username` = '$username'"); 

    //closing mysqli connection 
    $mysqli->close; 

    //echoing that the information has been added 
    echo "Your information has been successfully added to the database."; 
?> 
+0

頁面回顯「您的信息已成功添加到數據庫」。但數據庫不會更新。在檢查它報告的error_log:未定義的變量:mysqli的:: $關閉:用戶名,也 – joachim

+0

...從上面的第22行和未定義的屬性未定義的用戶名繼續。 – joachim

+0

我已經添加了一個隱藏的輸入來形成,並添加一個檢查,如果輸入用戶名 – TheSk8rJesus

0

變化

$mysqli_query=($connect,"UPDATE members (`state`, `city`, `name` WHERE  
username='$username'"); 
VALUES ('$state', '$city', '$name')"; 

$mysqli_query=($connect,"UPDATE members (`state`, `city`, `name`) VALUES ('$state', '$city', '$name') WHERE username='$username' "); 
+0

我試過,但它仍然不會更新,我仍然得到錯誤 – joachim