仍然无法用php更新mysql数据库

时间:2013-02-17 17:20:37

标签: php mysql

我正在寻找纠正错误的4小时,因为我只是mysql和php的初学者。我只是无法更新我的记录。这是我的代码我希望这不要求太多我只是想弄清楚我的代码错误和如何解决它。提前谢谢。

update.php

    <?php
    $id=(int)$_POST['update_id'];
    $connection = mysql_connect("localhost", "root", "");
    $dbname = "student";
    if($connection===FALSE)
        echo "<p> Connection failed. ". mysql_error() ."</p>";
    else {
        if(mysql_select_db($dbname)===FALSE)
            echo "<p>Could not select database.</p>";
    }
    $query = "SELECT * FROM studenttable WHERE studID='$id'";
    $result = @mysql_query($query);
    if ($result==FALSE){
        echo "<p>Unable to execute query.</p>". mysql_error();
    }
    else 
        {

            while (($row = mysql_fetch_assoc($result))!==FALSE){
                $id = $row['studID'];
                $firstname = $row['studFname'];
                $lastname =  $row['studLname'];
                $address =  $row['studAddr'];
                $email = $row['studEmail'];
                $contact = $row['studContact'];
                $month = $row['studBDmonth'];
                $day = $row['studBDday'];
                $year = $row['studBDyear'];
                $about = $row['studNotes'];

                }
        }
    mysql_close($connection);
    $connection = FALSE;

    ?>


<form action="update_process.php" method="post" id="<?php echo '$id' ?>">
<ul>

<li id="li_1" >
    <label class="description" for="element_1">Name </label>
<span>
   <input id="element_1_1" name= "u_firstname" class="element text" maxlength="255"     size="8" value="<?php echo "$firstname" ?>"/>
                <label>First</label>
                </span>
                <span>
                <input id="element_1_2" name= "u_lastname" class="element text" maxlength="255" size="14" value="<?php echo "$lastname" ?>"/>
                <label>Last</label>
                </span></li>


                <li id="li_3" >
                <label class="description" for="element_3">Address </label>
                <div>
                    <input id="element_3" name="u_address" class="element text large" type="text" maxlength="255" value="<?php echo "$address" ?>" /> 
                </div> 
                </li>       <li id="li_2" >
                <label class="description" for="element_2">Email </label>
                <div>
                    <input id="element_2" name="u_email" class="element text medium" type="text" maxlength="255" value="<?php echo "$email" ?>" /> 
                </div> 
                </li>       <li id="li_6" >
                <label class="description" for="element_6">Contact Number </label>
                <div>
                    <input id="element_6" name="u_contactnumber" class="element text medium" type="text" maxlength="255" value="<?php echo "$contact" ?>" /> 
                </div> 
                </li>       <li id="li_4" >
                <label class="description" for="element_4">Date </label>
                <span>
                    <input id="element_4_1" name="u_month" class="element text" size="2" maxlength="2" value="<?php echo "$month" ?>" type="text"> /
                    <label for="element_4_1">MM</label>
                </span>
                <span>
                    <input id="element_4_2" name="u_day" class="element text" size="2" maxlength="2" value="<?php echo "$day" ?>" type="text"> /
                    <label for="element_4_2">DD</label>
                </span>
                <span>
                    <input id="element_4_3" name="u_year" class="element text" size="4" maxlength="4" value="<?php echo "$year" ?>"  type="text">
                    <label for="element_4_3">YYYY</label>
                </span>

                </li>       <li id="li_5" >
                <label class="description" for="element_5">About self </label>
                <div>
                    <textarea id="element_5" name="u_notes" class="element textarea medium" ><?php echo "$about" ?></textarea> 
                </div> 
                </li>

                            <li class="buttons">
                        <input type="hidden" name="form_id" value="575228" />

                </li>

                </ul>

                <input type="Submit" value="Update">
                </form>

                <a href="javascript:history.back()">Go back</a>

update_processor.php

    <?php
$u_id=(int)$_POST['update_id'];
$ufname = mysql_real_escape_string($_POST['u_firstname']);
$ulname = mysql_real_escape_string($_POST['u_lastname']);
$uaddress = mysql_real_escape_string($_POST['ud_address']);
$uemail = mysql_real_escape_string($_POST['u_email']);
$ucontact = mysql_real_escape_string($_POST['u_contactnumber']);
$umonth = mysql_real_escape_string($_POST['u_month']);
$uday = mysql_real_escape_string($_POST['u_day']);
$uyear =mysql_real_escape_string($_POST['u_year']); 
$unote = mysql_real_escape_string($_POST['u_notes']);


$connection = mysql_connect("localhost", "root", "");
$dbname = "student";

if($connection===FALSE)
    echo "<p> Connection failed. ". mysql_error() ."</p>";
else {
    if(mysql_select_db( $dbname)===FALSE)
        echo "<p>Could not select database.</p>";
    else echo "<p>Successfully selected the database '$dbname'!";
}
$query = "UPDATE studentTable SET studFname='$ufname', 
studLname='$ulname', studAddr='$uaddress', studEmail='$uemail',
studContact='$ucontact', studBDmonth='$umonth', studBDday='$uday', 
studBDuyear='$year', studNotes='$unote' WHERE studID='$u_id' ";


$result = @mysql_query($query);
if ($result===FALSE){
    echo "<p>Unable to insert data.</p>";
} 
else {
    echo "<p> Succesfully added data! </p>";
    echo "<p>" . mysql_info($connection) . "</p>";
}
mysql_close($connection);
$connection = FALSE;
?>

这是我向你们寻求帮助的最后手段。希望得到答复。 :)

0 个答案:

没有答案