Hej, znowu będę Wam zawracał głowę (IMG:
style_emoticons/default/smile.gif)
Mam pewien problem oczywiście z bazą, nie działa to zapytanie
$stmt = $conn->prepare("INSERT INTO articles (articleID, userID, author, title, artciletext, authorIP, createDate, editDate ) VALUES ('$articleID','$userID', '$user', '$title', '$articletext', '$ip' , '$date', '$modify')");
A tu cały kod
<?php
function checkExists($title){
try{
$conn = new PDO("mysql:host=localhost;dbname=wojtecki", 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT count(*) FROM articles WHERE title = '$title'");
$stmt->execute();
$row = $stmt->fetch();
$result = $row[0];
$conn = null;
if($result <> 1){
return 1;
}
else
{
header("Location: ../admin.php?id=news&news=exist"); return;
}
}
catch(PDOException $e){
$e ->getMessage();
}
}
function checkID(){
try {
$len = 20;
$conn = new PDO("mysql:host=localhost;dbname=wojtecki", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT count(*) FROM articles WHERE articleID = '$articleID'");
$stmt->execute();
$row = $stmt->fetch();
$result = $row[0];
$conn = null;
if ($result != 1){
return $articleID;
}
else{
checkID();
}
}
catch(PDOException $e)
{
$e->getMessage();
}
}
function getAuthor(){
try {
$userID = $_SESSION['log'];
$conn = new PDO("mysql:host=localhost;dbname=wojtecki", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT login, lastiplogin FROM users WHERE userID = '$userID'");
$stmt->execute();
$row = $stmt->fetch();
$author = $row['login'];
$authorArray["login"] = $author;
$authorArray["userID"] = $userID;
$authorArray['lastiplogin'] = $row['lastiplogin'];
return $authorArray;
$conn = null;
}
catch(PDOException $e)
{
$e->getMessage();
}
}
function insertNews($articletext,$title){
try {
$date = date("H.i d-m-Y"); $modify = date("H.i d-m-Y"); $articleID = checkID();
$author = getAuthor();
$userID = $author['userID'];
$user = $author['login'];
$ip = $author['lastiplogin'];
$conn = new PDO("mysql:host=localhost;dbname=wojtecki", 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'idartu'. $articleID; echo "<br>userID". $userID; echo "<br>title". $title; echo "<br>art". $articletext; echo "<br>userID". $userID; $stmt = $conn->prepare("INSERT INTO articles (articleID, userID, author, title, artciletext, authorIP, createDate, editDate ) VALUES ('$articleID','$userID', '$user', '$title', '$articletext', '$ip' , '$date', '$modify')");
$stmt->execute();
$conn = null;
}
catch(PDOException $e)
{
$e->getMessage();
}
}
$title ='dupa';
$articletext = 'fefefefefefefefwefefegrgewrrefwefegwecefa';
$check = checkExists($title);
if ($check == 1) {
insertNews($articletext,$title);
}
?>
Wiem, że problem jest z zapytaniem, bo skrypt działa do miejsca, w którym wyświetlam zmienne, które mają być do niej wpisane, wrzuciłem je do echo, bo myślałem, że może któraś zmienna jest pusta albo z nimi jest problem, ale widzę, że wyświetla je, tak jakimi powinny być.
Ten post edytował Dukov 7.08.2017, 20:05:31