iT邦幫忙

0

找不出bug哪裡有錯php登入

Tzu 2020-12-24 00:39:141473 瀏覽

各位大大晚安
事情是這樣的~
我在做一個php的登入介面
可以怎麼找都找不出錯誤在哪QQ
求指點/images/emoticon/emoticon37.gif
https://ithelp.ithome.com.tw/upload/images/20201224/20119035Xu3aTeUDJy.png

下面是我的整個製作過程....
怎麼看都看不出哪裡錯
/images/emoticon/emoticon19.gif

從http://localhost:8012/phpmyadmin/
新增資料庫

建立資料表

做連線用的conDB.php

<?php
	class conDB
	{
		static $conn=NULL;
		
		function getConnection()
		{
			$serverName ="localhost:8012";
			$databaseName="account";
			$dbuser="root";
			$dbpassword="";
			
			if(!isset($conn))
			{
				try
				{
					$conn=new PDO("mysql:host=serverName; dbname=$databaseName",$dbuser,$dbpassword);
					$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
					$conn->exec("set names utf8");
					
					
				}
				
				catch(Exception $e)
				{
					echo $e->getMessage();
					error_log($e->getMessage(),0);
				}
			}	
			return $conn;
		}
		
	}
?>


註冊畫面:開一個html檔

從http://localhost:8012/register.html

<html>
<head>
<meta charset="UTF-8">
<style>
body
{
font-family:微軟正黑體;
}
</style>
</head>
<body>
<table style="text-align:center;padding:5;border:1px solid black">
<tr>
<td style="min-width:100px">帳號</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td style="min-width:100px">密碼</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td style="min-width:100px">姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td style="min-width:100px">性別</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td style="min-width:100px">系碼</td>
<td><input type="text" name="coursecode"></td>
</tr>
<tr>
<td><input type="reset" value="重設"></td>
<td><input type="submit" value="送出"></td>
</tr>


</table>
</body>
</html>



加入連結跳轉的from

<html>
<head>
<meta charset="UTF-8">
<style>
body
{
font-family:微軟正黑體;
}
</style>
</head>
<body>
<form method="post" action="register.php">
<table style="text-align:center;padding:5;border:1px solid black">
<tr>
<td style="min-width:100px">帳號</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td style="min-width:100px">密碼</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td style="min-width:100px">姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td style="min-width:100px">性別</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td style="min-width:100px">系碼</td>
<td><input type="text" name="coursecode"></td>
</tr>
<tr>
<td><input type="reset" value="重設"></td>
<td><input type="submit" value="送出"></td>
</tr>

</form>
</table>
</body>
</html>


寫跳轉過去的register.php
判斷帳號是否存在:存在0不存在1

/images/emoticon/emoticon06.gif
https://ithelp.ithome.com.tw/upload/images/20201224/20119035NZJnQrjLJe.png

現在是按送出就不行

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
海綿寶寶
iT邦大神 1 級 ‧ 2020-12-24 07:43:32
最佳解答

第一張圖裡就有寫了
出錯的是 conDB.php 第 30 列
$conn 未定義

由於 conDB.php 沒有成功回傳 $conn
以致 register.php 第 9 列無法呼叫 prepare function
在 ($conn=)null 值上呼叫 prepare function

另外
你的 Web(HTTP) 已經是 localhost:8012
你的 DB(MySQL) 不該是 localhost:8012,應該是另一個 port

看更多先前的回應...收起先前的回應...
Tzu iT邦新手 1 級 ‧ 2020-12-24 07:59:15 檢舉

大大早安

在 ($conn=)null 值上呼叫 prepare function

這句沒有很懂?
所以code要改?
你的 DB(MySQL) 不該是 localhost:8012
所以是你的 DB(MySQL) 是 localhos還是我再try

謝謝大大?

柯柯 iT邦新手 3 級 ‧ 2020-12-24 11:09:48 檢舉

port 8012 已經給HTTP用了
所以DB的port 不會是8012而是其他的
伺服器預設 DB給的port 應該是 3306

Tzu iT邦新手 1 級 ‧ 2020-12-24 11:29:35 檢舉

上班不能
Try好痛苦

一直在思考出錯的是 conDB.php 第 30 列
$conn 未定義

由於 conDB.php 沒有成功回傳 $conn
以致 register.php 第 9 列無法呼叫 prepare function
在 ($conn=)null 值上呼叫 prepare function
的意思

