gpt4 book ai didi

php - Codeigniter 菜鸟问题

转载 作者:行者123 更新时间:2023-12-04 17:00:59 25 4
gpt4 key购买 nike

我是 CI 的菜鸟,我无法在数据库中插入数据,这是我所做的一切,它没有向我显示错误,但我没有得到结果,

admin.php( Controller )

public function postnews(){

$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == "my ip address"){

session_start();

if (!isset($_SESSION['admin'])){
$this->load->view('admin-login');
die(0);
}

if ($_SESSION['admin'] != 'loged'){
$this->load->view('admin-login');
die(0);
}

if ($_SESSION['admin'] == 'loged'){

if (isset($_POST['title'])and isset($_POST['main-poster']) and isset($_POST['type']) and isset($_POST['year']) and isset($_POST['language'])and isset($_POST['platform'])and isset($_POST['publisher'])and isset($_POST['size'])and isset($_POST['graphics'])and isset($_POST['little-info'])and isset($_POST['full-info'])and isset($_POST['posters'])and isset($_POST['screenshots'])and isset($_POST['trailers'])and isset($_POST['gameplays'])and isset($_POST['author'])){

$title = $_POST['title'];
$main_poster = $_POST['main-poster'];
$type = $_POST['type'];
$year = $_POST['year'];
$language = $_POST['language'];
$platform = $_POST['platform'];
$publisher = $_POST['publisher'];
$size = $_POST['size'];
$graphics = $_POST['graphics'];
$little_info = $_POST['little-info'];
$full_info = $_POST['full-info'];
$posters = $_POST['posters'];
$screenshots = $_POST['screenshots'];
$trailers = $_POST['trailers'];
$gameplays = $_POST['gameplays'];
$autor = $_POST['author'];
$date = date("d.m.Y");

$this->load->model('Gamesmodel');
echo $this->Gamesmodel->PostArticle($title, $main_poster, $type, $year, $language, $platform, $publisher, $size, $graphics, $little_info, $full_info, $posters, $screenshots, $trailers, $gameplays, $autor, $date);

}else{
$this->load->view('postnews');
}

}

} else {
$this->load->view('404.htm');
die(0);
}

}

gamemodel.php 模型
<?php
class Gamesmodel extends CI_Model {

function __construct()
{
// Call the Model constructor
parent::__construct();
}

function PostArticle($title, $main_poster, $type, $year, $language, $platform, $publisher, $size, $graphics, $little_info, $full_info, $posters, $screenshots, $trailers, $gameplays, $autor, $date)
{
$sql = "INSERT INTO game-articles (id, title, type, year, language, platform, publisher, size, graphics, little-info, full-info, posters, screenshots, trailers, gameplays, date, author) VALUES ('' ,".$this->db->escape($title).",".$this->db->escape($main_poster).",".$this->db->escape($type).",".$this->db->escape($year).",".$this->db->escape($language).",".$this->db->escape($platform).",".$this->db->escape($publisher).",".$this->db->escape($size).",".$this->db->escape($graphics).",".$this->db->escape($little_info).",".$this->db->escape($full_info).",".$this->db->escape($posters).",".$this->db->escape($screenshots).",".$this->db->escape($trailers).",".$this->db->escape($gameplays).",".$this->db->escape($date).",".$this->db->escape($author).")";
$this->db->query($sql);
return $this->db->affected_rows();
}
}

postnews.php View
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Post News</title>
</head>
<body style="background-color:black;">
<div style="margin:auto auto auto auto; width:800px; background-color:white; padding-top:20px; padding-bottom:20px; text-align:center;">
<form action="http://www.gameslib.net/admin/postnews" method="post"><br />
<input type="text" placeholder="title" name="title" style="width:300px;" /><br />
<input type="text" placeholder="main poster" name="main-poster" style="width:300px;" /><br />
<input type="text" placeholder="type" name="type" style="width:300px;" /><br />
<input type="text" placeholder="year" name="year"/><br />
<input type="text" placeholder="language" name="language" style="width:300px;" /><br />
<input type="text" placeholder="platform" name="platform" style="width:300px;" /><br />
<input type="text" placeholder="publisher" name="publisher" style="width:300px;" /><br />
<input type="text" placeholder="size" name="size"/><br />
<input type="text" placeholder="graphics" name="graphics" style="width:300px;" /><br />
<textarea name="little-info" placeholder="little-info" style="width:600px; height:100px;" ></textarea><br />
<textarea name="full-info" placeholder="full-info" style="width:600px; height:200px;" ></textarea><br />
<textarea name="posters" placeholder="posters" style="width:600px; height:50px;" ></textarea><br />
<textarea name="screenshots" placeholder="screenshots" style="width:600px; height:50px;" ></textarea><br />
<textarea name="trailes" placeholder="trailes" style="width:600px; height:50px;" ></textarea><br />
<textarea name="gameplays" placeholder="gameplays" style="width:600px; height:50px;" ></textarea><br />
<input type="text" placeholder="author" name="author" /><br />
<input type="submit" value="P O S T"/><br />
<input type="reset" value="reset"/><br />
</form>
</div>
</body>
</html>

请帮助我,我复制了几乎所有内容以确保我不会忽略某些内容,

最佳答案

好的,让我们从清理代码开始。不必在 if ($_SESSION['admin'] == 'loged') 中创建每个自变量方法,可以使用函数extract(); . extract()方法为提供的数组中的每个键创建一个变量。假设你有 key name在数组中 $_POST ,extract 方法将创建一个名为 name 的变量为你。要检索该值,您需要做的就是访问变量 $name .

if ($_SESSION['admin'] == 'loged'){

extract($_POST);

}

其次,你不用 and这个词如果你想在一个 if 语句中检查不止一件事情,你可以使用下面的操作数“&&”。
if (isset($_POST['title']) && isset($_POST['main-poster']) && isset($_POST['type']) && isset($_POST['year']) && isset($_POST['language']) && isset($_POST['platform']) && isset($_POST['publisher']) && isset($_POST['size']) && isset($_POST['graphics']) && isset($_POST['little-info']) && isset($_POST['full-info']) && isset($_POST['posters']) && isset($_POST['screenshots']) && isset($_POST['trailers']) && isset($_POST['gameplays']) && isset($_POST['author']))

而不是手动检查每个对象是否已在 $_POST 中设置数组,你可以遍历 $_POST .

创建需要设置的变量数组:
$req_fields = array(

'title',
'main-poster',
'type',
'year',
'language',
'platform',
'publisher',
'size',
'graphics',
'little-info',
'full-info',
'posters',
'screenshots',
'trailers',
'gameplays',
'author'

);

然后为尚未设置的元素创建一个数组:
$notset = array();

最后,遍历 $_POST检查是否设置了每个值。如果没有,请将其添加到数组中。
foreach ($req_fields as $key) {

if (!isset($_POST[$key]) {

$notset[] = $key;

}
}

然后检查是否有任何值尚未设置并重定向用户,否则,加载模型并回显帖子:
if (count($notset) > 0) {

$this->load->view('postnews');

}
else {

$this->load->model('Gamesmodel');
echo $this->Gamesmodel->PostArticle($title, $main_poster, $type, $year, $language, $platform, $publisher, $size, $graphics, $little_info, $full_info, $posters, $screenshots, $trailers, $gameplays, $autor, $date);

}

大概插入不工作背后的实际原因是因为它实际上没有被调用。这背后的原因是某些键实际上并未设置。

遍历 $notset数组以查看是否是这种情况:
foreach ($notset as $unsetField) {

echo "Field {$unsetField} is not set. <br />";

}

关于php - Codeigniter 菜鸟问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com