/    Sign up×
Community /Pin to ProfileBookmark

fairly basic php insert problem

Hi guys. im new to php so this is probably an easy spot for you guys but its doing my head in. im trying to insert data into a table using pdo. im not getting any error messages or anything its just not going in. any help is greatly appreciated. heres the code

MAIN PAGE

`<?php require_once(“includes/db.php”);?>
<?php require_once(“includes/functions.php”);?>
<?php require_once(“includes/sessions.php”);?>
<?php

if (isset($_POST[“submit”])){
$catagory = $_
POST[“title”];
$admin = “Cole”;

date_default_timezone_set(“Europe/Dublin”);
$currenttime = time();
$datetime= strftime(“%B-%d-%Y %H:%M:%S”, $currenttime);

if (empty($catagory)){
$_SESSION[“error”] = “please fill in the form correctly”;
redirect(“catagory.php”);
} elseif (strlen($catagory)<3){
$_
SESSION[“error”] = “catagory must be greater than two charachters”;
redirect(“catagory.php”);
}elseif(strlen($catagory)>49){
$_SESSION[“error”] = “catagory is too long”;
redirect(“catagory.php”);
}else {
$sql = “INSERT INTO catagory(title,author,datetime) VALUES(:catagoryname,:adminname,:dateTime)”;
$stmt = $connect->prepare($sql);
$stmt->bindValue(‘:catagoryname’, $catagory);
$stmt->bindValue(‘:adminname’, $admin);
$stmt->bindValue(‘:dateTime’, $datetime);
$Execute = $stmt->execute();

if($Execute){
$_SESSION[“success”] = “data added successfully”;
redirect(“basic.html”);
}else {
$_SESSION[“error”] = “something went wrong. data not added to table”;
redirect(“catagory.php”);
}

}
}

?>
<!doctype html>
<html>

<head>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<meta http-equiv=”X-UA-compatible” content=”ie=edge”>
<title>Catagories</title>
<link rel=”stylesheet” href=”https://use.fontawesome.com/releases/v5.7.2/css/all.css” integrity=”sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr” crossorigin=”anonymous”>
<link rel=”stylesheet” href=”https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css” integrity=”sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T” crossorigin=”anonymous”>
<link rel=”stylesheet” href=”css/styles.css”>
</head>

<body>

<!– NAVBAR START –>
<div style=”height:10px; background-color:rgb(100,100,100);”></div>
<nav class=”navbar navbar-expand-lg bg-dark navbar-dark”>
<div class=”container”>
<a href=”#” class=”navbar-brand”>jazebakram.com</a>
<button class=”navbar-toggler” data-toggle=”collapse” data-target=”#navbarcollapsecms”><span class=”navbar-toggler-icon”></span></button>
<div class=”collapse navbar-collapse” id=”navbarcollapsecms”>
<ul class=”navbar-nav mr-auto”>
<li class=”nav-item”><a href=”myprofile.php” class=”nav-link”> <i class=”fas fa-user”></i> Profile</a></li>
<li class=”nav-item”><a href=”dashboard.php” class=”nav-link”>Dashboard</a></li>
<li class=”nav-item”><a href=”posts.hp” class=”nav-link”>Posts</a></li>
<li class=”nav-item”><a href=”catagories.php” class=”nav-link”>Catagories</a></li>
<li class=”nav-item”><a href=”admins.php” class=”nav-link”>Manage admins</a></li>
<li class=”nav-item”><a href=”comments.php” class=”nav-link”>Comments</a></li>
<li class=”nav-item”><a href=”blog.php?page=1″ class=”nav-link”>Live blog</a></li>
</ul>
<ul class=”navbar-nav ml-auto”>
<li class=”nav-item”><a href=”logout.php” class=”nav-link”><i class=”fas fa-user-times”></i> Log out</a></li>
</ul>
</div>
</div>
</nav>
<div style=”height:10px; background-color:rgb(100,100,100);”></div>
<!– NAVBAR END –>

<!– HEADER START –>
<header class=”bg-dark text-white”>
<div class=”container”>
<div class=”row”>
<h1> <i class=”fas fa-edit style=”color: #27aae1;”></i> Manage catagories</h1>
</div>
</div>
</header>
<!– HEADER END –>

<!– MAIN AREA –>
<section class=”container py-2 mb-4″>
<div class=”row”>
<div class=”offset-lg-1 col-lg-10″ style=”min-height: 600px;”>
<?php
echo errormessage();
echo successmessage();
?>
<form action=”catagory.php” method=”post”>
<div class=”card bg-secondary text-light “>
<div class=”card-header”>
<h1>Add new catagory</h1>
</div>
</div>
<div class=”card-body bg-dark” >
<div class=”form-group”>
<label for=”title”><span class=”fieldinfo”>Catagory title:</span></label>
<input class=”form-control” type=”text” name=”title” id=”title” value=””>
</div>
<div class=”row”>
<div class=”col-lg-6 mb-2″>
<a href=”dashboard.php” class=”btn btn-warning btn-lg btn-block”><i class=”fas fa-arrow-left”>Back to dashboard</i></a>
</div>
<div class=”col-lg-6 mb-2″>
<button type=”submit” name=”submit” class=”btn btn-success btn-lg btn-block”><i class=”fas fa-check”>Publish</i></button>
</div>
</div>
</div>
</form>
</div>
</div>
</section>

<!– FOOTER START –>
<footer class=”bg-dark text-white”>
<div class-“container”>
<div class=”row”>
<div class=”col”>
<p class=”lead text-center”>Theme by Cole Pratt</p>
</div>
</div>
</div>
</footer>

<script src=”https://code.jquery.com/jquery-3.3.1.slim.min.js” integrity=”sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo” crossorigin=”anonymous”></script>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js” integrity=”sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1″ crossorigin=”anonymous”></script>
<script src=”https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js” integrity=”sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM” crossorigin=”anonymous”></script>

</body>

</html>
`
DATABASE CONNECT FILE
`
<?php

$DSN = “mysql:host = localhost, dbname=cms”;
$connect = new PDO($DSN, ‘root’, ”);

?>`

