使用php更新用户配置文件更新的MySql查询

时间:2014-02-16 21:16:40

标签: php mysql mysqli

我正在尝试通过覆盖MySql中的当前数据来更新用户配置文件。 它没有正常工作它说它回应了我的“名字不存在”

这是我的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>

1 个答案:

答案 0 :(得分:0)

我想出了答案:

来自php代码行

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

单引号和双引号搞砸了,所以我不得不像这样使用它;

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

然后它正确读取它我也决定使用firstname作为选择性