无法更新PHP DB

时间:2014-07-16 20:29:19

标签: php mysql

我无法弄清楚为什么我的数据没有更新到mysql。我相信这个问题介于我在下面显示的两个文件之间。但是,问题是$ sql更新中的语法错误 - 缺少几个逗号。在收到社区的反馈后,我回去用javascript和php验证了表单。

update.php。

<title>update</title>
</head>

<?php

session_start();
// Connect to server and select database.
mysql_connect("localhost", "root", "pwdpwd", "pet_shop")or die("cannot connect"); 
mysql_select_db("pet_shop")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['GroomingID'];



// Retrieve data from database 
$sql="SELECT * FROM grooming WHERE GroomingID = '$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<body>


<table width="1200" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="6"><strong>Update Porting Details</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Last Name</strong></td>
<td align="center"><strong>Address</strong></td>
<td align="center"><strong>City</strong></td>
<td align="center"><strong>State</strong></td>
<td align="center"><strong>Zip</strong></td>
<td align="center"><strong>Phone Number</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Pet Type</strong></td>
<td align="center"><strong>Breed</strong></td>
<td align="center"><strong>Pet Name</strong></td>
<td align="center"><strong>Neutered Or Spayed</strong></td>
<td align="center"><strong>Pet Birthday</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="FirstName" type="text" id="FirstName" value="<?php echo $rows['FirstName']; ?>"size= "15"/>
</td>
<td align="center">
<input name="LastName" type="text" id="LastName" value="<?php echo $rows['LastName']; ?>"size= "15"/>
</td>
<td align="center">
<input name="Address" type="text" id="Adress" value="<?php echo $rows['Address']; ?>"size= "15"/>
</td>
<td align="center">
<input name="City" type="text" id="City" value="<?php echo $rows['City']; ?>" size="15"/>
</td>
<td align="center">
<input name="State" type="text" id="State" value="<?php echo $rows['State']; ?>" size="15"/>
</td>
<td align="center">
<input name="Zip" type="text" id="Zip" value="<?php echo $rows['Zip']; ?>" size="15"/>
</td>
<td align="center">
<input name="PhoneNumber" type="text" id="PhoneNumber" value="<?php echo $rows['PhoneNumber']; ?>" size="15"/>
</td>
<td align="center">
<input name="Email" type="text" id="Email" value="<?php echo $rows['Email']; ?>" size="15"/>
</td>
<td align="center">
<input name="PetType" type="text" id="PetType" value="<?php echo $rows['PetType']; ?>" size="15"/>
</td>
<td align="center">
<input name="Breed" type="text" id="Breed" value="<?php echo $rows['Breed']; ?>" size="15"/>
</td>
<td align="center">
<input name="PetName" type="text" id="PetName" value="<?php echo $rows['PetName']; ?>" size="15"/>
</td>
<td align="center">
<input name="NeuteredOrSpayed" type="text" id="NeuteredOrSpayed" value="<?php echo $rows['NeuteredOrSpayed']; ?>" size="15"/>
</td>
<td align="center">
<input name="PetBirthday" type="text" id="PetBirthday" value="<?php echo $rows['PetBirthday']; ?>" size="15"/>
</td>
<tr>
</table>
<input name="GroomingID" type="hidden" id="GroomingID" value="<?php echo $rows['GroomingID']; ?>"/>
<input type="submit" name="Submit" value="Submit" /></td>
<td align="center">&nbsp;</td>
</td>
</form>
</tr>
</table>
</body>
</html>

最后,最后一个文件 - update_ac.php。我收到文件没有更新的错误。

<?php

    session_start();

    if ($_SESSION['username'])
        echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>logout</a>";
    else
        die("You must be logged in!");
    //Session login - all admin pages must include this to interact with DB.
    // Connect to server and select database.
    mysql_connect("localhost", "root", "pwdpwd", "pet_shop")or die("cannot connect"); 
    mysql_select_db("pet_shop")or die("cannot select DB");

    // update data in mysql database 
    $sql="UPDATE grooming SET FirstName='".$_POST['FirstName']."',
    LastName='".$_POST['LastName']."',
    Address='".$_POST['Address']."', 
    City='".$_POST['City']."', 
    State='".$_POST['State']."', 
    Zip='".$_POST['Zip']."', 
    PhoneNumber='".$_POST['PhoneNumber']."',
    Email='".$_POST['Email']."', 
    PetType='".$_POST['PetType']."', 
    Breed='".$_POST['Breed']."', 
    PetName='".$_POST['PetName']."', 
    NeuteredOrSpayed='".$_POST['NeuteredOrSpayed']."',
    PetBirthday='".$_POST['PetBirthday']."' WHERE GroomingID='".$_POST['GroomingID']."'";

    $result=mysql_query($sql)or
    die ("<br>Could not Update");


    // if successfully updated. 
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='Welcome.php'>View result</a>";
    }

    else {
    echo "ERROR";
    }

    ?>

1 个答案:

答案 0 :(得分:0)

您的PhoneNumber=...声明中Email=...UPDATE之间需要逗号。

哦,NeuteredOrSpayed=...PetBirthday=...

另请注意评论;保护您的数据库并在您的代码中实现一些调试。