FUNCTIONS FILE

`<?php

function redirect($new_target){
header(“Location:”.$new_target);
exit;
}

?>
`

SESSIONS FILE

`<?php

SESSION_START();

function errormessage(){
if (isset($_SESSION[“error”])){
$output = “<div class=”alert alert-danger”>”;
$output .= htmlentities($_
SESSION[“error”]);
$output .= “</div>”;
$_SESSION[“error”] = null;
return $output;
}
}

function successmessage(){
if (isset($_SESSION[“success”])){
$output = “<div class=”alert alert-success”>”;
$output .= htmlentities($_
SESSION[“success”]);
$output .= “</div>”;
$_SESSION[“success”] = null;
return $output;
}
}

?>
`

THATS IT. THANKS IN ADVANCE

to post a comment
PHP

24 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 12.2019 — Before I even consider the possibility of wading through all of that code, consider putting this at the start of the main script:
<i>
</i>&lt;?php
ini_set('display_errors', true); // set to false in production
error_reporting(E_ALL);

Then see if that addresses...
im not getting any error messages [/quote]
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — still no error messages. i would really appreciate it if you could have a look
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — How about adding some echo statements to tell you how far this script gets? That's what good programmers do - they add stuff to help them trace the route that the program takes in its travels and make sure that what they THINK is happening or is supposed to be happening is ACTUALLY happening.
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — Hi ginerjm it works up until the insert else block, i dont know whats going wrong after or during that. i followed the tutorial to a tee so maybe there are some mysql settings i need to check?
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — So that I don't have to "find" it how about you isolate that part of your long posting and show us the code and your echo of the vars in use at that point so that you and we know what's going on?
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — heres the concerning php code. the quotes arent working here so im going to just paste it in


if (isset($_POST["submit"])){

$catagory = $_
POST["title"];

$admin = "Cole";

date_default_timezone_set("Europe/Dublin");

$currenttime = time();

$datet= strftime("%B-%d-%Y %H:%M:%S", $currenttime);

if (empty($catagory)){

$_SESSION["error"] = "please fill in the form correctly";

redirect("catagory.php");

} elseif (strlen($catagory)<3){

$_
SESSION["error"] = "catagory must be greater than two charachters";

redirect("catagory.php");

}elseif(strlen($catagory)>49){

$_SESSION["error"] = "catagory is too long";

redirect("catagory.php");

}else {

$sql = "INSERT INTO catagory(title,author,datetime) VALUES(:catagoryname,:adminname,:dateT)";

$stmt = $connect->prepare($sql);

$stmt->bindValue(':catagoryname', $catagory);

$stmt->bindValue(':adminname', $admin);

$stmt->bindValue(':dateTime', $datet);

$Execute = $stmt->execute();



if($Execute){
$_SESSION["success"] = "data added successfully";
redirect("basic.html");
}else {
$_SESSION["error"] = "something went wrong. data not added to table";
redirect("catagory.php");
}

}

}
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — oh i have since fixed the dateT error i made in the insert statement
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — Do you have PHP error checking turned on properly and is it displaying messages on the client screen? You have syntax errors that s/b preventing things from happening perhaps. Plus - does it matter that you misspell the word "category"?

