Jump to content

Recommended Posts

Hello, i've been fixing errors in my code for a while.. but i keep having a few :(( i'm afraid that i don't get some points in php as i've been spending hours to fix some errors, but then new came.

 

(i have programmed a tiny bit in jave earlier so i mistake here a lot i guess)

 

I have 4 files:

beheerderForm.htlm

beheerderController.php

beheerder.php

beheerderDA.php 

 

I'm trying to simple add an 'beheerder' by letting him enter a name and pasword in a form and then write it to the database using 3 layer architecture. (Controller and DA classes are persistence classes)

 

Current errors:

 

-fatal error: Call to a member function setObj() on a non-object in C:\wamp\www\BeheerderController.php on line 41

-Notice: Undefined variable: beheerder in C:\wamp\www\BeheerderController.php on line 41

-Undefined variable: aB in C:\wamp\www\Beheerder.php on line 22

-You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' )' at line 1

 

It could be that some code is just wrong thinking, if you notice it or have any other suggestions, i'm very pleased to learn it!

 

My code:

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script src="validateBeheerderPasw.js" type="text/javascript"></script>
<link href="validateBeheerderPasw.css" rel="stylesheet" type="text/css"/>


<title>BeheerderForm</title>
</head>
<body>
<form action="/BeheerderController.php?" id="form1" name="form1" method="POST">
  <div align="center">
    <table width="200" border="1">
      <tr>
        <td>naam</td>
        <td><label for="TFnaam"></label>
        <input type="text" name="TFnaam" id="TFnaam" /></td>
      </tr>
      <tr>
        <td>pasw</td>
        <td><label for="TFpasw1"></label>
        <input type="password" name="TFpasw1" id="TFpasw1" /></td>
      </tr>
      <tr>
        <td>herhaal pasw</td>
        <td><span id="spryconfirm1">
          <label for="confirmpasw"></label>
          <input type="password" name="confirmpasw" id="confirmpasw" />
        <span class="confirmRequiredMsg">A value is required.</span><span class="confirmInvalidMsg">The values don't match.</span></span></td>
      </tr>
      <tr>
        <td> </td>
        <td><input type="submit" name="Bvoegbeheertoe" id="Bvoegbeheertoe" value="Submit" /></td>
      </tr>
    </table>
  </div>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<script type="text/javascript">
var spryconfirm1 = new Spry.Widget.ValidationConfirm("spryconfirm1", "TFpasw1");
</script>
</body>
</html>

<?php require_once('Beheerder.php'); ?>

<?php

class BeheerderController {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""){

if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;

}
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
$beheerder = new Beheerder(GetSQLValueString($_POST['TFnaam'], "text"),GetSQLValueString($_POST['confirmpasw'], "text"));
}

$beheerder->setObj($beheerder);

?>


<?php require_once('BeheerderDA.php'); ?>

<?php

class Beheerder {

private $naam;
private $paswoord;
private $aB;

function __construct($naam, $paswoord){
  $this->naam = $naam;
  $this->paswoord = $paswoord;
   }

function setObj ($aB){
  $this->ab = $aB;
}
}
$aBeheerderDA = new BeheerderDA (); 

$aBeheerderDA->saveBeheerder($aB);

function getBeheerderNaam (){
global $naam;
return $naam;
}

function getBeheerderPaswoord(){
global $paswoord;
return $paswoord;
}

?>


<?php require_once('DBconnQualifield.php'); ?>
<?php require_once('Beheerder.php'); ?>
<?php

class BeheerderDA {

private $aB;
private $naam;
private $paswoord;

public function __construct() {
}

function saveBeheerder ($aB){
$this->aB = &$aB;
}
}

$naam = &$aB.getBeheerderNaam (); 

$paswoord = &$aB.getBeheerderPaswoord ();

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES (%s, %s)",$naam,$paswoord);

  mysql_select_db($database_DBconnQualifield, $DBconnQualifield);
  $Result1 = mysql_query($insertSQL, $DBconnQualifield) or die(mysql_error());
}

