I'm trying to make a way to edit my posts on a blog I'm making but for some reason when I try to submit the "update post" form it will give me the error "Something went wrong..." (meaning it got to update post.php) and I'm not sure why. The only thing I could see it being is because I'm using TinyMCE to edit the content of the post and the way I'm doing it is wrong?
editpost.php
<?php
include 'php/mysql_connect.php'; // opens a PDO of variable $db
if(isset($_GET['id'])){
$q = $db->prepare('SELECT * FROM posts WHERE id=:post_id LIMIT 1');
$q->execute(array(':post_id'=>$_GET['id']));
$row = $q->fetch(PDO::FETCH_ASSOC);
if($row){
echo '<form method="post" action="php/update_post.php?post_id='.$_GET['id'].'">';
echo '<div class="form-group">';
echo ' <input type="text" class="form-control" name="title" id="title" placeholder="Post Title" autocomplete="off" value="'.$row['title'].'" required />';
echo '</div>';
echo '<div class="form-group">';
echo ' <textarea class="form-control" name="body" id="body">'.$row['body'].'</textarea>';
echo '</div>';
echo '<input type="submit" value="Update Post" class="btn btn-default" />';
echo '</form>';
}
else{
echo 'Post not found.';
}
}
else{
echo 'Post not found.';
}
?>
update_post.php
<?php
$post_id = $_GET['post_id'];
$title = $_POST['title'];
$body = $_POST['body'];
include 'mysql_connect.php'; // establishes $db, a PDO connection
// insert the records
$sql = "UPDATE posts SET title=:title, body=:body WHERE id=:post_id)";
$q = $db->prepare($sql);
if($q->execute(array(':title'=>$title, ':body'=>$body, ':post_id'=>$post_id))){
echo '<script type="text/javascript">alert("Success!");location.href="../posts.php";</script>';
}
else{
echo '<script type="text/javascript">alert("Something went wrong...");location.href="../posts.php";</script>';
}
?>
I've changed the form method to GET, and it is passing the variables correctly, so that isn't the problem. The update_post.php is a modified version of my add_post.php, which works perfectly fine so I don't understand why updating it doesn't work right.
$sql = "UPDATE posts SET title=:title, body=:body WHERE id=:post_id)";
remove this one >-----^
you have a bracket at the end wrong ;)
Remove it and it should work:
$sql = "UPDATE posts SET title=:title, body=:body WHERE id=:post_id";
If you use GET use GET then ;-)
$post_id = $_GET['post_id'];
$title = $_GET['title'];
$body = $_GET['body'];
if you use POST use POST:
$post_id = $_POST['post_id'];
$title = $_POST['title'];
$body = $_POST['body'];
According to your last comment try change here:
if($row){
echo '<form method="post" action="php/update_post.php">';
echo '<input type="hidden" name="post_id" value="'.$_GET['id'].'">';
echo '<div class="form-group">';
echo ' <input type="text" class="form-control" name="title" id="title" placeholder="Post Title" autocomplete="off" value="'.$row['title'].'" required />';
echo '</div>';
echo '<div class="form-group">';
echo ' <textarea class="form-control" name="body" id="body">'.$row['body'].'</textarea>';
echo '</div>';
echo '<input type="submit" value="Update Post" class="btn btn-default" />';
echo '</form>';
}
Related
This is my code to print Questions and then their respective Answers.
<?php
$reg = $_SESSION['reg'];
$sql1="select users.reg,questions.question,questions.id from users,questions where users.reg=questions.reg and questions.company='infosys' and questions.reg!='$reg' limit 0,5";
$query1=mysqli_query($conn,$sql1);
$sql2="select questions.id,answers.reg,answers.answer from questions,answers where questions.id=answers.id";
$query2=mysqli_query($conn,$sql2);
while($row1 = mysqli_fetch_assoc($query1)){
echo "<p class='fill'><bold>".$row1["reg"]."</bold> asked:-" ;
echo '<br>QID:-'.$row1['id'].' "'.$row1["question"].'"<br></p>';
while($row2 = mysqli_fetch_assoc($query2)){
echo "<pre align='right'><bold>".$row2["reg"]."</bold> answered:-" ;
echo '<br> "'.$row2["answer"].'"</pre>';
}
echo '<form method="post" style="display:none;">';
echo '<textarea rows="2" cols="80" name="ans" class="form-control" required></textarea>';
echo '<button class="w3-button w3-blue w3-padding-medium w3-medium w3-margin-top w3-right" type="submit" name="answer">submit</button></form>';
echo '<button class="w3-button w3-blue w3-padding-small w3-small w3-right w3-margin-top show_button2">Answer</button><br><br>';
}
?>
But I'm not able to get correct output and plus I cant get the answer from the text area and insert into table with correct question ID. My INSERT code is:
if(isset($_POST['answer'])){
$id = $_SESSION['id'];
$reg = $_SESSION['reg'];
$answer = $_POST['ans'];
$sql="insert into answers VALUES('$id','$reg','$answer')";
$query=mysqli_query($conn,$sql);
if($query==TRUE){
header("location: infosys.php");
}
}
Please tell me how can i access the $row variable from that scope this insert scope
I got the solution. I stored the $row1['id'] in $id and stored that in "Submit" button's value
$id=$row31['id'];
echo '<form method="post" style="display:none;">';
echo '<textarea rows="2" cols="80" name="ans" class="form-control" required></textarea>';
echo '<button type="submit" name="answer_BTN" value='.$id.'">submit</button></form>';
and then i accessed the value via $_POST['answer_BTN'].
if(isset($_POST['answer_BTN'])){
$id = $_POST['answer_BTN'];
$reg = $_SESSION['reg'];
$answer = $_POST['ans'];
$sql2="insert into answers VALUES('$id','$reg','$answer')";
$query2=mysqli_query($conn,$sql2);
if($query2==TRUE){
header("location: infosys.php");
echo "<script>alert('Added your Answer!')</script>";
}
}
are you checking for submit on the same PHP File or another one?
if its another file mention the location here:
echo '<form method="post" action="../location/to/yourfile.php" style="display:none;">';
else make sure you are checking isset() at the top of the page
I have struggled with this issue for a majority of the day and I am really stumped.
I have this page called preview.php with the following code below. Values are being passed to this page from another page called order.php. This preview.php page allows a user to review his or her data input from order.php.
If correction is needed, the user returns to order.php to make his or her corrections. If everything looks fine, the user sends his or her orders to process.php to be processed. The information sent to process.php are hidden form fields.
For some reason that I am unable to figure out, process.php is not recognizing the hidden form field values.
Does anyone have an idea how to resolve this?
This gentle man #spencer7593 actually helped me out with this insert statement. However, each time I attempt to add a record, I get the following:
Error: custname value cannot be null
This happens because the hidden form values are null and I can't figure out why.
preview.php
?php
error_reporting(E_ALL);
echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>";
if(isset($_POST['custname']))
$custname = $_POST['custname'];
if(isset($_POST['zip']))
$zip = $_POST['zip'];
if(isset($_POST['city']))
$city = $_POST['city'];
if(isset($_POST['email']))
$email = $_POST['email'];
$address = $_POST['address'];
$rowIDs = $_POST['rowIDs'];
$row2IDs = $_POST['row2IDs'];
echo $custname .'<br>';
echo $zip .'<br> <hr width=400 align=left>';
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
$agentname = $_POST['agentname' . $id];
$agentaddress = $_POST['agentaddress' . $id];
$agentincome = $_POST['agentincome' . $id];
echo 'Name: '. $agentname . '<br />';
echo 'Address: '. $agentaddress . '<br />';
echo 'agentincome: '. $agentincome . '<br /><br>';
}
?>
<body>
<form action='process.php' method = 'POST'>
<input type='hidden' name='custname' value="<?php echo $custname; ?>">
<input type='hidden' name='address' value="<?php echo $address; ?>">
<input type='hidden' name='email' value="<?php echo $email; ?>">
<input type='hidden' name='city' value="<?php echo $city; ?>">
<input type='hidden' name='zip' value="<?php echo $zip; ?>">
<input type='hidden' name='agentname' value="<?php echo $agentname; ?>">
<input type='hidden' name='agentaddress' value="<?php echo $agentaddress; ?>">
<input type='hidden' name='agentincome' value="<?php echo $agentincome; ?>">
<input type="action" type="button" value="Return to data" onclick="history.go(-1);" /> | <input type="submit" value="Submit your form" />
</form>
</body>
process.php
<?php
global $wpdb;
// Database connection
$conn = mysqli_connect("localhost","myuser","mypwd","myDB");
if(!$conn) {
die('Problem in database connection: ' . mysql_error());
}
// Data insertion into database
$sql = 'INSERT INTO `myDB`.`myTable` ( `custname`'
. ', `address`, `city`, `zip`, `email` )'
. ' VALUES ( ? , ? , ? , ? , ? )';
if( $sth = mysqli_prepare($conn,$sql) ) {
mysqli_stmt_bind_param($sth,'sssss'
,$_POST["custname"]
,$_POST["address"]
,$_POST["city"]
,$_POST["zip"]
,$_POST["email"]
);
if( mysqli_stmt_execute($sth) ) {
echo '<h1>Thank you</h1> <br> Go back to the main page <a href="index.php");';
} else {
printf("Error: %s\n",mysqli_stmt_error($sth));
}
} else {
printf("Error: %s\n",mysqli_connect_error($conn));
}
?>
I'm in need of a bit help. I'm trying to find out how to associate a specific query (deletion of a record) with not the id of a record, but the record with which another query (selection of a record) is echoed out.
This line of code totally works when the id is specified, but again I need it for the record that gets called, where the id can skip numbers if I delete a record.
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
I've got a table in my phpmyadmin database with columns 'id', 'pagetitle', 'toevoeging' (addition in Dutch) , 'message'. First one is an INT, rest are varchars/text.
This may be a stupid question, I'm sorry for that. I'm still new to PHP, and to programming in general.
Here is the code. I've commented on lines code to clarify. Thanks you!.
<?php
if (isset($_SESSION['email'])) //if the admin is active, forms can be written out.
{
echo '</nav>
<br><br> <div class="inlogscript">
<form action="verstuurd.php" method="post">
<input type="text" placeholder="Titel" method="POST" name="pagetitle" /><br><br>
<input type="text" placeholder="Toevoeging" method="POST" name="toevoeging" /><br><br>
<textarea class="pure-input-1-2" placeholder="Wat is er nieuws?" name="message"></textarea><br>
<input type="submit" value="Bevestigen" />
</form></div>';
}
?>
<div class="mainContent">
<?php
include_once("config.php"); //this is the database connection
$query = "SELECT * FROM paginas "; //selects from the table called paginas
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
$pagetitle = $row['pagetitle'];
$toevoeging = $row['toevoeging'];
$message = $row['message'];
echo '<article class="topcontent">' . '<div class="mct">' . '<h2>' . "$pagetitle" .'</h2>' . '</div>' . "<br>" .
'<p class="post-info">'. "$toevoeging" . '</p>' . '<p class="post-text">' . '<br>'. "$message" . '</p>' .'</article>' . '<div class="deleteknop">' . '<form method="post">
<input name="delete" type="submit" value="Delete Now!">
</form>' . '</div>' ;
} //This long echo will call variables $pagetitle, $toevoeging and &message along with divs so they automatically CSS styled,
//along with a Delete button per echo that has the 3 variables
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked
{
$resulttwo = $mysqli->query($querytwo);
}
?>
</div>
</div>
Also here is the Insert INTO query of the records. Thanks again!
$sql = "INSERT INTO paginas (pagetitle,toevoeging, message)
VALUES ('$_POST[pagetitle]','$_POST[toevoeging]','$_POST[message]')";
//the insertion into the table of the database
if ($MySQLi_CON->query($sql) === TRUE) {
echo "";
} else {
echo "Error: ". $sql . "" . $MySQLi_CON->error;
}
This won't be sufficient but, to begin with your echo :
echo '<article class="topcontent">
<div class="mct">
<h2>' . $pagetitle .'</h2>
</div><br>
<p class="post-info">'. $toevoeging . '</p>
<p class="post-text"><br>'.$message.'</p>
</article>
<div class="deleteknop">
<form method="post">';
// you ll want to use $_POST["id"] array to delete :
echo '<input type="hidden" name="id" value="'.$row['id'].'">
<input name="delete" type="submit" value="Delete Now!">
</form>
</div>' ;
I'm trying to integrate CKEditor into my simple CMS. I got it to show up on the page, but it's just above everything. I'm wondering how to get it into the correct spot, below my title textbox? Here is my code:
require_once 'conn.php';
include_once 'ckeditor/ckeditor.php';
$CKEditor = new CKEditor();
$CKEditor->editor('body');
$title= '';
$body= '';
$article= '';
$author_id= '';
if (isset($_GET['a'])
and $_GET['a'] == 'edit'
and isset($_GET['article'])
and $_GET['article']) {
$sql = "SELECT title, body, author_id FROM cms_articles " .
"WHERE article_id=" . $_GET['article'];
$result = mysql_query($sql, $conn) or
die ('Could not retrieve article data: ' . mysql_error());
$row = mysql_fetch_array($result);
$title = $row['title'];
$body = $row['body'];
$article = $_GET['article'];
$author_id = $row['author_id'];
}
require_once 'header.php';
?>
<form method="post" action="transact-article.php">
<h2>Compose Article</h2>
<p>
Title: <br />
<input type="text" class="title" name="title" maxlength="255" value="<?php echo htmlspecialchars($title); ?>" />
</p>
<p>
Body: <br />
<textarea class="body" name="body" id="body" rows="10" cols="60"><?php echo htmlspecialchars($body); ?></textarea>
</p>
<p>
<?php
echo '<input type="hidden" name="article" value="' .
$article . "\" />\n";
if ($_SESSION['access_lvl'] < 2) {
echo '<input type="hidden" name="author_id" value="' .
$author_id . "\" />\n";
}
if ($article) {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Save Changes\" />";
} else {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Submit New Article\" />";
}
?>
</p>
</form>
Personally I don't think you need the PHP library. Just add
<div contenteditable="true">
Editable text
</div>
as your editable and then just the script to get it running:
<script type="text/javascript" src="/path/to/ckeditor/ckeditor.js"></script>
That said, you may be able to pass the id of your textarea to the PHP library. To avoid confusion with the body tag, rename the id and name of this control to editable_content or similar. And as I mention above, try using a div instead.
I need to insert some form values into a db table , how would I create a function to call once the user clicks on the button and run the insert scrtipt .
<form name="quiz_info" method="post">
<?php
echo $this->quiz->title;
echo $mainframe->getPageTitle();
echo '<p><input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed">' . JText::_('I have Read and Acknowledge the procedure'). '</label></p>' ;
echo '<input id="proceedButton" name="proceedButton" disabled="true" value="' . JText::_('Acknowledge') . '" type="submit" />' ;
//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge` (course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
?>
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP is executed on the server-side. If you want to call the function without reloading the page you will have to use an AJAX call, for example with jQuery.
You can find billions of tutorials through google.
<?php
if ($_POST['proceedButton'] != '') {
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge`(course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
}
?>
<form name="quiz_info" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $this->quiz->title; ?>
<?php echo $mainframe->getPageTitle(); ?>
<input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed"><?php echo JText::_('I have Read and Acknowledge the procedure'); ?></label>
<input id="proceedButton" name="proceedButton" disabled="true" value="<?php JText::_('Acknowledge'); ?>" type="submit" />
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP doesn't work this way. If you wish to activate your PHP script on form submit, you'll need to submit the form to a PHP page, and on that page to put your script, for example
<form action=submit.php method=post>
<input type=text name=text>
<input type=submit>
</form>
Next, on submit.php:
<?php
if (!empty($_POST['text'])) { //If the POST variable set by input named 'text' is not empty...
echo $_POST['text']; //Print it on the screen.
} else { //If it is empty
echo "No form submission detected"; //Print an error
}
?>
If you want it to work without a page reload, you'll have to use some client side technology. The most popular one for that purpose is AJAX