【问题标题】:Call to a member function saveSibling() on a non-object?在非对象上调用成员函数 saveSibling()?
【发布时间】:2015-08-29 22:30:37
【问题描述】:

我添加了代码以将行动态添加到表单中,当用户提交表单时,它将调用应用程序对象的函数(saveSibling),将数据保存到数据库(同级表)中,但它不起作用。当所有数据都提交后,用户将被重定向到 updateAppplication.php

$userID = $_SESSION['username'];
	$a = new application();
	$nameSib = $jobSib = $relationshipSib  = $jabatan = $age = $statusSib = "";
	
	$check = true;
	
	if ($_SERVER["REQUEST_METHOD"] == "POST")
	{ 
		//save n cont to next form
	   if(isset($_POST['saveContinue']))
	   {
			$nameSib = $_POST['nameSib'];
			$jobSib = $_POST['jobSib'];
			$relationshipSib = $_POST['relationshipSib'];
			$jabatan = $_POST['jabatan'];
			$age = $_POST['age'];
			$statusSib = $_POST['statusSib'];
			 
			foreach($nameSib as $a => $b)
			{
				if(isset($_POST['nameSib']) && isset($_POST['relationshipSib']) && isset($_POST['age']) && isset($_POST['statusSib']) || isset($_POST['jobSib']) || isset($_POST['jabatan']))
					$a -> saveSibling($userID, $nameSib[$a], $relationshipSib[$a], $age[$a], $statusSib[$a], $jobSib[$a], $jabatan[$a]);
			}
			header('Location: displayApplication.php');
			
		}
		
	}

public function saveSibling($studID, $nameSib,  $relationshipSib, $age, $statusSib, $jobSib, $jabatan)
	{
		$this -> studID = $studID;
		$this -> nameSib = $nameSib;
		$this -> jobSib = $jobSib;
		$this -> relationshipSib = $relationshipSib;
		$this -> age = $age;
		$this -> statusSib = $statusSib; 
		$this -> jabatan = $jabatan;
		
		$query = mysql_query("SELECT appID FROM application WHERE (studID = '" . mysql_real_escape_string($studID) . "')");
		while($row = mysql_fetch_array($query)) 
		{
			$appID = $row['appID'];
			$sql = "INSERT INTO `sibling`(`nameSib`, `jobSib`, `relationshipSib`, `age`, `statusSib`, `jabatan`, `appID`) 
					VALUES ('$nameSib', '$jobSib', '$relationshipSib', '$age', '$statusSib', '$jabatan', '$appID')";
			$query2 = mysql_query($sql);
			if(!$query2)
				echo mysql_error();
		}
		
		
	}

【问题讨论】:

  • 错误信息就是你的答案,如果你在谷歌搜索你的错误,你会发现很多结果。

标签: php mysql oop dynamic


【解决方案1】:

你正在覆盖你的变量:

$a = new application();

...

        foreach($nameSib as $a => $b)
                            ^^ here
        {

您需要为循环使用不同的变量名:

        foreach($nameSib as $key => $value)
        {

【讨论】:

  • 没有意识到这一点。当我更改它并提交3个数据时,数据库中只有一个。我该怎么做才能将所有 3 个都插入到数据库中?
  • @mimisya 您需要缩小问题范围并可能提出一个新问题。可以是任何东西,比如 sql 注入自己,POST 值不是数组等。
  • @jereon 看不懂,第一次用php开发系统:(
猜你喜欢
  • 2010-09-26
  • 2012-04-20
  • 2015-05-01
  • 2014-08-07
  • 2013-12-01
  • 2015-12-23
  • 2015-02-02
  • 2015-05-22
  • 2015-03-24
相关资源
最近更新 更多