mysql_select_db($database_DBconnQualifield, $DBconnQualifield);
$query_RSvoegbeheerdertoe = "SELECT * FROM beheerder";
$RSvoegbeheerdertoe = mysql_query($query_RSvoegbeheerdertoe, $DBconnQualifield) or die(mysql_error());
$row_RSvoegbeheerdertoe = mysql_fetch_assoc($RSvoegbeheerdertoe);
$totalRows_RSvoegbeheerdertoe = mysql_num_rows($RSvoegbeheerdertoe);
?>



 

 

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/
Share on other sites

the first two may be your if statement:

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

 

beheerder is set if that's true, and beheerder isn't being set - so maybe that's not evaluating to true as it should?

 

edit: actually, looking at this i can't tell which file is which. you have the includes and then code below that, is that the file included or just more code?

 

post each page in it's own code tags, if you would, so we can see what they are :)

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314537
Share on other sites

 

  /// BeheerderForm.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script src="validateBeheerderPasw.js" type="text/javascript"></script>
<link href="validateBeheerderPasw.css" rel="stylesheet" type="text/css"/>


<title>BeheerderForm</title>
</head>
<body>
<form action="/BeheerderController.php?" id="form1" name="form1" method="POST">
  <div align="center">
    <table width="200" border="1">
      <tr>
        <td>naam</td>
        <td><label for="TFnaam"></label>
        <input type="text" name="TFnaam" id="TFnaam" /></td>
      </tr>
      <tr>
        <td>pasw</td>
        <td><label for="TFpasw1"></label>
        <input type="password" name="TFpasw1" id="TFpasw1" /></td>
      </tr>
      <tr>
        <td>herhaal pasw</td>
        <td><span id="spryconfirm1">
          <label for="confirmpasw"></label>
          <input type="password" name="confirmpasw" id="confirmpasw" />
        <span class="confirmRequiredMsg">A value is required.</span><span class="confirmInvalidMsg">The values don't match.</span></span></td>
      </tr>
      <tr>
        <td> </td>
        <td><input type="submit" name="Bvoegbeheertoe" id="Bvoegbeheertoe" value="Submit" /></td>
      </tr>
    </table>
  </div>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<script type="text/javascript">
var spryconfirm1 = new Spry.Widget.ValidationConfirm("spryconfirm1", "TFpasw1");
</script>
</body>
</html>

 

 /// BeheerderController.php

<?php require_once('Beheerder.php'); ?>

<?php

class BeheerderController {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""){

if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;

}
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
$beheerder = new Beheerder(GetSQLValueString($_POST['TFnaam'], "text"),GetSQLValueString($_POST['confirmpasw'], "text"));
}

$beheerder->setObj($beheerder);

?>

 

 

 

 /// Beheerder.php
<?php require_once('BeheerderDA.php'); ?>

<?php

class Beheerder {

private $naam;
private $paswoord;
private $aB;

function __construct($naam, $paswoord){
  $this->naam = $naam;
  $this->paswoord = $paswoord;
   }

function setObj ($aB){
  $this->ab = $aB;
}
}
$aBeheerderDA = new BeheerderDA (); 

$aBeheerderDA->saveBeheerder($aB);

function getBeheerderNaam (){
global $naam;
return $naam;
}

function getBeheerderPaswoord(){
global $paswoord;
return $paswoord;
}

?>

 

 /// BeheerderDA.php

<?php require_once('DBconnQualifield.php'); ?>
<?php require_once('Beheerder.php'); ?>
<?php

class BeheerderDA {

private $aB;
private $naam;
private $paswoord;

public function __construct() {
}

function saveBeheerder ($aB){
$this->aB = &$aB;
}
}

$naam = &$aB.getBeheerderNaam (); 

$paswoord = &$aB.getBeheerderPaswoord ();

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES (%s, %s)",$naam,$paswoord);

  mysql_select_db($database_DBconnQualifield, $DBconnQualifield);
  $Result1 = mysql_query($insertSQL, $DBconnQualifield) or die(mysql_error());
}

mysql_select_db($database_DBconnQualifield, $DBconnQualifield);
$query_RSvoegbeheerdertoe = "SELECT * FROM beheerder";
$RSvoegbeheerdertoe = mysql_query($query_RSvoegbeheerdertoe, $DBconnQualifield) or die(mysql_error());
$row_RSvoegbeheerdertoe = mysql_fetch_assoc($RSvoegbeheerdertoe);
$totalRows_RSvoegbeheerdertoe = mysql_num_rows($RSvoegbeheerdertoe);
?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314549
Share on other sites

