更新到sql失败

updating to sql failed

本文关键字:失败 sql 更新      更新时间:2023-09-26

我收到一个错误,说失败,每当我检查这个代码时,我都觉得我的代码没有问题。请帮帮我,因为我只是个初学者。有表单、javascript和php。

<?php   
    $species="";    //declare variable to assign table record data
    $age= ""; 
    $sex="";
    $location="";
    $comment="";
    require('connectdb.php');
    $record=$_REQUEST['id' ]; 
    $query="SELECT * FROM usersform WHERE  id = $record "; // listing reservation record
    $result=mysql_query($query);
    $row=mysql_fetch_array($result);
    $species= $row['species'];    //retrieve  table record and assign  to variable 
    $age= $row['age']; 
    $location= $row['location']; 
    $comment= $row['comment']; 
    if (isset($_POST['submit'])) 
    {
        //get data from reservation form 
        $spec=$_POST['species'];
        $eage=$_POST['age'];
        $gender=$_POST['gender'];
        $elocation=$_POST['location'];
        $ecomment=$_POST['comment'];
        //update data to table
        $query="    UPDATE `usersform` SET 
                        `species` ='$spec',
                        `age`='$eage',
                        `location`='$elocation',
                        `comment`='$ecomment',
                    WHERE usersform= $record        
                ";
        $qresult = mysql_query($query);
        if ($qresult){
            /*echo '<script type="text/javascript">
            window.location = "http://www.example.com/";
            </script>';*/
            echo '<script type="text/javascript"> window.location="gallery.php";</script>';
            #header("location:gallery.php"); // redirect to list
        }
        else
        {
            echo "<script type='text/javascript'>alert('failed!')</script>";
        }
    }

?>

这是我的表单代码:

<h2>Edit/Update Reservation Record </h2>
<form  name="Form" method="Post"  onsubmit="return(validateForm());" onreset="cancel();">
<table cellspacing="2" cellpadding="2">
    <tr>
        <td>species: </td>
        <td><input type="text"  name="species" size="35" value="<?php echo $species; ?>"/></td>
    </tr>
    <tr>
        <td>age:</td>
        <td><input type="text" name="age" maxlength="15" size="15" value="<?php echo $age; ?>"/></td>
    </tr>
    <tr>
        <td>sex:</td>
        <td><input type="radio" name="gender" value="male" value="<?php echo $sex; ?>"/>Male &nbsp; <input type="radio" name="gender" value="female" value="<?php echo $sex; ?>"/>Female</td>
    </tr>
    <tr>
        <td>location:</td>
        <td><input type="text" name="location" size="35" value="<?php echo $location; ?>"/></td>
    </tr>
    <tr>
        <td>comment:</td>
        <td><input type="text" name="comment" size="50" value="<?php echo $comment; ?>"/></td>
    </tr>
    <tr>
        <td><input type="submit" value="Update" name="submit" /></td>
            <td><input type="reset" value="Cancel" name="reset" /></td>
    </tr>
</table>
</form>

这是我的javascript:

function validateForm() {
    if( document.Form.species.value == "" )
    {
        alert( "Please insert species." );
        document.Form.Name.focus() ;
        return false;
    }
    if( document.Form.age.value == "" ||isNaN( document.Form.age.value ) )
    {
        alert( "Please insert age." );
        document.Form.age.focus() ;
        return false;
    }
    if( document.Form.location.value == "" )
    {
        alert( "Please insert location." );
        document.Form.Name.focus() ;
        return false;
    }
    if( document.Form.comment.value == "" )
    {
        alert( "Please insert comment." );
        document.Form.Name.focus() ;
        return false;
    }
}
function cancel()
{
    window.location.href="gallery.php";
}

更新语法不正确。。这是正确的sql语法

$query="    UPDATE `usersform` SET 
                        `species` ='$spec',
                        `age`='$eage',
                        `location`='$elocation',
                        `comment`='$ecomment'
                    WHERE usersform= $record        
                ";

update查询更改为此

$query="    UPDATE `usersform` SET 
                    `species` ='$spec',
                    `age`='$eage',
                    `location`='$elocation',
                    `comment`='$ecomment'
                WHERE id= $record        
            ";
$qresult = mysql_query($query) or die(mysql_error());

注意:学习mysqli_或p.D.O,因为mysql是去极化