更新数据库中的记录

时间:2014-03-13 17:59:43

标签: php mysql

我正在为学生创建一个示例更新页面。它很简单,但不保存到数据库中。我是PHP的新手,在这个页面中,它从数据库中检索数据并显示在文本框中,但是当我保存它时,它会将我重定向到正确的页面,但我的数据已经消失......

$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'testing';

// Connect to server and select databse.
mysql_connect("$server", "$user", "$pass")or die("cannot connect");
mysql_select_db("$db")or die("cannot select DB");

if(isset($_POST["submit"]))
{
    $name = $_POST["name"][$username];
    $address = $_POST["address"][$username];
    $age = $_POST["age"][$username];
    $cellno = $_POST["cellno"][$username];
    $email = $_POST["email"][$username];

    mysql_query ("UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `email` ='$email' WHERE username = '".$_SESSION['user']['username']."' ") or die(mysql_error()); 

    header("Location: myprofile.php");  
}
        $sql= "SELECT * FROM users_info WHERE username= '".$_SESSION['user']['username']."' ";
        $res= mysql_query($sql) or die (mysql_error());

        if(mysql_num_rows($res) >0) {
        echo '<form method="post">';
        while ($row=mysql_fetch_array($res)) {
            echo 'Username:'. $row['username'].'</p> ';
            echo 'Name: <input type="text" name="name['.$row["username"].']" value="'. $row['name'].'"></p> ';
            echo 'Address: <input type="text" name="address['.$row["username"].']" value="'. $row['address'].'"></p> ';
            echo 'Age: <input type="text" name="age['.$row["username"].']" value="'. $row['age'].'"></p> ';
            echo 'Phone: <input type="text" name="cellno['.$row["username"].']" value="'. $row['cellno'].'"></p> ';
            echo 'Email: <input type="text" name="email['.$row["username"].']" value="'. $row['email'].'"></p> ';
        }

        echo '<input type="submit" name="submit" value="Update">';
        echo '</form>';

    }

&GT;

我在此页面已经有session_start();

4 个答案:

答案 0 :(得分:0)

$query = "UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `email` ='$email' WHERE username = '".$_SESSION['user']['username']."'";

echo $query;

然后查看输出。或者使用而不是echo $ query:

mysql_query($query); 
echo mysql_error();

完整代码更新2:

$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'testing';

// Connect to server and select databse.
mysql_connect("$server", "$user", "$pass")or die("cannot connect");
mysql_select_db("$db")or die("cannot select DB");

if(isset($_POST["submit"]))
{
    $name = $_POST["name"][$username];
    $address = $_POST["address"][$username];
    $age = $_POST["age"][$username];
    $cellno = $_POST["cellno"][$username];
    $email = $_POST["email"][$username];

   $query = "UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `email` ='$email' WHERE username = '".$_SESSION['user']['username']."'";

    die($query);

    //header("Location: myprofile.php");  
}
        $sql= "SELECT * FROM users_info WHERE username= '".$_SESSION['user']['username']."' ";
        $res= mysql_query($sql) or die (mysql_error());

        if(mysql_num_rows($res) >0) {
        echo '<form method="post">';
        while ($row=mysql_fetch_array($res)) {
            echo 'Username:'. $row['username'].'</p> ';
            echo 'Name: <input type="text" name="name['.$row["username"].']" value="'. $row['name'].'"></p> ';
            echo 'Address: <input type="text" name="address['.$row["username"].']" value="'. $row['address'].'"></p> ';
            echo 'Age: <input type="text" name="age['.$row["username"].']" value="'. $row['age'].'"></p> ';
            echo 'Phone: <input type="text" name="cellno['.$row["username"].']" value="'. $row['cellno'].'"></p> ';
            echo 'Email: <input type="text" name="email['.$row["username"].']" value="'. $row['email'].'"></p> ';
        }

        echo '<input type="submit" name="submit" value="Update">';
        echo '</form>';

    }

更新3:

您遇到了一些数据库连接问题! 这是固定的:

$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'testing';

// Connect to server and select databse.
mysql_connect($server, $user, $pass );
mysql_select_db($db);

答案 1 :(得分:0)

尝试这样:

// Connect to server and select databse.
mysql_connect($server, $user, $pass)or die("cannot connect");
mysql_select_db($db)or die("cannot select DB");
if(isset($_POST["submit"]))
{

$name = $_POST["name[$username]"];
$address = $_POST["address[$username]"];
$age = $_POST["age[$username]"];
$cellno = $_POST["cellno[$username]"];
$email = $_POST["email[$username]"];

 mysql_query ("UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `email` ='$email' WHERE username = '".$_SESSION['user']['username']."' ") or die(mysql_error()); 

    header("Location: myprofile.php");  
}

答案 2 :(得分:0)

在读取$ _POST变量时,看起来你的$ username变量看起来不像。是插入空行吗? 尝试更改

$name = $_POST["name"][$username];

$name = $_POST["name"][$_SESSION['user']['username']];

答案 3 :(得分:0)

我已经为此尝试了另一个答案,并且它正常工作。

<?php
// First we execute our common code to connection to the database and start the session 
    require("common.php"); 

    // At the top of the page we check to see whether the user is logged in or not 
    if(empty($_SESSION['user'])) 
    { 
        // If they are not, we redirect them to the login page. 
        header("Location: login.php"); 

        // Remember that this die statement is absolutely critical.  Without it, 
        // people can view your members-only content without logging in. 
        die("Redirecting to login.php"); 
    } 

    // Everything below this point in the file is secured by the login system 

    // We can display the user's username to them by reading it from the session array.  Remember that because 
    // a username is user submitted content we must use htmlentities on it before displaying it to the user.
    // Database Variables (edit with your own server information)
        $server = 'localhost';
        $user = 'root';
        $pass = '';
        $db = 'testing';

        // Connect to server and select databse.
        mysql_connect("$server", "$user", "$pass")or die("cannot connect"); 
        mysql_select_db("$db")or die("cannot select DB");


    $result = mysql_query("SELECT * FROM users_info WHERE username = '".$_SESSION['user']['username']."'");
    $test = mysql_fetch_array($result);
    if (!$result) 
        {
        die("Error: Data not found..");
        }
                $name = $test['name'];
                $address = $test['address'];
                $age = $test['age'];
                $cellno = $test['cellno'];
                $email = $test['email'];


if(isset($_POST['save']))
{   
    $name = $_POST['name'];
    $address = $_POST['address'];
    $age = $_POST['age'];
    $cellno = $_POST['cellno'];
    $email = $_POST['email'];

    mysql_query("UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `email` ='$email' WHERE username = '".$_SESSION['user']['username']."'");

    header("Location: myprofile.php");          
}
?>

你去吧。谢谢您的帮助。我很感激。