then yes, the first two i assume are because that if statement is not validating to true. 

 

i would check the statement, maybe like this:

<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
//$beheerder = new Beheerder(GetSQLValueString($_POST['TFnaam'], "text"),GetSQLValueString($_POST['confirmpasw'], "text"));
echo 'the statement was true!!';
} else { echo 'the statement was FALSE '; }

$beheerder->setObj($beheerder);
?>

 

 

i'll update more...

 

as for the third error, i don't know. i'm not good enough in oop to help with that yet...

 

for #4, i think it's just that your values need to be enclosed:

  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES ('%s','%s')",$naam,$paswoord);

instead of

  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES (%s, %s)",$naam,$paswoord);

 

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314551
Share on other sites

the first two may be your if statement:

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

 

beheerder is set if that's true, and beheerder isn't being set - so maybe that's not evaluating to true as it should?

 

 

Well, MM_insert is filled up when they fill in the form. Maybe it's indeed incorrect to try to view the file Beheerder.php since then the form is not filled in and MM_insert isn't 'set'. Indien, when i try to browse the beheerderForm.html, the browser only throws the SQL mistake!

 

Thanks for pointing it out!

 

Any ideas on the SQL syntax ?

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314554
Share on other sites

for #4, i think it's just that your values need to be enclosed:

  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES ('%s','%s')",$naam,$paswoord);

instead of

  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES (%s, %s)",$naam,$paswoord);

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314558
Share on other sites

for #4, i think it's just that your values need to be enclosed:

  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES ('%s','%s')",$naam,$paswoord);

instead of

  $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES (%s, %s)",$naam,$paswoord);

 

You are right, thanks again! However, at first sight i don't really see why when looking at the SQL INSERT INTO statement, but i'll give it a deeper look tomorrow!

 

For moderators: Please don't close this tread, i have new errors but i'll try to fix them first myself tomorrow!

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314568
Share on other sites

Here are the files with all errors corrected. Your code is not nearly close to an OO MVC architecture.

 

BeheerderForm.html

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="validateBeheerderPasw.js" type="text/javascript"></script>
  <link href="validateBeheerderPasw.css" rel="stylesheet" type="text/css"/>
  <title>BeheerderForm</title>
</head>
<body>

<form action="BeheerderController.php" id="form1" name="form1" method="POST">
  <div align="center">
    <table width="200" border="1">
      <tr>
        <td>naam</td>
        <td><label for="TFnaam"></label>
        <input type="text" name="TFnaam" id="TFnaam" /></td>
      </tr>
      <tr>
        <td>pasw</td>
        <td><label for="TFpasw1"></label>
        <input type="password" name="TFpasw1" id="TFpasw1" /></td>
      </tr>
      <tr>
        <td>herhaal pasw</td>
        <td><span id="spryconfirm1">
          <label for="confirmpasw"></label>
          <input type="password" name="confirmpasw" id="confirmpasw" />
        <span class="confirmRequiredMsg">A value is required.</span><span class="confirmInvalidMsg">The values don't match.</span></span></td>
      </tr>
      <tr>
        <td> </td>
        <td><input type="submit" name="Bvoegbeheertoe" id="Bvoegbeheertoe" value="Submit" /></td>
      </tr>
    </table>
  </div>
  <input type="hidden" name="MM_insert" value="form1" />
</form>

<script type="text/javascript">
    var spryconfirm1 = new Spry.Widget.ValidationConfirm("spryconfirm1", "TFpasw1");
</script>

</body>
</html>

 

BeheerderController.php

<?php

require_once('Beheerder.php');

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

class BeheerderController
{/* this is doing what exactly? Sit and look pretty? */}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
    $beheerder = new Beheerder(GetSQLValueString($_POST['TFnaam'], "text"),GetSQLValueString($_POST['confirmpasw'], "text"));
    // fatal error: Call to a member function setObj() on a non-object in C:\wamp\www\BeheerderController.php on line 41
    // Notice: Undefined variable: beheerder in C:\wamp\www\BeheerderController.php on line 41
    $beheerder->setObj($beheerder);
}

 

