나에게 표준적인 실행처럼 보이지 않으므로 여기에서 무슨 일이 일어나는지 확실하지 않습니다. 하지만 기본적으로 사용자가 코드 스 니펫을 제출할 수 있도록하는 기본적인 데이터베이스가 있습니다. 제출을 위해 최대 5 개의 태그를 제공 할 수 있습니다.PHP가 아포스트로피를 삽입하지 않아야하는 곳
지금은 아직 배우지 않으므로 이것이 분명한 경우 나에게 용서해주세요. 이 코드의 지루한 군침에 대한
function submitform()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->database();
$this->form_validation->set_error_delimiters('<p style="color:#FF0000;">', '</p>');
$this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[5]|max_length[255]|xss_clean');
$this->form_validation->set_rules('summary', 'Summary', 'trim|required|min_length[5]|max_length[255]|xss_clean');
$this->form_validation->set_rules('bbcode', 'Code', 'required|min_length[5]'); // No XSS clean (or <script> tags etc. are gone)
$this->form_validation->set_rules('tags', 'Tags', 'trim|xss_clean|required|max_length[254]');
if ($this->form_validation->run() == FALSE)
{
// Do some stuff if it fails
}
else
{
// User's input values
$title = $this->db->escape(set_value('title'));
$summary = $this->db->escape(set_value('summary'));
$code = $this->db->escape(set_value('bbcode'));
$tags = $this->db->escape(set_value('tags'));
// Stop things like <script> tags working
$codesanitised = htmlspecialchars($code);
// Other values to be entered
$author = $this->tank_auth->get_user_id();
$bi1 = "";
$bi2 = "";
// This long messy bit basically sees which browsers the code is compatible with.
if (isset($_POST['IE6'])) {$bi1 .= "IE6, "; $bi2 .= "1, ";} else {$bi1 .= "IE6, "; $bi2 .= "NULL, ";}
if (isset($_POST['IE7'])) {$bi1 .= "IE7, "; $bi2 .= "1, ";} else {$bi1 .= "IE7, "; $bi2 .= "NULL, ";}
if (isset($_POST['IE8'])) {$bi1 .= "IE8, "; $bi2 .= "1, ";} else {$bi1 .= "IE8, "; $bi2 .= "NULL, ";}
if (isset($_POST['FF2'])) {$bi1 .= "FF2, "; $bi2 .= "1, ";} else {$bi1 .= "FF2, "; $bi2 .= "NULL, ";}
if (isset($_POST['FF3'])) {$bi1 .= "FF3, "; $bi2 .= "1, ";} else {$bi1 .= "FF3, "; $bi2 .= "NULL, ";}
if (isset($_POST['SA3'])) {$bi1 .= "SA3, "; $bi2 .= "1, ";} else {$bi1 .= "SA3, "; $bi2 .= "NULL, ";}
if (isset($_POST['SA4'])) {$bi1 .= "SA4, "; $bi2 .= "1, ";} else {$bi1 .= "SA4, "; $bi2 .= "NULL, ";}
if (isset($_POST['CHR'])) {$bi1 .= "CHR, "; $bi2 .= "1, ";} else {$bi1 .= "CHR, "; $bi2 .= "NULL, ";}
if (isset($_POST['OPE'])) {$bi1 .= "OPE, "; $bi2 .= "1, ";} else {$bi1 .= "OPE, "; $bi2 .= "NULL, ";}
if (isset($_POST['OTH'])) {$bi1 .= "OTH, "; $bi2 .= "1, ";} else {$bi1 .= "OTH, "; $bi2 .= "NULL, ";}
// $b1 is $bi1 without the last two characters (,) which would cause a query error
$b1 = substr($bi1, 0, -2);
$b2 = substr($bi2, 0, -2);
// :::::::::::THIS IS WHERE THE IMPORTANT STUFF IS, STACKOVERFLOW READERS::::::::::
// Split up all the words in $tags into individual variables - each tag is seperated with a space
$pieces = explode(" ", $tags);
// Usage:
// echo $pieces[0]; // piece1 etc
$ti1 = "";
$ti2 = "";
// Now we'll do similar to what we did with the compatible browsers to generate a bit of a query string
if ($pieces[0]!=NULL) {$ti1 .= "tag1, "; $ti2 .= "$pieces[0], ";} else {$ti1 .= "tag1, "; $ti2 .= "NULL, ";}
if ($pieces[1]!=NULL) {$ti1 .= "tag2, "; $ti2 .= "$pieces[1], ";} else {$ti1 .= "tag2, "; $ti2 .= "NULL, ";}
if ($pieces[2]!=NULL) {$ti1 .= "tag3, "; $ti2 .= "$pieces[2], ";} else {$ti1 .= "tag3, "; $ti2 .= "NULL, ";}
if ($pieces[3]!=NULL) {$ti1 .= "tag4, "; $ti2 .= "$pieces[3], ";} else {$ti1 .= "tag4, "; $ti2 .= "NULL, ";}
if ($pieces[4]!=NULL) {$ti1 .= "tag5, "; $ti2 .= "$pieces[4], ";} else {$ti1 .= "tag5, "; $ti2 .= "NULL, ";}
$t1 = substr($ti1, 0, -2);
$t2 = substr($ti2, 0, -2);
$sql = "INSERT INTO code (id, title, author, summary, code, date, $t1, $b1) VALUES ('', $title, $author, $summary, $codesanitised, NOW(), $t2, $b2)";
$this->db->query($sql);
$this->load->view('subviews/template/headerview');
$this->load->view('subviews/template/menuview');
$this->load->view('subviews/template/sidebar');
$this->load->view('thanksforsubmission');
$this->load->view('subviews/template/footerview');
}
}
죄송합니다 :
여기이 모든 작업 (거기에 일부 CodeIgniter의 특정 기능이있을 수 있습니다)하게 PHP 스크립트입니다. 나는 아마도 거기에 몇 가지 나쁜 습관을 가지고 있음을 알고 있습니다.
이
은 (그것이 오류가 발생 전혀 조회되지 않음)처럼 출력 쿼리 모습입니다 :A Database Error Occurred
Error Number: 1136
Column count doesn't match value count at row 1
INSERT INTO code (id, title, author, summary, code, date, tag1, tag2, tag3, tag4, tag5, IE6, IE7, IE8, FF2, FF3, SA3, SA4, CHR, OPE, OTH) VALUES ('', 'test2', 1, 'test2', 'test2 ', NOW(), 'test2, test2, test2, test2, test2', NULL, NULL, 1, 1, 1, 1, 1, 1, 1, NULL)
당신은 (NOW 후 비트에서 볼 수), 'TEST2, TEST2 , test2, test2, test2 '- 나는 아포스트로피로 모든 것을 넣지 않았다. 내가 그랬어?
내가 이런 식으로 그 라인의 각을 넣어 할 수있는 : 단일 약 $ 따옴표 조각
if ($pieces[0]!=NULL) {$ti1 .= "tag1, "; $ti2 .= "'$pieces[0]', ";} else {$ti1 .= "tag1, "; $ti2 .= "NULL, ";}
[0] 등 -하지만 내 문제는 사용자 만 입력하면이 좀 실패하다 4 태그, 또는 3, 또는 뭐든간에.
죄송합니다. 역사상 최악의 문구가 있다면 죄송합니다.하지만 두뇌가 약화되었습니다.
도움 주셔서 감사합니다.
잭
음 ... 코드를 살펴볼 수 없으므로 지금 떠날 것입니다. 그러나 "테스트"는 어딘가에서 왔어야합니까? '$ tags'의 예제 입력을 보여줄 수 있습니까? –
걱정하지 마십시오. 정말 감사드립니다. 아마 사용하기 좋은 예제는 아닙니다. $ tags는 단일 텍스트 상자에서 가져온 $ _POST에서 가져옵니다. 내가 입력 한 내용은 다음과 같습니다. test2 test2 test2 test2 테스트 죄송합니다. 동일한 태그 등. 몇 가지 오류 코드가 표시됩니다. 도움이된다면 더 좋은 예를 게시 할 수 있습니다. – Jack