我有一个php代码,我想将表单数据保存到数据库中。我测试过,我可以在数据库中直接写入“插入表值()”,它可以插入到数据库中。无法将表单数据保存到db当我点击提交按钮
但在我的用户界面,当我点击提交按钮,我不能保存到分贝?
<?php
include('connectWorkbench.php');
$sql=mysql_query("SELECT * FROM Properties");
$epr = '';
$msg = '';
if(isset($_GET['epr']))
$epr=$_GET['epr'];
//++++++++++++++++++++++++++++++save record++++++++++++++++++
if($epr=='save')
{
$pid=$_POST['pid'];
$aid=$_POST['aid'];
$uid=$_POST['uid'];
$code=$_POST['code'];
$name=$_POST['name'];
$description=$_POST['description'];
$parking=$_POST['parking'];
$sfeet=$_POST['sfeet'];
$price=$_POST['price'];
$distance=$_POST['distance'];
$owner=$_POST['owner'];
$a_sql=mysql_query("INSERT INTO Properties VALUES($pid,$aid,$uid,$code,'$name','$description','$parking',$sfeet,'$price','$distance','$owner')");
if($a_sql)
header("location:index.php");
else
{
$msg='Error:'.mysql_error();
echo '$msg';
}
}
?>
<!--$query = mysql_query("SELECT * FROM Contact");
while($row = mysql_fetch_array($query))
{
$id = $row['contact_ID'];
$fname = $row['first_name'];
$midname = $row['middle_name'];
$lname = $row['last_name'];
echo '<br />' .$id. ':' . $fname . '<br />';
}
$query2 = mysql_query("SELECT * FROM Properties");
while($row = mysql_fetch_array($query2))
{
$id = $row['property_ID'];
$addressID = $row['property_address_ID'];
$ownerID = $row['owner_user_ID'];
$propertyCode = $row['property_type_code'];
echo '<br />' .$id. ':' . $addressID . ':' . $ownerID .':'. $propertyCode .'<br />';
}
<IMG SRC="">
-->
<html>
<head>
</head>
<body>
<h2 align="center">Contact Us</h2>
<table align="center" border="1" cellspacing="0" cellpadding="0" width="700">
<thead>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Message</th>
</thead>
</table>
<h2 align="center">Edit Property</h2>
<form method="POST" action='index.php?epr=save'>
<table align="center" width="700">
<tr>
<td>property id</td>
<td><input type='text' name='pid'></td>
</tr>
<tr>
<td>Property address ID</td>
<td><input type='text' name='aid'/></td>
</tr>
<tr>
<td>Owner user ID</td>
<td><input type='text' name='uid'/></td>
</tr>
<tr>
<td>Property type code</td>
<td><input type='text' name='code'/></td>
</tr>
<tr>
<td>Property name</td>
<td><input type='text' name='name'/></td>
</tr>
<tr>
<td>Property description</td>
<td><input type='text' name='description'/></td>
</tr>
<tr>
<td>Parking</td>
<td><input type='text' name='parking'/></td>
</tr>
<tr>
<td>Square feet</td>
<td><input type='text' name='sfeet'/></td>
</tr>
<tr>
<td>Rent price</td>
<td><input type='text' name='price'/></td>
</tr>
<tr>
<td>Distance from campus</td>
<td><input type='text' name='distance'/></td>
</tr>
<tr>
<td>Property owner</td>
<td><input type='text' name='owner'/></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='submitbutton' value="Submit"></td>
</tr>
</table>
</form>
<!--+++++++++++++++++++++++++Show Data++++++++++++++++++++-->
<h2 align="center">Property List</h2>
<table align="center" border="1" cellspacing="0" cellpadding="0" width="1200">
<thead>
<th>Property ID</th>
<th>Property address ID</th>
<th>Owner user ID</th>
<th>Property type code</th>
<th>Property name</th>
<th>Property description</th>
<th>Parking</th>
<th>Square feet</th>
<th>Rent price</th>
<th>Distance from campus</th>
<th>Property owner</th>
<th>Action</th>
</thead>
<?php
$i=1;
while ($row=mysql_fetch_array($sql))
{
echo"<tr>
<td>".$i."</td>
<td>".$row['property_address_ID']."</td>
<td>".$row['owner_user_ID']."</td>
<td>".$row['property_type_code']."</td>
<td>".$row['property_name']."</td>
<td>".$row['property_description']."</td>
<td>".$row['parking']."</td>
<td>".$row['square_feet']."</td>
<td>".$row['rent_price']."</td>
<td>".$row['distanceFromCampus']."</td>
<td>".$row['property_owner']."</td>
<td align='center'>
<a href='#'>Delete</a> |
<a href='#'>Edit</a> |
<a href='#'>New</a>
</td>
<tr>";
$i++;
}
?>
</table>
</body>
</html>
重新编写新代码,** _请_不要使用'mysql_ *'函数**。它们已经老化并且破损,在PHP 5.5中已经被弃用了(这已经很旧了,它甚至不再接收安全更新),并且在PHP 7中完全删除。使用['PDO'](https://secure.php.net/manual /en/book.pdo.php)或['mysqli_ *'](https://secure.php.net/manual/en/book.mysqli.php)替换为_prepared statements_和_parameter binding_。有关详细信息,请参阅http://stackoverflow.com/q/12859942/354577。 – Chris
谢谢。我稍后会改变它。你能解决我目前的代码/问题吗? – AngularFan
也许吧。你能更清楚地问你的问题吗?究竟是什么问题? – Chris