2014-02-16 40 views
0

我想通過覆蓋MySql中的當前數據來更新用戶配置文件。 這是不正常的,說明它回聲我 「這個名字不存在」更新用戶配置文件更新的MySql查詢使用php

這裏是我的PHP代碼:

``````

<?php 
// see if the form has been completed 
session_start(); 
include_once("php_includes/check_login_status.php"); 
include_once("php_includes/db_conx.php"); 
// Initialize any variables that the page might echo 
$u = ""; 
$firstname = ""; 
$surname = ""; 
$gender = "Male"; 
$country = ""; 
$weight = ""; 
$height = ""; 
$password = ""; 
$password2 = ""; 

if(isset($_GET["u"])){ 
    $u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']); 
} 

$sql = "SELECT * FROM users WHERE username='$u' AND activated='1' LIMIT 1"; 
$user_query = mysqli_query($db_conx, $sql); 

// check if the user exists in the database 
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) { 
    $firstname = $row["firstname"]; 
    $surname = $row["surname"]; 
    $weight = $row["weight"]; 
    $height = $row["height"]; 
    $email = $row["email"]; 
    $gender = $row ["gender"]; 
    } 
if (isset($_POST['submit'])){ 
    $firstname = $_POST['firstname']; 
    $surname = $_POST['surname']; 
    $weight = $_POST['weight']; 
    $height = $_POST['height']; 
    $email = $_POST['email']; 
    $gender = $_POST['gender']; 
mysql_connect ("localhost","root","pass123"); mysql_select_db('worldoi5_social'); 
    // check if that user exist 
    $exists = mysql_query ("SELECT * FROM users WHERE username='$u' ") or die ("query cant connect"); 
    if (mysql_num_rows ($exists) != 0) { 
    // update the description in the database  
     mysql_query("UPDATE users SET firstname='$firstname', surname='$surname', weight='$weight', height='$height' WHERE username='$u'") or die ("update could not be applied"); 
     echo "successful"; 
} else echo "the name does not exist"; 
} 
?> 

我已經代碼使用較少的代碼工作,但由於我添加了更多,停止工作,數據讀取正常,我沒有看到錯誤或錯誤。

這裏是位HTML藏漢的:

 <form action="user1.php" method="POST"> 
      <div> 
      <p>First Name: <input type="text" name="firstname" id="firstname" value="<?=$firstname?>"></p> 
      <p>Surname: <input type="text" name="surname" id="surname" value="<?=$surname?>"></p> 
      <p>Weight: <input type="text" name="weight" id="weight" value="<?=$weight?>"></p> 
      <p>Height: <input type="text" name="height" id="height" value="<?=$height?>"></p> 
      <p> <input type="submit" name="submit" id="submit" value="Update Description"></p> 
      </div> 
     </form> 
    </body> 
+2

馬上蝙蝠,混合'mysqli_ *'和'mysql_ *' - 你算算'$ mysqli_ *' +'mysql_ *'= ** NO GO。** –

+0

我之前做的是回顯mysql_query的內容,然後複製並粘貼到MySQL客戶端。 75%的時間,只是看到原始查詢可以讓你注意到一些錯誤。 – evilpenguin

+0

另外,我真的不知道你爲什麼使用'session_start();'這個。沒有分配給一個。 –

回答

0

我想出答案:

從PHP代碼行

$exists = mysql_query ("SELECT * FROM users WHERE username='$u' ")

的單一和雙重的問題重新搞砸了,所以我不得不這樣使用它;

$exists = mysql_query ("SELECT * FROM users WHERE firstname='" . $firstname . "'")

然後正確讀取它也是我決定使用名字作爲選擇