代碼1: PHP MySQL數據庫問題
<?php class dbConnect { var $dbHost = 'localhost', $dbUser = 'root', $dbPass = '', $dbName = 'input_oop', $dbTable = 'users'; function __construct() {$dbc = mysql_connect($this->dbHost,$this->dbUser,$this->dbPass) or die ("Cannot connect to MySQL : " . mysql_error()); mysql_select_db($this->dbName) or die ("Database not Found : " . mysql_error()); } } class User extends dbConnect { var $name; function userInput($q) { $sql = "INSERT INTO $this->dbTable set name = '".$q."'"; mysql_query($sql) or die (mysql_error()); } } ?>
這是代碼調用的類。
<?php include ('class.php'); $q=$_GET["q"]; $user = new User; $user->userInput($q); ?>
代碼2:
<?php $q = $_GET['q']; $dbc=mysql_connect("localhost","root","") or die (mysql_error()); mysql_select_db('input_oop') or die (mysql_error()); $sql = "INSERT INTO users set name = '".$q."'"; mysql_query($sql) or die (mysql_error()); ?>
我代碼1保存在我的數據庫:
保存多個!
我代碼2保存在我的數據庫:
什麼是錯我的代碼1?
你最想做什麼? – 2010-04-27 18:11:19
除了您的實際問題:請記住在將每個傳入數據發送到數據庫之前對其進行驗證。否則,您的腳本將容易受到SQL注入的影響。以'mysql_real_escape()'爲例。 – Ham 2010-04-27 18:13:24
你如何在Code1中調用userInput()函數? – harwig 2010-04-27 18:13:54