Tzu iT邦新手 1 級 ‧ 2020-12-24 12:43:26 檢舉
Tzu iT邦新手 1 級 ‧ 2020-12-24 13:18:44 檢舉

剛剛還想到我可以裡面先建一筆資料讓它不要空

柯柯 iT邦新手 3 級 ‧ 2020-12-24 15:22:23 檢舉

海綿大的意思 應該是
$conn=null上 去呼叫prepare

另外 $conn未定義的問題 應該是你連接資料庫錯誤
$serverName ="localhost";
如果你沒有去改 DB PORT 改成這樣跑看看

回家先把你 conDB.php 裡的 getConnection 裡的
$serverName ="localhost:8012";
改成
$serverName ="localhost:3307";
試試看

剛剛還想到我可以裡面先建一筆資料讓它不要空

現在是連資料庫都還連不上
你去裡面建一筆資料馬洗沒有用...

Tzu iT邦新手 1 級 ‧ 2020-12-24 18:53:08 檢舉

$serverName ="localhost:3307";
還是不行
https://ithelp.ithome.com.tw/upload/images/20201224/201190359NGXVwSCW3.png

https://ithelp.ithome.com.tw/upload/images/20201224/20119035DrMWqL764F.png

https://ithelp.ithome.com.tw/upload/images/20201224/20119035uThaduUvBF.png

Tzu iT邦新手 1 級 ‧ 2020-12-24 18:54:00 檢舉

https://ithelp.ithome.com.tw/upload/images/20201224/20119035tlyf6Mg9fX.png

Tzu iT邦新手 1 級 ‧ 2020-12-24 19:03:38 檢舉

https://ithelp.ithome.com.tw/upload/images/20201224/20119035zJkDEuVy9c.png
https://ithelp.ithome.com.tw/upload/images/20201224/20119035kNCTccubqk.png

所有的建議都做了還是不行QQ

port 用 3307
接著改 conDB.php

<?php
	class conDB
	{
		public $conn;
		
		function getConnection()
		{
			$serverName ="localhost:8012";
			$databaseName="account";
			$dbuser="root";
			$dbpassword="";
			
			if(!isset(this->$conn))
			{
				try
				{
					this->$conn=new PDO("mysql:host=serverName; dbname=$databaseName",$dbuser,$dbpassword);
					this->$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
					this->$conn->exec("set names utf8");
					
					
				}
				
				catch(Exception $e)
				{
					echo $e->getMessage();
					error_log($e->getMessage(),0);
				}
			}	
            return this->$conn;
		}
		
	}
?>
Tzu iT邦新手 1 級 ‧ 2020-12-25 00:06:34 檢舉

DEAR 大大們~終於過了12點
我可以又有發言權限了/images/emoticon/emoticon31.gif

剛剛去找了其他學php的學習發法..(所以之後會有新的問題?)
發現我在$pdo=new PDO('mysql:host=localhost:3307;dbname=shop;charset=utf8',
'帳號', 'password');
是可以的~

回到這裡又不行~
/images/emoticon/emoticon04.gif

https://ithelp.ithome.com.tw/upload/images/20201224/2011903507Jo2sRuXY.png
https://ithelp.ithome.com.tw/upload/images/20201224/201190357AHmYoGg0p.png

現在變成是這個錯誤

確認一下樓上大大說的

我所認知的...
變數->方法
PDO->query('SQL指令')

/images/emoticon/emoticon19.gif

this->$conn全部改成$conn試看看

柯柯 iT邦新手 3 級 ‧ 2020-12-25 13:23:33 檢舉

$this->conn 應該是這樣吧@@

Tzu iT邦新手 1 級 ‧ 2020-12-25 22:38:12 檢舉

https://ithelp.ithome.com.tw/upload/images/20201225/20119035F96BcIJlqL.png

https://ithelp.ithome.com.tw/upload/images/20201225/20119035papkEmzipC.png

https://ithelp.ithome.com.tw/upload/images/20201225/20119035A50vxMbxSE.png

https://ithelp.ithome.com.tw/upload/images/20201225/20119035y47aSgM39C.png

還是無解~/images/emoticon/emoticon06.gif

我把}去掉改;也不行
增加;也不行/images/emoticon/emoticon02.gif

第30列最後面要加一個分號;

return this->$conn;
Tzu iT邦新手 1 級 ‧ 2020-12-26 11:07:15 檢舉

