我可以列出数据库记录,但我无法更新记录(MYSQL,XAMPP,PHP)

时间:2014-11-13 13:20:41

标签: php mysql xampp

我不知道这是关于my.ini的问题。有人可以帮帮我吗?我可以列出记录,但我无法更新记录。

list_property.php

<title>List Property</title>
</head>

<?php

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


$sql="SELECT * FROM property";
$result=mysql_query($sql);

?>
<body>


<table width="1200" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="1200" border="1" cellspacing="1" cellpadding="3">
<tr>
<td colspan="50"><strong>List the property</strong> </td>
</tr>

<tr>
<td align="center"><strong>Update_date</strong></td>
<td align="center"><strong>Usage</strong></td>
<td align="center"><strong>Region</strong></td>
<td align="center"><strong>Street</strong></td>
<td align="center"><strong>Building</strong></td>
<td align="center"><strong>Layers</strong></td>
<td align="center"><strong>Unit</strong></td>
<td align="center"><strong>Construction_area</strong></td>
<td align="center"><strong>Saleable_area</strong></td>
<td align="center"><strong>Price</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Contant_person</strong></td>
<td align="center"><strong>Contant_Num</strong></td>
<td align="center"><strong>Layout</strong></td>
<td align="center"><strong>Decoration</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['Update_date']; ?></td>
<td><?php echo $rows['Usage']; ?></td>
<td><?php echo $rows['Region']; ?></td>
<td><?php echo $rows['Street']; ?></td> 
<td><?php echo $rows['Building']; ?></td>
<td><?php echo $rows['Layers']; ?></td>
<td><?php echo $rows['Unit']; ?></td>
<td><?php echo $rows['Construction_area']; ?></td> 
<td><?php echo $rows['Saleable_area']; ?></td>
<td><?php echo $rows['Price']; ?></td>
<td><?php echo $rows['Rent']; ?></td>
<td><?php echo $rows['Contant_person']; ?></td> 
<td><?php echo $rows['Contant_Num']; ?></td>
<td><?php echo $rows['Layout']; ?></td>
<td><?php echo $rows['Decoration']; ?></td>
<td align="center"><a href="update.php?P_ID=<?php echo $rows['P_ID']; ?>">update</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>
</body>
</html>

update.php

<title>Update Property</title>
</head>

<?php
// Connect to server and select database.
mysql_connect("localhost", "root", "123456")or die("cannot connect"); 
mysql_select_db("album") or die("cannot select DB");

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



// Retrieve data from database 
$sql="SELECT * FROM album.property WHERE P_ID = '$P_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 Property 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>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<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>Usage</strong></td>
<td align="center"><strong>Region</strong></td>
<td align="center"><strong>Street</strong></td>
<td align="center"><strong>Building</strong></td>
<td align="center"><strong>Layers</strong></td>
<td align="center"><strong>Unit</strong></td>
<td align="center"><strong>Construction_area</strong></td>
<td align="center"><strong>Saleable_area</strong></td>
<td align="center"><strong>Price</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Contant_person</strong></td>
<td align="center"><strong>Contant_Num</strong></td>
<td align="center"><strong>Layout</strong></td>
<td align="center"><strong>Decoration</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="Usage" type="text" P_ID="Usage" value="<?php echo $rows['Usage']; ?>"size= "15"/>
</td>
<td align="center">
<input name="Region" type="text" P_ID="Region" value="<?php echo $rows['Region']; ?>" size="15"/>
</td>
<td align="center">
<input name="Street" type="text" P_ID="Street" value="<?php echo $rows['Street']; ?>" size="15"/>
</td>
<td align="center">
<input name="Building" type="text" P_ID="Building" value="<?php echo $rows['Building']; ?>" size="15"/>
</td>
<td align="center">
<input name="Layers" type="text" P_ID="Layers" value="<?php echo $rows['Layers']; ?>" size="15"/>
</td>
<td align="center">
<input name="Unit" type="text" P_ID="Unit" value="<?php echo $rows['Unit']; ?>" size="15"/>
</td>
<td align="center">
<input name="Construction_area" type="text" P_ID="Construction_area" value="<?php echo $rows['Construction_area']; ?>" size="15"/>
</td>
<td align="center">
<input name="Saleable_area" type="text" P_ID="Saleable_area" value="<?php echo $rows['Saleable_area']; ?>" size="15"/>
</td>
<td align="center">
<input name="Price" type="text" P_ID="Price" value="<?php echo $rows['Price']; ?>" size="15"/>
</td>
<td align="center">
<input name="Rent" type="text" P_ID="Rent" value="<?php echo $rows['Rent']; ?>" size="15"/>
</td>
<td align="center">
<input name="Contant_person" type="text" P_ID="Contant_person" value="<?php echo $rows['Contant_person']; ?>" size="15"/>
</td>
<td align="center">
<input name="Contant_Num" type="text" P_ID="Contant_Num" value="<?php echo $rows['Contant_Num']; ?>" size="15"/>
</td>
<td align="center">
<input name="Layout" type="text" P_ID="Layout" value="<?php echo $rows['Layout']; ?>" size="15"/>
</td>
<td align="center">
<input name="Decoration" type="text" P_ID="Decoration" value="<?php echo $rows['Decoration']; ?>" size="15"/>
</td>
<tr>
</table>
<input name="P_ID" type="hidden" P_ID="P_ID" value="<?php echo $rows['P_ID']; ?>"/>
<input type="submit" name="Submit" value="Submit" /></td>
<td align="center">&nbsp;</td>
</td>
</form>
</tr>
</table>
</body>
</html>

