dimanche 19 avril 2015

Having an issue inputting values into DB with PDO

So this is the PHP code that processes the form info and it supposed to send data into the DB. It is successful every time and shoots me a success message but when I look in database I see a new row but no actual data from the form.


REMINDER: the db connection is working, except it's sending blank values to fill up the table as opposed to the form data.


Here is the HTML form to be handled by the PHP code in dealer.php:



<form action="dealer.php" method="POST">
<div class="form-group">
<label for="company">Company Name</label>
<input type="text" class="form-control" id="company" placeholder="Company Name">
</div>
<div class="form-group">
<label for="location">Location</label>
<input type="text" class="form-control" id="location" placeholder="Location">
</div>
<div class="form-group">
<label for="founded">Founded</label>
<input type="text" class="form-control" id="founded" placeholder="Founded">
</div>
<div class="form-group">
<label for="employees"># of Employees</label>
<input type="text" class="form-control" id="employees" placeholder="# of employees">
</div>
<div class="form-group">
<label for="employees"># of Employees</label>
<input type="text class="form-control" id="sales" placeholder="2014 sales">
</div>
<div class="radio">
<label><p>Is the company traded publicly?</p>
<input type="radio" name="optionsRadios" id="Yes" value="Yes">
Yes
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="No" value="No">
No
</label>
</div>
<div class="form-group">
<label class="sr-only" for="gross_2014">Gross Revenue</label>
<div class="input-group">
<div class="input-group-addon">$</div>
<input type="text" class="form-control" id="gross_2014" placeholder="Gross Revenue">
<div class="input-group-addon">.00</div>
</div>
</div>
<div class="form-group">
<label class="sr-only" for="net_2014">Net Revenue</label>
<div class="input-group">
<div class="input-group-addon">$</div>
<input type="text" class="form-control" id="net_2014" placeholder="Net Revenue">
<div class="input-group-addon">.00</div>
</div>
</div>
<div class="form-group">
<label class="sr-only" for="growth_2014">Growth(%)</label>
<div class="input-group">
<div class="input-group-addon">%</div>
<input type="text" class="form-control" id="growth_2014" placeho lder="Growth %">
<div class="input-group-addon">.00</div>
</div>
</div>
<textarea class="form-control" rows="3"></textarea>
<div class="form-group">
<p class="help-block" id="customer">Customer profile info...</p>
</div>
<textarea class="form-control" rows="3"></textarea>
<div class="form-group">
<p class="help-block" id="products">Info about product offerings.</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>


And here are the contents of dealer.php that process the HTML form above:



<?php
$servername = "XX.XXX.XXX.XX";
$username = "smartkrawl";
$password = "Bondurant15!";
$dbname = "smartkrawl";

class companyInfo{
public $name;
public $location;
public $founded;
public $employees;
public $sales;
public $gross_2014;
public $net_2014;
public $growth_2014;

public function __construct($name,$location,$founded,$employees,$sales,$gross_2014, $net_2014, $growth_2014) {

$this->name = $name;
$this->location = $location;
$this->founded = $founded;
$this->employees = $employees;
$this->sales = $sales;
$this->gross_2014 = $gross_2014;
$this->net_2014 = $net_2014;
$this->growth_2014 = $growth_2014;

}
}


try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO companyInfo (name, location, founded, employees, sales, gross_2014, net_2014, growth_2014)
VALUES ('$name', '$location', '$founded', '$employees', '$sales', '$gross_2014', '$net_2014', '$growth_2014')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;


?>

Aucun commentaire:

Enregistrer un commentaire