Beheerder.php

<?php

require_once('BeheerderDA.php');

class Beheerder
{
    private $naam;
    private $paswoord;
    private $aB;

    function __construct($naam, $paswoord){
        $this->naam = $naam;
        $this->paswoord = $paswoord;
   }
    
    function setObj ($aB){
        $this->ab = $aB;
    }
}

$aBeheerderDA = new BeheerderDA (); 
$aBeheerderDA->saveBeheerder($aB);

function getBeheerderNaam ()
{
    global $naam;
    return $naam;
}

function getBeheerderPaswoord()
{
    global $paswoord;
    return $paswoord;
}

 

BeheerderDA.php

<?php

require_once('DBconnQualifield.php');
require_once('Beheerder.php');

class BeheerderDA
{
    private $aB;
    private $naam;
    private $paswoord;
    
    public function __construct() {}
    
    public function saveBeheerder ($aB){
        $this->aB = $aB;
    }
}

$naam = getBeheerderNaam (); 
$paswoord = getBeheerderPaswoord ();

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
    // You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' )' at line 1
    $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES ('%s', '%s')",$naam,$paswoord);
    mysql_select_db($database_DBconnQualifield, $DBconnQualifield);
    $Result1 = mysql_query($insertSQL, $DBconnQualifield) or die(mysql_error());
}

mysql_select_db($database_DBconnQualifield, $DBconnQualifield);
$query_RSvoegbeheerdertoe = "SELECT * FROM beheerder";
$RSvoegbeheerdertoe = mysql_query($query_RSvoegbeheerdertoe, $DBconnQualifield) or die(mysql_error());
$row_RSvoegbeheerdertoe = mysql_fetch_assoc($RSvoegbeheerdertoe);
$totalRows_RSvoegbeheerdertoe = mysql_num_rows($RSvoegbeheerdertoe);

 

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314716
Share on other sites

require 'BeheerderDAO.php';

class BeheerderController
{
    public $view;
    public $request;
    
    public function __construct() {
        $this->dao = new BeheerderDAO;
    }
    
    public function insertAction() {
        /** your code here **/
    }
    
    public function errorAction() {
        /** your error code here **/
    }
    
    public function dispatch(array $input, $view)
    {
        $this->view = $view;
        $this->request = new ArrayObject($input);
        
        if (!isset($input['action'])) {
            return $this->errorAction();
        }
    
        $methodName = strtolower($input['action']) . 'Action';
        unset($input['action']);
        
        if (!method_exists($this, $methodName)) {
            return $this->errorAction();
        }
    
        $this->$methodName();
    }
}

/**
* Is this file being called directly? ie http://domain/BeheerderController.php
*/
if (basename($_SERVER['REQUEST_FILENAME']) == basename(__FILE__))
{
    require 'MyTemplateView.php';
    
    $_REQUEST = $_GET;
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $_REQUEST = $_POST;
    }
    
    $controller = new BeheerderController;
    $controller->dispatch($_REQUEST, new MyTemplateView);
}

 

Of course you will need to abstract/move both the above if() statement and the dispatch() method if you want it to work in other cases as well. The DAO could look something like:

 

class BeheerderDAO
{
    public $conn;
    
    public function __construct() {
        require 'db-config.php';
        $this->conn = mysql_connect($db['host'], $db['user'], $db['pass']);
        if ($this->conn === false)
            throw new RuntimeException('Failed to connect to the database server.');
        if (mysql_select_db($db['name'], $this->conn) === false)
            throw new RuntimeException('Failed to select the specified database.');
    }
    
    public function save($naam, $paswoord) {
        $sql = "INSERT INTO beheerder (naam, paswoord) VALUES ('%s', '%s')";
        $frm = sprintf($sql, $naam, $paswoord);
        $res = mysql_query($sql, $this->conn);
        if ($res === false)
            throw new RuntimeException('Query failed. Beheerder has not been saved.' . "\r\n\r\n" . ' QUERY: ' . $frm . "\r\n\r\n" . ' ERROR: ' . mysql_error($this->conn));
        return true;
    }
}

 