update_ac.php

<?php
// Connect to server and select database.
mysql_connect("localhost", "root", "123456")or die("cannot connect"); 
mysql_select_db("album")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE property SET Usage='".$_POST['Usage']."', Region='".$_POST['Region']."' , Street='".$_POST['Street']."' , Building='".$_POST['Building']."' , Layers='".$_POST['Layers']."' , Unit='".$_POST['Unit']."' , Construction_area='".$_POST['Construction_area']."' , Saleable_area='".$_POST['Saleable_area']."' , Price='".$_POST['Price']."' , Rent='".$_POST['Rent']."' , Contant_person='".$_POST['Contant_person']."' , Contant_Num='".$_POST['Contant_Num']."' , Layout='".$_POST['Layout']."' , Decoration='".$_POST['Decoration']."' WHERE P_ID='".$_POST['P_ID']."'";
$result=mysql_query($sql) or die ("Cannot Update Property......");


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

else {
echo "ERROR";
}

?>

按照建议更改错误处理后:查询为:

UPDATE property SET Usage='0', Region='Wah Chai' , Street='Tai Hang Road' , Building='Happy Valley' , Layers='a' , Unit='A' , Construction_area='1085 square feet' , Saleable_area='872 square feet' , Price='16800000' , Rent='48000' , Contant_person='Mr Yu' , Contant_Num='99968977' , Layout='3 bedrooms, including one suit' , Decoration='Yes' WHERE P_ID='1'

错误是

You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version 
for the right syntax to use near 
'Usage='0', Region='Wah Chai' , Street='Tai Hang Road' , Building='Happy Valley'  
 at line 1

1 个答案:

答案 0 :(得分:0)

如果您正确处理错误会有所帮助。然后你会知道错误是什么,而不仅仅是发生错误。

// update data in mysql database 
$sql="UPDATE property SET Usage='".$_POST['Usage'].
                      "', Region='".$_POST['Region'].
                      "' , Street='".$_POST['Street'].
                      "' , Building='".$_POST['Building'].
                      "' , Layers='".$_POST['Layers'].
                      "' , Unit='".$_POST['Unit'].
                      "' , Construction_area='".$_POST['Construction_area'].
                      "' , Saleable_area='".$_POST['Saleable_area'].
                      "' , Price='".$_POST['Price'].
                      "' , Rent='".$_POST['Rent'].
                      "' , Contant_person='".$_POST['Contant_person'].
                      "' , Contant_Num='".$_POST['Contant_Num'].
                      "' , Layout='".$_POST['Layout'].
                      "' , Decoration='".$_POST['Decoration'].
                      "' WHERE P_ID='".$_POST['P_ID'].
                      "'";
$result=mysql_query($sql);

if ( ! $result ) {
    echo "SQL\n $sql\nGenerated this error\n";
    echo 'Database error: ' . mysql_errno() . ' : ' . mysql_error();
}

这个mysql_ *扩展已被弃用,因此你用它编写的任何代码都会在某个时间点不再有效,因为扩展将很快从PHP的新版本中删除。