hi, i am building a cms, when a user(already logged in) post anything, the script will check if the user is already exist or not if not it will create a new account(only save username and useremail) for the cms, so i can keep track of users.
ok, so i already managed to get this part.
at first i used mysql extension but a daniweb member suggest me to use pdo, so i read some documents on pdo and write down the code below, but it is not working as expected, it do not save the data post by user. though it do save userdata if not exist, but for some reason the second query does seem to working.
<?php
$pdo_table = 'post';
$pdo_usertable = 'author';
$success_page = 'postsuccess.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form_name'] == 'save')
{
try
{
$pdo = new PDO('mysql:host=localhost;dbname=blog', 'Avik', '');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
echo $output;
exit();
}
$post_username = ($_POST['username']);
$post_useremail = ($_POST['useremail']);
try
{
$sql = "SELECT * FROM ".$pdo_usertable." WHERE authorname = '".$post_username."' AND authoremail = '".$post_useremail."'";
$result = $pdo->query($sql);
if (!$data = $result->fetch())
{
$sql = "INSERT `".$pdo_usertable."` (`id`, `authorname`, `authoremail`) VALUES (NULL, '$post_username', '$post_useremail')";
$s = $pdo->prepare($sql);
$s->execute();
}
}
catch (PDOException $e)
{
$error = 'error getting and iserting data';
echo $error;
exit();
}
$post_title = $_POST['title'];
$post_topic = $_POST['topic'];
$post_tags = $_POST['tags'];
$post_content = $_POST['elm1'];
try
{
$sql = "INSERT `".$pdo_table."` (`title`, `topicid`, `tags`, `postdate`, `content`) VALUES ('$post_title', '$post_topic', '$post_tags', CURDATE(), '$post_content')";
$s = $pdo->prepare($sql);
$s->execute();
}
catch (PDOException $e)
{
$error = 'error getting and iserting data';
echo $error;
exit();
}
header('Location: '.$success_page);
}
?>
need help!