PDA

View Full Version : راهنمایی در مورد ثبت فیلد تکراری



ali1150
یک شنبه 19 دی 1395, 01:27 صبح
سلام نمیدونم اینجا درست تاپیک زدم یا نه .. تازه اومدم به انجمن.

راهنمایی میخواستم از دوستان کار بلد.

چطور میشه کاری کرد اگه یک فیلد تکراری بود تو دیتابیس ثبت نشه و یه پیغام نمایش بده.

کد زیرو لطفا ببینید.



include('config.php');

if(isset($_POST['btn'])){


$name = $_POST["nam"];
$emeil = $_POST["em"];
$namefilm = $_POST["nf"];
$vipuser = $_POST["us"];
$publicyear = $_POST["py"];
$ip = $_POST["ips"];
if(empty($name) || empty($emeil) || empty($namefilm) || empty($vipuser) || empty($publicyear)){
echo "<font color=red>"."وارد کردن تمامی موارد الزامی است"."</font>"."";
}
else{

$insert = "INSERT INTO `list` (`name`, `emeil`, `namefilm`, `vipuser`, `publicyear`,`ip`,`cod`) VALUES (:name,:emeil,:namefilm,:vipuser,:publicyear,:ip,: cod);";

$str="0123456789abcdefghijklmnopqrstuvwxyz";
$max=strlen($str)-1;
$result="";
for($i=0;$i<5;$i++)
{
$random=rand(0,$max);
$char=substr($str,$random,1);
$result.=$char;
}
$cod1=strtoupper($result);


$result=$connect->prepare($insert);
$result->bindparam(":name",$name);
$result->bindparam(":emeil",$emeil);
$result->bindparam(":namefilm",$namefilm);
$result->bindparam(":vipuser",$vipuser);
$result->bindparam(":publicyear",$publicyear);
$result->bindparam(":ip",$ip);
$result->bindparam(":cod",$cod1);
$query=$result->execute();
if($query){
echo "فیلم درخواستی شما ثبت شد کد پیگیری شما : ".$cod1." می باشد ";
}//query
else{
echo "<font color=red>"."خطایی رخ داده است لطفا مجدد امتحان کنید"."</font>"."";
}
}
}//btn

[younes]
یک شنبه 19 دی 1395, 05:03 صبح
نمیشه خوندش....
لطفا داخل تگ php کدتون رو ارسال کنید....

ali1150
یک شنبه 19 دی 1395, 13:20 عصر
گذاشتم داخل تگ PHP چک کنید الان . اگه نشد لطفا تو ندپد بریزید ببینید راحت تر کد رو . تشکر

charcharkh
یک شنبه 19 دی 1395, 13:29 عصر
اگر منظور شما رو درست متوجه شده باشم -> ایتو بخون: البته برای کد ایگنایتر هست. سعی کنید فریم ورک یاد بگیرید خیلی مشکلاتتون حل میشه

elements($items, $array[, $default = NULL])


Parameters:


[*=left]$item (string) – Item to fetch from the array
[*=left]$array (array) – Input array
[*=left]$default (bool) – What to return if the array isn’t valid




Returns:

NULL on failure or the array item.


Return type:

mixed



Lets you fetch a number of items from an array. The function tests whether each of the array indices is set. If an index does not exist it is set to NULL, or whatever you’ve specified as the default value via the third parameter.Example:$array = array(
'color' => 'red',
'shape' => 'round',
'radius' => '10',
'diameter' => '20'
);

$my_shape = elements(array('color', 'shape', 'height'), $array);



The above will return the following array:array(
'color' => 'red',
'shape' => 'round',
'height' => NULL
);



You can set the third parameter to any default value you like.$my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar');



The above will return the following array:array(
'color' => 'red',
'shape' => 'round',
'height' => 'foobar'
);



This is useful when sending the $_POST array to one of your Models. This prevents users from sending additional POST data to be entered into your tables.$this->load->model('post_model');
$this->post_model->update(
elements(array('id', 'title', 'content'), $_POST)
);



This ensures that only the id, title and content fields are sent to be updated.

ali1150
یک شنبه 19 دی 1395, 13:49 عصر
همینو به صورت عملی میتونید تو کدهایی که قرار دادم پیاده کنید ؟ ضروریه. تشکر

ali1150
دوشنبه 20 دی 1395, 15:50 عصر
کسی نیست یه راهنمایی درست حسابی کنه تو این انجمن ؟

numberone1
دوشنبه 20 دی 1395, 19:40 عصر
سلام

فیلد هایی که میخوای تکراری نباشه باید تو دیتابیس بهشوت کلید یکتا بدی اینجوری کوئری insert اجرا نمیشه
راه دیگش هم اینه که قبل از کوئری insert کوئری select بزنی و توی شرطش چک کنی که اون مقدار هست یا نه اگر بزرگتر از 0 بود یعنی قبلا ثبت شده
اگر اپدیت کردن مقدار قبلی هم برات مهم نباشه میتونی از کوئری replace بجای insert استفاده کنی . ولی باید قبلش فیلد unique تعریف کرده باشی