1

I've have a simple HTML contact form and a PHP script to submit the data to a MySQL DB.

Everything seems to be working fine - my browser is showing the data was submitted - except the data doesn't show up in the DB Table in phpMyAdmin. All I see is "MySQL returned an empty result set (i.e. zero rows)."

Here is the PHP Script:

<?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));
}
}
?>

Here is my HTML form:

<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>

Any help or insights would be appreciated. This is my first attempt at using PHP and after struggling with this for a few hours X-) I'm at a loss.

EDIT: Along with the missing backticks identified by Chris - I was missing name="submit" in the form button's HTML.

2
  • 2
    Please lookup prepared statements. You don't want to insert directly from a $_POST. It will help immensely here. Commented Oct 1, 2015 at 21:13
  • echo the $insqDbtb and see what you get. Commented Oct 1, 2015 at 21:29

1 Answer 1

1

You are missing a single-quote and a back-tick in your SQL query. It should be:

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

(ZipCode was missing the back-tick and $propertyAddress the single-quote.)

The query will probably work if you correct this.

EDIT: Try this in phpMyAdmin.

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

Do you get any errors?

Also, you probably don't need to select the database again in your query. So you can just have:

INSERT INTO `House_Leads` ( ...
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for catching this. I've updated the code, but still nothing in the DB. I'm going to look into prepared statements as suggested by @blahfunk
Can you also post your database structure? It might be failing because a column is not spelled right, or because the input is of wrong datatype. Also, did you check that all $_POST variables are being sent properly from the form to the handler file?
dbo594303259 is username and db594303259 is DB name
Oh, I see. Many times username and database name is the same, hence my comment.
I can't see any error right off the bat. Try entering the code I added above directly into the database (SQL tab in phpMyAdmin).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.