我有一个PHP代码,我想将表单数据保存到datatase中。我测试,我可以写"插入表值()"直接在数据库中,它可以插入到数据库中。
但是在我的界面中,当我点击提交按钮时,我无法保存到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>
答案 0 :(得分:1)
看起来你的SQL查询可能是错误的。您需要包括每个项目的提交位置:
您的代码:
INSERT INTO Properties VALUES($ pid,$ aid,$ uid)
您需要将其更改为:
INSERT INTO属性(pidrow,aidrow,uidrow)VALUES($ pid,$ aid,$ uid)
这会使$ pid输入到pidrow,$ aid输入到aidrow等等。
答案 1 :(得分:0)
Ya,您还需要在查询中将列名称指定为:
INSERT INTO Properties(col1, col2, col3)
VALUES(val1, val2, val3)
您还必须连接到数据库
使用:
mysqli($con, "query");