https://ithelp.ithome.com.tw/upload/images/20201226/20119035IvvCoZsDQP.png
https://ithelp.ithome.com.tw/upload/images/20201226/20119035FnNcKvvjLW.png

加了之後又變回錯這個
/images/emoticon/emoticon02.gif

Tzu iT邦新手 1 級 ‧ 2020-12-27 09:23:18 檢舉

看來這題要永遠無解了
其實我這個是跟著在udemy的老師做的
只是老師不願意提供程式碼
奇怪~跟著老師做居然做不出來?

柯柯 iT邦新手 3 級 ‧ 2020-12-28 14:34:50 檢舉

$this->conn
你上面都寫$this->conn
30行怎又變成 this->$conn

Tzu iT邦新手 1 級 ‧ 2020-12-28 23:05:47 檢舉

https://ithelp.ithome.com.tw/upload/images/20201228/20119035xziRJQGl5P.pnghttps://ithelp.ithome.com.tw/upload/images/20201228/20119035EVrRXxT6os.png

還是不行 /images/emoticon/emoticon06.gif

你這份 code 的 serverName 沒有加上$符號,細心一點吧

$this->conn = new PDO("mysql:host=$serverName; dbname=$databaseName", $dbuser, $dbpassword);
Tzu iT邦新手 1 級 ‧ 2021-01-05 23:34:46 檢舉

加了之後還是跳出這個QQ

Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\conDB.php:13 Stack trace: #0 C:\xampp\htdocs\register.php(8): conDB::getConnection() #1 {main} thrown in C:\xampp\htdocs\conDB.php on line 13

Tzu iT邦新手 1 級 ‧ 2021-01-10 21:57:21 檢舉

雖然最後還是沒有解決..但是謝謝大大幫我找報port...所以先結案吧/images/emoticon/emoticon06.gif

0

mysql的port預設值因該是3306。
理論上你的db host只要設定localhost就好。

如果你並未去修改my.ini的話。

Tzu iT邦新手 1 級 ‧ 2020-12-24 18:59:54 檢舉

我的port是https://ithelp.ithome.com.tw/upload/images/20201224/20119035lw0090YHg2.png

0
耿直小伙
iT邦新手 1 級 ‧ 2020-12-24 14:03:54

你在 Class 內宣告了 static $conn,但你的呼叫方式卻是定義 function 內的 $conn,呼叫 Class 內的成員為 $this->conn

Tzu iT邦新手 1 級 ‧ 2020-12-24 18:57:47 檢舉
<?php
	class conDB
	{
		$this->conn;
		
		function getConnection()
		{
			$serverName ="localhost:3307";
			$databaseName="account";
			$dbuser="root";
			$dbpassword="";
			
			if(!isset($conn))
			{
				try
				{
					$conn=new PDO("mysql:host=serverName; dbname=$databaseName",$dbuser,$dbpassword);
					$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
					$conn->exec("set names utf8");
					
					
				}
				
				catch(Exception $e)
				{
					echo $e->getMessage();
					error_log($e->getMessage(),0);
				}
			}	
			return $conn;
		}
		
	}
?>

改成這樣變成錯在
https://ithelp.ithome.com.tw/upload/images/20201224/20119035H0dRf33pZa.png

https://ithelp.ithome.com.tw/upload/images/20201224/20119035venna1y2j1.png

Tzu iT邦新手 1 級 ‧ 2020-12-24 19:06:20 檢舉

https://ithelp.ithome.com.tw/upload/images/20201224/201190359v5Dk9rG2O.png

權限這裡看也是沒有問題

關於物件導向的概念你得找時間再去查查喔

<?php

class conDB
{
    protected $conn = null;

    function getConnection()
    {
        $serverName ="localhost:3307";
        $databaseName = "account";
        $dbuser = "root";
        $dbpassword = "";

        if ($this->conn !== null) {
            try {
                $this->conn = new PDO("mysql:host=$serverName; dbname=$databaseName", $dbuser, $dbpassword);
                $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->conn->exec("set names utf8");
            } catch (Exception $e) {
                echo $e->getMessage();
                error_log($e->getMessage(), 0);
            }
        }

        return $this->conn;
    }
}
0
akss5768
iT邦新手 5 級 ‧ 2021-05-24 10:48:25

端口是3307,仔細看看你的phpmyadmin

我要發表回答

立即登入回答