I'm assuming that the query line that you referenced is this:

}else {

$sql = "INSERT INTO catagory(title,author,datetime) VALUES(:catagoryname,:adminname,:dateT)";


And where are the displays of variables that I asked you to add? The problem with most bugs is that the programmer makes assumptions of things without bothering to find out if he is correct in his assumptions. I see several OBVIOUS problems here that this little bit of detective work would have pointed out over 9 days ago.
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — sorry i just presumed that because the three if statements where i was checking empty and strlen were working ok i presumed that the variables were set. i tried to echo out the admin variable and it didnt show in the browser, im not sure if thats because its hiding behind the html or not though

php errors are turned on thanks to nogdogs post. i have been getting errors but no errors when i try to echo the admin variable it just doesnt show. the catagory misspell is incorrect but it is consistent throughout the script
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — I was expecting a new showing of the PERTINENT code with your corrected date and the contents of the necessary vars. As well as the code where you enable error checking. Since I can't see your problem areas you need to SHOW them to me/us.

And if you have error checking turned on I think you should be seeing some warnings about your misspelled global vars
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — ok i fixed the spelling errors. and to detirmine if the variables were set or not i removed the sql part and added an elseif to see if they were set using isset function and they are indeed being set once i submit the form. so the problem is the sql part. heres the revised code with the error reporting turned on near the start



<?php require_once("includes/db.php");?>

<?php require_once("includes/functions.php");?>

<?php require_once("includes/sessions.php");?>

<?php

ini_set('display_errors', true); // set to false in production

error_reporting(E_ALL);

?>

<?php

if (isset($_POST["submit"])){

$category = $_
POST["title"];

$admin = "Cole";

date_default_timezone_set("Europe/Dublin");

$currenttime = time();

$datetime= strftime("%B-%d-%Y %H:%M:%S", $currenttime);

if (empty($category)){

$_SESSION["error"] = "please fill in the form correctly";

redirect("catagory.php");

} elseif (strlen($category)<3){

$_
SESSION["error"] = "catagory must be greater than two charachters";

redirect("catagory.php");

}elseif(strlen($category)>49){

$_SESSION["error"] = "catagory is too long";

redirect("catagory.php");

}

else {

$sql = "INSERT INTO category(title,author,datetime) VALUES(:categoryname,:adminname,:datetime)";

$stmt = $connect->prepare($sql);

$stmt->bindValue(':categoryname',$category);

$stmt->bindValue(':adminname',$admin);

$stmt->bindValue(':datetime',$datetime);

$Execute = $stmt->execute();



if($Execute){
$_SESSION["success"] = "data added successfully";
redirect("basic.html");
}else {
$_SESSION["error"] = "something went wrong. data not added to table";
redirect("catagory.php");
}

}

}
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — for some reason there is a missing underscore when i set the category variable but it is there in the code
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — just didnt show up on here
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — do you see any problem with the following code? its the database connect file and when i fiddled with it it threw an error

<?php

$DSN = "mysql:host = localhost, dbname=cms";

$connect = new PDO($DSN, 'root', '');

?>

as it is there it doesnt show any errors. but when i removed the spaces around the = sign in the $DSN variable for the host part it threw an error.
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — My last attempt to communicate to you. PLEASE SHOW us the values being used. An isset is rather ridiculous to validate your values since if it is false then no assignment will happen. Plus without the echo you CANNOT see what the value is!

Echo Echo Echo please.

And why all of the php on and off tags? You do know that you don't have to do that? Since you are not showing any html of js code in this block you only need the initial <?php tag and no other ones here.

One more thing that may solve your problem. There is no value of $POST['title']. I can safely guarantee that.
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — i do appreciate your help but i have said that the variables are being set. that i can guarantee you. it is being echo'd in the html i didnt post that part because its just too much reading but you can rest assured that the values are being set. also i mentioned that the $POST was missing an underscore for some reason this site removed it but it is present in the code. do you see anything wrong with the db connect code i posted in the previous comment?
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — You are correct - the underscore doesn't show when I type it either. But it does show when you use $_SESSION at the end. How is that?

Any moderators reading along with this?

Anyway - please add the echos so we can get to the bottom of this.
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — It seems that the code you post outside of the <> symbol doesn't show the underscores. Please revise your methods of posting here.
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — $_POST

versus using a simple
and the proper close &lt;code&gt; tag with a / included
[code]
$_POST


(The </> icon isn't working for me right now.)
Copy linkTweet thisAlerts:
@coleioauthorMar 21.2019 — ive just spotted the problem. i was missing the semi colon after localhost in the db connect file. like i said there was nothing wrong with the variables being set. thanks for your help
Copy linkTweet thisAlerts:
@NogDogMar 21.2019 — Underscores have a format meaning in markdown, which this form software uses -- for better or worse. Therefore, be sure to put all source code within [code]...[/code] tags, or you can use single </ESC>back-ticks<ESC> around bits of code like variable names such as $_SESSION or $hello_world.
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — Interesting to learn. Silly but interesting. Can you also tell me why the </> symbol isn't working?
Copy linkTweet thisAlerts:
@NogDogMar 21.2019 — > @ginerjm#1602076 Can you also tell me why the </> symbol isn't working?

Nope, I just moderate stuff -- nothing to do with forum code maintenance (and no inclination to dive into the JavaScript -- yuck! -- that probably is the source of the problem ;) ).
Copy linkTweet thisAlerts:
@ginerjmMar 21.2019 — It's probably another IE glitch from the recent maintenance.
×

Success!

Help @coleio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...