UPDATE
I rewrote the code that was missing from the above post and included it in this one, i tried to run it and everything works except the part that inserts into teh tag table and also teh comments table does not get created(i had that problem before too, nothing new) I am really stumped as to why, because i feel taht all my logic is correct and my code is clean, maybe im just missing something stupid
.
Here is all the code:
PHP Code:
<?php
//create variables
$title = $_POST['title'];
$article = $_POST['post'];
$tags = $_POST['tags'];
$o_id = $_POST['o_id'];
$time = $_SERVER['REQUEST_TIME'];
$today = date("l, M d o @ g:ia T", $time);
$year = date("Y", $time);
if (empty($title) || $title == ' '){
echo '<meta http-equiv="refresh" content="0; URL=addPost.php?error=noname">;';
exit;
}else if (empty($article) || $article == ' '){
echo '<meta http-equiv="refresh" content="0; URL=addPost.php?error=nopost">;';
exit;
}
//connect to database
$dbuser = '************';
$dbpass = '************';
$dsn = '************;';
try {
$dbcon = new PDO($dsn, $dbuser, $dbpass);
} catch (PDOException $e) {
echo 'could not connect to database';
echo 'Connection Failed: '. $e->getMessage();
exit;
}
//insert new row
$sql = 'INSERT INTO Articles (o_id, Time, Year, Title, Post) VALUES (:o_id, :today, :year, :title, :post)';
$stmt = $dbcon->prepare($sql);
//preform query
try {
$stmt->execute(array(
':o_id' => $o_id,
':today' => $today,
':year' => $year,
':title' => $title,
':post' => $article)
);
} catch (PDOException $e){
echo 'could not insert article into database';
exit;
}
//create tags
$tag = explode(',', $tags);
for ( $i=0; $i < count($tag); $i++){
$tag[$i] = trim($tag[$i]);
}
echo "<pre>\no_id = {$o_id}\n";
print_r($tag);
echo "</pre>";
//insert tags into table
$sqlTag = 'INSERT INTO Tags (id, Tags) VALUES (:t_id, :tags)';
try {
if ($stmtTag = $dbcon->prepare($sqlTag))
{
//run query
foreach ($tag as $thetag)
{
$stmtTag->execute(array(
't_id' => $o_id,
'tags' => $thetag)
);
}
} else {
exit("Couldn't prepare the statement.");
}
}
catch (PDOException $e)
{
echo 'could not insert tags into database:' . $e->getMessage();
exit;
}
//create comments table
$tablename = 'comment'.$o_id;
$sqlComment = 'CREATE TABLE :tableName
(
id INT NOT NULL PRIMARY AUTO_INCREMENT,
Name VARCHAR(9999) NOT NULL,
Email VARCHAR(9999) NOT NULL,
Site VARCHAR(99999),
Comment VARCHAR(999999) NOT NULL
)';
try{
$stmtComment = $dbcon->prepare($sqlComment);
if($stmtComment){
//run query
$stmtComment->execute(array(
':tableName' => $tablename)
);
}else{
//if problem with $dbcon->prepare
exit('Could not prepare MySQL statement!');
}
}
catch(PDOException $e){
echo 'there was a problem creating the comment table'.$e->getMessage();
}
//manipulate file system
$dir = 'Articles/'.$year;
$fname = rawurlencode($title);
if (!is_dir($dir)){
mkdir($dir, 0777);
}
//create article specific file
$location = $dir.'/'.$fname.'.php';
//check if file exists
if (file_exists($location)){
$location2 = $dir.'/'.$fname.'2.php';
$fp = fopen($location2, 'x+');
}else{
$fp = fopen($location, 'x+');
}
//create data to write
$npage = <<<HERE
HERE;
?>
Bookmarks