PDA

View Full Version : مشکل این کد چیه



ghadirkiani
یک شنبه 14 دی 1393, 22:55 عصر
سلام
من تو php تازه کارم و دارم تمرین میکنم
من یه دیتابیس ساختم و درونش یه جدول که دارای سه فیلد id title content هستش
میخوام توش مطلب ذخیره کنم
حالا یه کلاس براش نوشتم که کد زیر هستش
میشه بگید چه مشکلی داره که کار نمیکنه؟ هیچ اروری هم نمیده


<?phpinclude 'config1.php';
$title=$_POST['title'];
$content=$_POST['content'];
class zahra{
private $host;
private $root;
private $pass;
private $db;
private $title;
private $content;
function __construct() {
global $host;
global $root;
global $pass;
global $db;
global $title;
global $content;
$this->host = $host;
$this->root = $root;
$this->pass = $pass;
$this->db = $db;
$this->title = $title;
$this->content = $content;
}
/*------------------------------------------------------------------------------------*/
public function connect() {
mysql_connect($this->host,$this->root,$this->pass);
mysql_select_db($this->db);
}
/*------------------------------------------------------------------------------------*/
public function getnewsid(){
$this->connect();
$id = 0;
$result = mysql_query("SELECT * FROM `ghadir` ORDER BY `id` DESC LIMIT 1 ");
if (mysql_num_rows($result) > 0) {
$id = mysql_result($result,0,0);
} $id++;
return $id;}
/*------------------------------------------------------------------------------------*/
public function insertcontent($title,$content){
$this->connect();
$id = $this->getnewsid();
$result = mysql_query("INSERT INTO `ghadir` VALUES ('$id',$this->title','$this->content')");
return mysql_affected_rows();}
/*------------------------------------------------------------------------------------*/
}
$obja = new zahra;
$obja->insertcontent($title,$content);
?>

arenaw
دوشنبه 15 دی 1393, 11:03 صبح
1- توی فانکشین __construct()به دیتابیس کانکت شید، نه که توی هر فانکشن جداگونه اینکارو انجام بدین.
2- مطمئن شید توی صفحه‌ی config1.php متغیرهای pss, root, host درست مقدار دهی میشن.

یه پیشنهاد هم اینکه فیلد id توی تیبل ghadir رو AUTO_INCREMENT قرار بدین. با این کار وقتی میخواید یه رکورد اضافه کنید، دیگه لازم نیست به فیلد id مقدار بدید و کافیه که از NULL استفاده کنید. مای‌اس‌کیو‌ال خودش میاد یه آیدی غیر تکراری به رکوردتون میده.

DR.HTML
دوشنبه 15 دی 1393, 13:55 عصر
معمولا وقتی اروری دریافت نمیشه مشکل توی کدهای Sql هست

vahid78
دوشنبه 15 دی 1393, 14:03 عصر
این خط کد مشکل داره:


$result = mysql_query("INSERT INTO `ghadir` VALUES ('$id',$this->title','$this->content')");

درستش:


$result = mysql_query("INSERT INTO `ghadir` VALUES ('$id','$this->title','$this->content')");