I'm not sure which method is best to use when giving READABLE feedback to end user. I've read some forums, but not really gotten any wiser (or I have not understood it)
I would like to give feedback when insertion / update fails, when it's a success and when giving custom feedback (like checking if an item alread exists).
On INSERT, UPDATE, DELETE, DROP etc., the query returns either TRUE or FALSE. Therefor my result property $this->query_result should always be either true or false.
My issues:
- Dynamically display feedback to user after form submit (submits to same page)
- $this->query_result is true if it returns a string
I have added code to see what I'm doing (doing wrong)
These are the functions I use for connecting / querying the DB:
public function connect()
{
if (!($this->conn = mysql_connect($this->host, $this->username, $this->pwd))) {
die("Error connecting to DB by user = " . $this->username);
}
$this->db = mysql_select_db($this->dbname,$this->conn)
or die("Unable to connect to database " . $this->dbname);
}
private function query($sql)
{
$this->query_result = mysql_query($sql, $this->conn)or die("Unable to query local database <b>". mysql_error()."</b><br>$sql");
if (!$this->query_result){
die("database query failed.");
} else {
return $this->query_result;
}
}
Here is my problem: I'm giving feedback on the Data Access Layer (DAL), see e.g. this:
public function addNewPerson($formData)
{
$sql = "INSERT INTO my_table(`name`, `email`, `www`)";
$sql .= " VALUES('".
$formData['name']."','".
$formData['email']."','".
$formData['www']."','");
$this->query($sql);
return $this->query_result;
}
By returning a text string, the return result will always be true. From what I read, I should probably have a function which handles errors / feedback.
This is what I'm currently doing with feedback in my template:
if (isset($_POST['form_submit']))
{
if (isset($_POST['person_submit'])) {
$formData = $sl->getFormData();
$result = $myDB->addNewPerson($formData);
if ($result == true)
{
echo '<script type="text/javascript" language="JavaScript">
jQuery("#contentArea .messageWindow1").show(500);
jQuery("#contentArea :input").click(function(){ jQuery("#contentArea .messageWindow1").hide(500); });
</script>';
} else {
echo '<script type="text/javascript" language="JavaScript">
jQuery("#contentArea .messageWindow2").show(500);
jQuery("#contentArea :input").click(function(){ jQuery("#contentArea .messageWindow2").hide(500); });
</script>';
}
}
}
<div id="contentArea">
<div class="messageWindow1"> <span class="msg"><?php echo $labelResult ?></span></div>
<div class="messageWindow2"> <span class="msg"><?php echo $labelResult ?></span></div>
</div>