I decided to leave in all the code to make it less confusing to those who see this.
On line #57, the only form on this page, I'm trying to $_POST the id that equals the post_iD.
Everything uploads correctly to MySQL besides the post_iD.
I always get a Notice: Undefined index: post_iD on $post_iD = $_POST['post_iD']; inside the if(isset($POST['comment'])).
I'm sure the issue is with how I'm trying to retrieve the post_iD inside the form, and not any issues with the PDO but html as PDO the data is being inserted correctly besides the post_iD which I have mentioned.
I'm using post_iD to loop posts from database, it works besides inside the form, any enlightenment with this issue?
Code Below.
if(isset($_POST['comment'])){
$comment = $_POST['comment'];
$post_iD = $_POST['post_iD'];
$data = $Wall->Insert_Comment( $uiD, $post_iD, $comment, $_SERVER['REMOTE_ADDR'] );
}
if ( $updatesarray ){
foreach ($updatesarray as $data){
$post_iD = $data['post_iD'];
$orimessage = $data['message'];
$message = tolink(htmlcode($data['message']));
$time = $data['created'];
$mtime = date("g:i", $time);
$username = $data['username'];
$uploads = $data['uploads'];
$uiD = $data['uid_fk'];
?>
<div class="wrap">
<div class="item" id="stbody<?php echo $post_iD;?>">
<div class="loop-post">
<div class="loop-post-content">
<div class="loop-post-image">
<a href="" class="post-link">
<?php
if ($uploads){
$s = explode(",", $uploads);
foreach ($s as $a){
$newdata = $Wall->Get_Upload_Image_Id($a);
if ($newdata) echo "<a href='uploads/" . $newdata['image_path'] . "' rel='facebox'>
<img src='uploads/" . $newdata['image_path'] . "' width='520' height='245' class='imgpreview attachment-top_story_post wp-post-image' /></a>";
}
echo "</div>";
}
?>
</a>
</div>
<div class="loop-post-byline">By <a rel="author" title="Posts by Emil Protalinski" href=""><?php echo $username;?></a>
<span class="date"> — <?php echo $mtime;?></span>
</div>
<a class="post-link">
<?php echo clear($message);?>
</a>
<div class="post_comment">
<?php $x=1; include_once 'load_comments.php'; ?>
<div class="commentupdate" id="commentbox<?php echo $post_iD;?>">
<div class="stcommentimg">
<img src="<?php echo $photo;?>" class="small_face">
</div>
<div class="stcommenttext">
<form method="POST" action="">
<textarea name="comment" class="comment" id="<?php echo $post_iD;?>" value="<?php echo $post_iD;?>"></textarea> #57
<input type="submit" value="comment">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php } } else echo '<h3 id="noupdates">No Updates!</h3>';?>
You are never actually setting the post_iD variable anywhere. If you want to use it in the $_POST array you need to set it in the form first.
<form method="POST" action="">
<input type="hidden" name="post_iD" value="<?php echo $post_iD; ?>" />
<textarea name="comment" class="comment" id="<?php echo $post_iD;?>" value="<?php echo $post_iD;?>"></textarea> #57
<input type="submit" value="comment">
</form>
You need to do this...
<textarea name="comment" class="comment" id="comment-<?php echo $post_iD;?>"></textarea>
<input type="hidden" id="post_iD" name="post_iD" value="<?php echo $post_iD;?>" />
Cuz youre never passing the post_iD anywhere....this will pass it in hidden form...
And the value for your textarea wont be $post_iD, it will likely be a comment/post of some type....I assume you just had that for debugging
Related
This is my code I want to add one input field for every image preview and save it to db.. the field is coming but I'm not getting any data.. can anyone suggest how can I post them???
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
while($row = $fetch_imgid->fetch()):
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="submit" class="del_btn" value="Delete Image" />
<input type="text" id="cap" name="cap[]" placeholder="Enter Caption Here" />
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
<?php
endWhile;
?>
</ul>
<input type="submit" value="Add Caption" name="addcap" /> <?php include('addcap.php'); ?>
and this is addcap.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
foreach($_POST['cap'])
{
$imgcap = $_POST['cap'];
if($imgcap!=empty())
{
try
{
$con=new PDO("mysql:host=localhost;dbname=newimg","root","");
$sql=$con->prepare("UPDATE attempt010 SET caption='$imgcap' WHERE id='$cap_id'");
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
?>
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
Must have unique id. You can't send multiple fields with same id. You will get only last one.
For example:
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<form action="addcap.php" method="post">
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
$id_array="";
while($row = $fetch_imgid->fetch()):
$id_array = $id_array.$row['id'].",";
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="text" id="cap_<?php echo $row['id']; ?>" placeholder="Enter Caption Here" />
<?php
endWhile;
$id_array = substr($id_array, 0, -1);
?>
<input type="hidden" id="cap_ids" value="<?php echo $id_array ; ?>" />
</ul>
<input type="submit" value="Add Caption" name="addcap" />
</form>
<!--addcap.php-->
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
if(isset($_POST['cap_ids'])){
$ids_array = explode(",", $_POST['cap_ids']);
foreach($ids_array as $ids)
{
$idcap = 'cap_'.$ids;
$imgcap = $_POST[$idcap];
if($imgcap!=empty())
{
try
{
$con=new
PDO("mysql:host=localhost;dbname=newimg","root","");
$query = "UPDATE attempt010 SET
caption='$imgcap' WHERE id='$ids'";
echo $query;
$sql=$con->prepare($query);
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
}
?>
This code looks like it can't work. Because you have submit and form handling code in same page. The idea behind form is to post data to different page(set in form action) and this page will do something with this data and display results to the user. For your example to work make form in your first file like:
<form action="addcap.php">
<inputs here>
</form>
Nowadays it is common that database operations are done asynchronic on server side, when user can continue using the page/app.
So learn how to use jQuery and AJAX. Maybe nodeJS or other new stuff.
Hello monsters of programming. I just want to ask a question about using $_SESSION and $_GET. When to use $_GET and $_SESSION? what is the best for passing variable? Im just new to php and html and i don't know what is the best practice. Can someone help me to understand both of them?
Here is the example of my code. I used $_SESSION for passing the variable $newsid;
here is the edit.php
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type='hidden' name="news_id" value="<?php echo $newsid;?>">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
header('Location: edit2.php');
}
?>
here is the edit2.php
<?php
session_start();
$id = $_SESSION['news_id'];
include_once('connection.php');
$sql = "SELECT * FROM news WHERE news_id = '$id'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $title;?>"/><br>
Date<input type ="text" name="date" value="<?php echo $date;?>" /><br>
Content<textarea name="content"><?php echo $content;?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $newsimage;?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
$_GET is for parameters that are needed during that specific request (or can be easily carried over to other pages), e.g.:
item IDs
current page (pagination)
user's profile name
...
$_SESSION is for data that needs to be persisted across multiple requests, e.g.:
current user's ID
shopping carts
list filters
...
You should use the one that better suits your use case.
That being said, I'd consider storing news_id in the session a bad thing. What if I want to edit multiple items and open multiple browser tabs? I'll end up overwriting my data. Just because you can use sessions doesn't mean you should.
this is what i want to do, i have two pages, first is the "edit.php" and second is the "edit2.php". the page "edit.php" this is the page where all the news is shown and you will select what news you will edit by clicking the "edit button" and the "edit2.php" where will i exactly edit the news. i want to pass the value of the selected news in another page. But here is the problem, when i clicked the "Sample news 1" edit button the data showing in another page is the "Sample news 2". Even when i clicked the "Sample news 2" edit button, the data is also the "Sample news 2". Can someone give me ideas on how to fix this?
here is the picture of edit.php. I click the edit button of "Sample news 1".
here is the picture of edit2.php. and this is the output data. The data should be "Sample news 1" not "Samplel news 2".
here is my php code in edit.php
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $newsid;
$_SESSION['n_title'] = $title;
$_SESSION['n_date'] = $date;
$_SESSION['n_content'] = $content;
$_SESSION['n_image'] = $newsimage;
header('Location: edit2.php');
}
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
here is my php code for "edit2.php"
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $_SESSION['n_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $_SESSION['n_date']; ?>" /><br>
Content<textarea name="content"><?php echo $_SESSION['n_content']; ?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $_SESSION['n_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Remove if(isset($_POST['esubmit'])){ code out of while loop and add values in form as I shown in below example
Pass $newsid in edit form as hidden and retrieve the content based on new
<form action="" method="post">
<input type='hidden' value="<?php echo $newsid;?>" name="news_id">
<input type="submit" name="esubmit" value="edit" />
</form>
and in your php add this line.
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
The problem is, in each iteration of while loop you're overwriting $newsid, $title, $date,... variables. So when you submit the form, the last row's data will get stored in the corresponding $_SESSION variables.
So here's the solution to your problem.
You don't need $_SESSION to pass the form values to edit2.php page, instead change the <form> element in the following way,
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
On edit2.php page, first catch the news_id value using $_GET superglobal , like this:
$newsid = $_GET['news_id'];
And then get the appropriate news details using this $newsid, and finally populate the form.
Here's the complete code,
edit.php
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
edit2.php
<?php
if(isset($_POST['esubmit'])){
$newsid = $_GET['news_id'];
include_once('connection.php');
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
/* bind parameters */
mysqli_stmt_bind_param($stmt, "s", $newsid);
/* execute query */
mysqli_stmt_execute($stmt);
/* get the result set */
$result = mysqli_stmt_get_result($stmt);
/* fetch row from the result set */
$row = mysqli_fetch_array($result);
}
}
if(isset($_POST['submit'])){
// Write code to commit the edit details
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['esubmit'])){
?>
<form method="post" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $row['news_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $row['news_date']; ?>" /><br>
Content<textarea name="content"><?php echo $row['news_content']; ?></textarea>
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
<input type="submit" name="submit" value="Update" />
</form>
<?php
}
?>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Here is the issue, You are looping your data to the session, usually single session keeps only a value. Since that final item of your loop will store on the session [in this case it is 'Sample news 2']. I believe, you can place it on a hidden field & post it to the next page or you can use URL Parameter [GET], to pass the Id to the next page.
Ex : <a href ='edit2.php?newsId ='<?php echo $newsid?>> Edit </a>
what I think is happening is when you are clicking edit button this edit.php page is again reloading and that sql query is running again that's why it is taking value of first image in both cases.what I suggest you to try is use $_get[] instead of $_SESSION and $_POST and pass value of image id directly to page edit2.php and query others details from database in that page and then display it.try it
Remove <form> and <input> use just <a> instead like this.
<a href='edit2.php?id=$news_id'>Edit</a>
and then fetch this id in edit2.php like following
$news_id=$_GET['id'];
and atlast fetch other details of news from the database using this id like'
$query='select * from news where news_id=$news_id';
Here in this code I am accepting data as an array and inserting it in the database. As I am using while loop I am having multiple input fields with same name as you can see in the HTML code below. What I want to do is, I want to add a check that if two fields are same then echo error saying two fields cannot be the same else insert the data. I could do it if there were two different input fields for the data like if($field1 == $field2){ echo "Error!"; else insert data. But here the field is only one with is giving multiple fields in a while loop. How can I add this check?
HTML form code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php while($pro = $priq->fetch()){ extract($pro); ?>
<div class="row tbpad">
<div class="col-sm-3 col-xs-6">
<p><i class="fa fa-caret-right"></i> <?php echo $bk_title; ?></p>
</div>
<div class="col-sm-1 col-xs-1">
<div class="form-group">
<input type="text" class="form-control" name="priority[]" value="<?php echo $pr_priority; ?>">
<input type="hidden" class="form-control" value="<?php echo $bk_id; ?>" name="bkid[]">
</div>
</div>
</div>
<?php } ?>
<input type="submit" class="btn btn-orange" value="Set Priority" name="priority">
</form>
PHP code:
if(isset($_POST['priority'])){
$priority = (!empty($_POST['priority']))?$_POST['priority']:null;
$bkid = (!empty($_POST['bkid']))?$_POST['bkid']:null;
$iNumSets = count($priority);
for($i = 0; $i < $iNumSets; $i++){
$str = "INSERT INTO priorities(pr_book, pr_by, pr_priority)
VALUES('$bkid[$i]', '$sess', '$priority[$i]')";
$res = $PDO->prepare($str);
$res->execute();
if($res){
echo '<script>alert("Priority added successfully!");window.location = "view-order.php";</script>';
}else{
echo '<script>alert("Server Error! Please try back later.");window.location = "add-order.php";</script>';
}
}
}
Since $priority is an array which you get from $_POST['priority'], you could do the following:
$num_original = count($priority);
$num_unique = count(array_unique($priority));
if ($num_original != $num_unique) {
echo "One or more item were idendical!";
}
Also make sure you use mysql_real_escape_string() or similar methods on your $_POST input before you pass it into the SQL query. Otherwise your code is vulnerable to SQL injections!
I'm having a problem submitting only one of the forms at my site.
The forms are being created dynamic through a foreach loop.
Every time i'm submitting one form, it submits all of them, so if there is 10 posts then they have a reply form each, and all it inserted to the database.
<div id="newsFeed">
<div class='newsBox'>
<div class="newsProfileInfo">
<div class="newsProfileImg">
<img src="<?php echo $path, $res->profileImage;?>" alt="<?php echo $name; ?>">
</div><!--newsProfileInfo ends here-->
<div class="newsName">
<p>
<?php echo $res->name." ".$res->surname;?>
</p>
</div><!--newsName-->
</div><!--newsProfileInfo ends here-->
<div class="newPost">
<form action='' method='post'>
<textarea name="newPost" placeholder="Opret nyt indlæg"></textarea>
<input type="submit" name="postSubmit" value="Opret">
</form>
</div>
<?php
$data = newsFeed($db);
foreach($data as $news){
$newsId = $news['newsId'];
$headline = $news['headline'];
$post = $news['post'];
$date = $news['postDate'];
$dates = date_create($date);
$name = $news['name'];
$surname = $news['surname'];
$profileImg = $news['profileImage'];
$path = "images/profileImages/";
?>
<div class="newsProfileInfo">
<div class="newsProfileImg">
<img src="<?php echo $path, $profileImg;?>" alt="<?php echo $name; ?>">
</div><!--newsProfileInfo ends here-->
<div class="newsName">
<p><?php echo substr($name ." ". $surname, 0,11);?></p>
</div><!--newsName-->
<p class="postDate">
<?php echo date_format($dates, 'd/m/Y') ." - ".date_format($dates,'H:i');?>
</p>
</div><!--newsProfileInfo ends here-->
<div class="newsPost">
<p>
<?php echo $post;?>
</p>
</div>
<?php
$reply = replyToPost($db,$newsId);
foreach($reply as $msg){
$replyId = $msg['replyId'];
$message = $msg['message'];
$news_fk = $msg['newsId_fk'];
$userId_fk = $msg['userId_fk'];
$date = $msg['date'];
$name = $msg['name'];
$surname = $msg['surname'];
$profileImg = $msg['profileImage'];
$path = "images/profileImages/";
?>
<div class="replysLoop">
<div class="replyImg">
<img src="<?php echo $path, $profileImg;?>" alt="<?php echo $name;?>">
</div>
<div class="replysBoxes">
<p><?php echo $message;?></p>
</div><!--replysBoxes ends here-->
</div><!--replysLoop ends here-->
<?php
}
?>
<div class="replyToMsg">
<div class="replyImg">
<img src="<?php echo $path, $res->profileImage;?>" alt="<?php echo $res->name; ?>">
</div>
<?php
if(isset($_POST['replySubmit'])){
insertReply($db,$newsId);
header('Location: index.php');
}
?>
<div class="inputHolder">
<form action="" method="post">
<input type="text" name="replys" placeholder="Besvar indlæg">
<input type="submit" name="replySubmit" value="Besvar">
</form>
</div>
</div>
</div><!--newsbox ends here-->
<?php
}//foreach loop newsfeed ends here
?>
As far as I am aware the submit button will only submit the form that it is in. In fact it is very hard to submit more than one form at a time.
I suspect the problem is that the form submits the reply and that it is not bound to a current comment and ends up getting added to all of them.
I suggest that you start with something like this:
<?php
//... something that results in this post setting `$postid`
?>
<div class="reply">
<form action="" method="post">
<input type="hidden" name="replyToPostID" value="<?php echo $postid; ?>">
<input type="text" name="replyToPost" placeholder="Besvar indlæg">
<input type="submit">
</form>
</div>
Then your PHP can make SQL that pins the comment down to one parent comment. Of course you are forcing me to guess because there is no way what you have posted is the entire code.
See also: Submit Multiple Forms With One Button