This setup is not ideal and violates a few principles (OCP, SRP, Encapsulation, comes to mind) but it should be simple enough for you to understand and pick up.

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314725
Share on other sites

the reason these standards are important even though codes could work otherwise, is the same reason php is generally viewed with disdain as being to messy and having to low a barrier to entry. It's really easy to write out a piece of code with any understanding of syntax or functions, arrays, etc. it's more possible to hack your way through than other languages. but the honest truth is with a proper understanding it's one of the most flexible and useful codebases out there, and if you follow the new standards it's very impressive.

 

it's good for EVERYONE that uses php, for EVERYONE that uses php to use standards, because standards make things smoother and also allow for systematic improvement.

 

so don't get hard on yourself about the code not being to standard, that takes longer to learn and remember. just keep trying and you'll learn bits as you go. it really is very fun if you can stop yourself from getting stressed.  with a good enough understanding you'll actually ENJOY these debugging activities.

 

good luck :)

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314746
Share on other sites

the reason these standards are important even though codes could work otherwise, is the same reason php is generally viewed with disdain as being to messy and having to low a barrier to entry. It's really easy to write out a piece of code with any understanding of syntax or functions, arrays, etc. it's more possible to hack your way through than other languages. but the honest truth is with a proper understanding it's one of the most flexible and useful codebases out there, and if you follow the new standards it's very impressive.

 

it's good for EVERYONE that uses php, for EVERYONE that uses php to use standards, because standards make things smoother and also allow for systematic improvement.

 

so don't get hard on yourself about the code not being to standard, that takes longer to learn and remember. just keep trying and you'll learn bits as you go. it really is very fun if you can stop yourself from getting stressed.  with a good enough understanding you'll actually ENJOY these debugging activities.

 

good luck :)

 

Haha, thanks :) I'm actually just starting to learn php in order to become a better system analyst. Not because i want to become a hardcore programmer. But i guess doing it the good way is always nicer :) Thanks for the support!

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314767
Share on other sites

Hi ignace,

i'm trying to understand your code  :-[. Could you tell me what you mean with MyTemplateView.php ? what does it do?

 

if (basename($_SERVER['REQUEST_FILENAME']) == basename(__FILE__))
{
    require 'MyTemplateView.php';
    
    $_REQUEST = $_GET;
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {    /// i don't understand what MyTemplateView.php does here?
        $_REQUEST = $_POST;
    }
    
    $controller = new BeheerderController;
    $controller->dispatch($_REQUEST, new MyTemplateView);
}

 

Of course you will need to abstract/move both the above if() statement and the dispatch() method if you want it to work in other cases as well. The DAO could look something like:

 

class BeheerderDAO
{
    public $conn;
    
    public function __construct() {
        require 'db-config.php';
        $this->conn = mysql_connect($db['host'], $db['user'], $db['pass']);
        if ($this->conn === false)
            throw new RuntimeException('Failed to connect to the database server.');
        if (mysql_select_db($db['name'], $this->conn) === false)
            throw new RuntimeException('Failed to select the specified database.');
    }
    
    public function save($naam, $paswoord) {
        $sql = "INSERT INTO beheerder (naam, paswoord) VALUES ('%s', '%s')";
        $frm = sprintf($sql, $naam, $paswoord);
        $res = mysql_query($sql, $this->conn);
        if ($res === false)
            throw new RuntimeException('Query failed. Beheerder has not been saved.' . "\r\n\r\n" . ' QUERY: ' . $frm . "\r\n\r\n" . ' ERROR: ' . mysql_error($this->conn));
        return true;
    }
}

 

This setup is not ideal and violates a few principles (OCP, SRP, Encapsulation, comes to mind) but it should be simple enough for you to understand and pick up.

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314772
Share on other sites

Currently it's just a dummy but you can replace it with Smarty. You can use it in your <action-name>Action() methods.

 

public function insertAction() {
    if ($this->request->offsetExists('MM_insert')) {
        /** process logic here **/
        /** onError: $this->view->assign('error', $error); **/
        /** onSuccess: header('Location: somewhere'); exit; **/
    }
    $this->view->display('beheerder/insert.tpl');
}

Link to comment
https://forums.phpfreaks.com/topic/256412-code-help/#findComment-1314851
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.