2015-10-01 80 views
1

我已经有一个简单的HTML联系表单和一个PHP脚本来提交数据到MySQL数据库。HTML表单&PHP脚本 - 没有提交数据到MySQL数据库表

一切似乎工作正常 - 我的浏览器显示the data was submitted - 除了数据不显示在phpMyAdmin的数据库表中。我看到的是"MySQL returned an empty result set (i.e. zero rows)."

这里是PHP脚本:

<?php 
$link = mysqli_connect("db.example.com","dbo","*********") or die("failed to connect to server !!"); 
mysqli_select_db($link,"db"); 
if(isset($_REQUEST['submit'])) 
{ 
$errorMessage = ""; 
$PropertyAddress=$_POST['PropertyAddress']; 
$City=$_POST['City']; 
$State=$_POST['State']; 
$ZipCode=$_POST['ZipCode']; 
$AskingPrice=$_POST['AskingPrice']; 
$Name=$_POST['Name']; 
$Phone=$_POST['Phone']; 
$Email=$_POST['Email']; 


// Validation will be added here 

if ($errorMessage != "") { 
echo "<p class='message'>" .$errorMessage. "</p>" ; 
} 
else{ 

//Inserting record in table using INSERT query 

$insqDbtb="INSERT INTO `db`.`House_Leads` 
(`PropertyAddress`, `City`, `State`, `ZipCode, `AskingPrice`, `Name`, 
`Phone`, `Email`) VALUES ('$PropertyAddress, '$City', 
'$State', '$ZipCode', '$AskingPrice', '$Name', '$Phone', '$Email')"; 
mysqli_query($link,$insqDbtb) or die(mysqli_error($link)); 
} 
} 
?> 

这里是我的HTML表单:

<form role="form" action="houseleads.php" method="POST" class="contact clearfix"> 
 
    <p class="text _text text-4">Property Information</p> 
 
    <input class="_input _input-1" name="PropertyAddress" placeholder="Property Street Address" type="text"> 
 
    <div class="container container-2 clearfix"> 
 
    <input id="city" class="_input" name="City" placeholder="City" type="text"> 
 
    <input id="state" class="_input _input-3" name="State" placeholder="State" type="text"> 
 
    </div> 
 
    <div class="container container-3 clearfix"> 
 
    <input id="zipcode" class="_input" name="ZipCode" placeholder="Zip Code" type="text" maxlength="5"> 
 
    <input id="price" class="_input _input-5" name="AskingPrice" placeholder="Your Asking Price" type="number" step="5000"> 
 
    </div> 
 
    <p class="text _text text-5">Contact Information</p> 
 
    <div class="container container-4 clearfix"> 
 
    <input id="name" class="firstname" name="Name" placeholder="Your Name" type="text"> 
 
    <input id="phone" class="_input _input-6" name="Phone" placeholder="Phone Number" type="tel" maxlength="10"> 
 
    </div> 
 
    <input id="email" class="_input _input-7" name="Email" placeholder="Email Address" type="email"> 
 
    <button id="submit" class="_button" type="submit">Get an Offer</button> 
 
</form>

任何帮助或洞察力,将不胜感激。这是我第一次尝试使用PHP,经过几个小时的努力之后,X)我不知所措。

编辑:随着克里斯认定的缺少反引号 - 我错过了表单按钮的HTML中的name="submit"

+2

请查阅准备好的语句。你不想直接从$ _POST插入。这将非常有帮助。 –

+0

好的,谢谢。会做。 –

+0

回显'$ insqDbtb'并看看你得到了什么。 –

回答

1

您在SQL查询中缺少单引号和反标号。它应该是:

$insqDbtb="INSERT INTO `db594303259`.`House_Leads` 
(`PropertyAddress`, `City`, `State`, `ZipCode`, `AskingPrice`, `Name`, 
`Phone`, `Email`) VALUES ('$PropertyAddress', '$City', 
'$State', '$ZipCode', '$AskingPrice', '$Name', '$Phone', '$Email')"; 

ZipCode失踪后剔和$propertyAddress单引号)。

,如果你解决这个查询可能会工作。

编辑:试试这个在phpMyAdmin。

INSERT INTO `db594303259`.`House_Leads` 
(`PropertyAddress`, `City`, `State`, `ZipCode`, `AskingPrice`, `Name`, 
`Phone`, `Email`) VALUES ('test1', 'test2', 
'test3', 'test4', 'test5', 'test6', 'test7', 'test8') 

你有什么错误吗?

另外,您可能不需要在查询中再次选择数据库。所以你可以只有:

INSERT INTO `House_Leads` (... 
+0

感谢您的支持。我已经更新了代码,但数据库中仍然没有任何内容。我将按照@blahfunk建议的方式查看准备好的语句 –

+0

您还可以发布数据库结构吗?它可能失败,因为列拼写不正确,或者因为输入的数据类型错误。另外,你是否检查过所有'$ _POST'变量是否正确地从窗体发送到处理程序文件? – Chris

+0

检查答案中的修改。 – Chris