【发布时间】:2016-01-03 14:27:15
【问题描述】:
我发现了类似的问题,但无法将它们与我的示例联系起来。我对 PHP 很陌生,完全是自学的。
目前我有一个输入新客户的表格。在该表单中,我希望用户能够选择现有的数据库项目(业务)并将该 BusinessID 插入到 CUSTOMER 表中。我的问题是我可以获取 BusinessID,但是我无法使用其他字段输入发布相同的 ID。代码如下
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>New Contact</title>
<!--Declare CSS and JavaScript-->
<link rel="stylesheet" type="text/css" href="RealtyCRM_Style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.resmenu.min.js"></script>
</head>
<body>
<script>
$(document).ready(function () {
$('.toresponsive').ReSmenu();
});
</script>
<!--Begin Header Code-->
<!--Begin Header Code-->
<div class="BodyHeader">
<div>
<a href='index.php'><img src='RealEstate(1).jpg' alt='LeasingLog_Logo' class="LeasingLog_Logo"></a>
</div>
</div>
<!--Begin Menu Code-->
<div class="menu_container" style="position:relative; z-index:11;">
<ul class="toresponsive">
<li><a href="logIn.php">Log In</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="news.php">News</a></li>
<li class="current-menu-item"><a href="dashboard.php">Dashboard</a>
<ul>
<li><a href="dataEntry.php">Add New Data</a></li>
<li><a href="updatedata.php">Update Data</a></li>
<li><a href="search.php">Search</a></li>
<li><a href="reports.php">Report</a></li>
<li><a href="adminPage.php">Admin Page</a></li>
<li><a href="logInteraction.php">Log Interaction</a></li>
</ul>
</li>
</ul>
</div>
<br>
<!--Begin Dashboard Buttons Code-->
<div class="DashboardButtonsTop">
<a href="retailerdataentry.php"><h1 class="centeredDashBoardButtonInactive">New Retailer</h1></a>
<a href="contactdataentry.php"><h1 class="centeredDashBoardButton">New Contact</h1></a>
<a href="propertydataentry.php"><h1 class="centeredDashBoardButtonInactive">New Property</h1></a>
</div>
<hr style="width:700px; height:5px;">
<br>
<br>
<!--END Dashboard Buttons Code-->
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'leasingl_dbwrite';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$contactFirstName = addslashes ($_POST['contactFirstName']);
$contactLastName = addslashes ($_POST['contactLastName']);
}
else
{
$contactFirstName = $_POST['contactFirstName'];
$contactLastName = $_POST['contactLastName'];
}
$contactPhoneNumber = $_POST['contactPhoneNumber'];
$contactEmail = $_POST['contactEmail'];
$Business = $_POST['BusinessID'];
$sql = "INSERT INTO Contact ". "(ContactFName,ContactLName, ContactMobilePhone, contactEmail, BusinessID, CreatedDate) ". "VALUES('$contactFirstName','$contactLastName','$contactPhoneNumber', '$contactEmail', '$Business', NOW())";
mysql_select_db('applicationDatabase');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "<div style='text-align:center;'>Entered data successfully\n</div>";
echo "<br><div><a href='contactdataentry.php' class='redirectButton'>Add More Contacts</a>\n</div>";
echo "<br><div><a href='dashboard.php' class='redirectButton'>Return to Dashboard</a></div>";
mysql_close($conn);
}
else
{
?>
<div class="Form_container">
<form method="post" action="<?php $_PHP_SELF ?>">
Contact First Name<br>
<input class="largeInput" type="text" name="contactFirstName" ID="contactFirstName"><br>
Contact Last Name<br>
<input class="largeInput" type="text" name="contactLastName" ID="contactLastName"><br>
Contact Phone Number<br>
<input class="largeInput" type="text" name="contactPhoneNumber" placeholder="### - ### - ####" ID="contactPhoneNumber"><br>
Contact Email<br>
<input class="largeInput" type="text" name="contactEmail"><br>
Business<br>
<?php
$servername = "localhost";
$username = "leasingl_dbread";
$password = "password";
$dbname = "applicationDatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT RetailerID, RetailerName FROM Retailer ORDER BY RetailerName DESC";
$result = $conn->query($sql);
?>
<select style='text-align:center;' class='largeInput' name='Business' ID='Business'>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='". $row["RetailerID"]. "'>" . $row["RetailerName"]. " - " . $row["RetailerID"]. "</option><br><br>";
}
} else {
echo "0 results";
}
?>
</select><br><br>
<?php
$conn->close();
?>
<input name="add" type="submit" id="add" value="Add Contact" class="button">
</form>
<br>
<hr style="width:400px; height:10px;">
</div>
<?php
}
?>
</body>
</html>'
所以能够从我的下拉列表中插入值是主要问题。此外,我确信我发布的内容中有不必要/不正确的代码,我一直在拼凑示例。
感谢您的所有帮助,如果我能正常工作,我的应用程序的基本版本就可以正常运行
编辑
我已成功预填充我的下拉列表,然后用户从该列表中进行选择。我想通过我的 INSERT 语句传递该选择。我担心我建立的两个不同的 CONNECTIONS 是我的 INSERT 无法识别 $Business 的部分原因
【问题讨论】:
-
很高兴知道,谢谢,我会进一步阅读。而且我没有 GET,但最后的 SELECT 语句会根据需要填充下拉框。会不会是这个问题?
-
哪个下拉菜单?零售商?