php - 在 SQL 查询中使用 $object->get_variable() [PHP]

标签 php mysql sql oop

我正在使用网络表单将一些数据写入 MySQL 数据库, 当我从表单接收输入数据时,我将其存储在一个对象中;

但是当我尝试在 SQL 查询中使用此数据时,出现错误。

有人可以帮我吗?

谢谢

K

这是对象的初始化:

$nieuweInschrijving = new InvoerGegevens($voornaam, $achternaam, $functie, $bedrijf, $sector, $email);

class InvoerGegevens {

public $voornaam;
public $achternaam;
public $functie;
public $bedrijf;
public $sector;
public $email;
public $datum;
public $uniekeCode;

这是我的类(class):InvoerGegegevens

function __construct($paramVoornaam, $paramAchternaam, $paramFunctie, $paramBedrijf, $paramSector, $paramEmail)
{
        $this->voornaam = $paramVoornaam;
        $this->achternaam = $paramAchternaam;
        $this->functie = $paramFunctie;
        $this->bedrijf = $paramBedrijf;
        $this->sector = $paramSector;
        $this->email = $paramEmail;
        $this->uniekeCode = $this->createUniekeCode();
        $this->datum = date("d-m-Y H:i:s");
}
function get_voornaam()
{
    return $this->voornaam;
}
...
function get_uniekeCode()
{
    if(isset($this->uniekeCode))
        return $this->uniekeCode;
    else
        return 0;               
}


function createUniekeCode()
{
    // range is numbers (48) through capital and lower case letters (122)
    $range_start = 48;
    $range_end   = 122;
    $random_string = "";
    $random_string_length = 12;

    for ($i = 0; $i < $random_string_length; $i++) {
      $ascii_no = round( mt_rand( $range_start , $range_end ) ); // generates a number within the range
      // finds the character represented by $ascii_no and adds it to the random string
      // study **chr** function for a better understanding
      $random_string .= chr( $ascii_no );
    }

return  $random_string;
}

这是我的声明:

$echo = "INSERT INTO `tbl_inschrijving` (Voornaam, Achternaam, Functie, Bedrijf, Sector, Email, UniekeCode, Datum) VALUES('$nieuweInschrijving->get_voornaam();'....

我得到的错误是:

Notice: Undefined property: InvoerGegevens::$get_voornaam

感谢您的建议

最佳答案

我看起来像

'$nieuweInschrijving->get_voornaam();'`

被解释为

$nieuweInschrijving->get_voornaam . '();'

而不是意图:

$nieuweInschrijving->get_voornaam() . ';'

使用字符串连接,不要依赖 PHP 的解释。

<小时/>

您还需要转义查询中的输入。

关于php - 在 SQL 查询中使用 $object->get_variable() [PHP],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23296972/

相关文章:

php - curl YouTube中没有匹配项-返回null

php - 在 Zend Framework 2 中对同一 namespace 使用两个不同的目录 - 如何?

mysql - Laravel 查询构建内连接

MySQL选择具有唯一属性值的一行

mysql - 对于表a中的每条记录,返回表b中日期最接近但大于现在的记录NOW()

php - 分析和优化 PHP/MySQL 网站

php - substr_count 和一个作为针的数组

mysql - 处理带有非英文字符的 SEO 友好 URL

mysql - 如何使用父数据从父表创建子表

php - 更新错误 : Column count doesn't match value count at row 1