mySQL query returns only one result - mysql

I am trying to get each row in a table to appear as part of a survey. The following code is returning only the first row in the table (so users can see only one question). I've been over and over this and can't see what I'm doing wrong. Would much appreciate any input.
Thank you!
function getQuestions ($dbc) <!--$dbc=database connection--> {
$query = "SELECT * FROM survey_questions" <!--survey_questions=table--> ;
$result = #mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC) ) {
$body = $row ['question_body'] <!--question_body=row in table--> ;
echo '
<div class="entry"> <!--user entry form-->
<h3 class="qTitle">'.$body.'</h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
';
}
}

First of all you don't need to echo so much using php...
You have to use // or /* */ to comment in PHP and not <!----> cuz that's for HTML
Secondly coming to your code..
Why you are using?
function getQuestions($dbc) //I dont know what this is doing here, why you are wrapping your code in a function???
you can simply write like this (use echo to print out your question):
<?php
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
?>
<div class="entry"> <!--user entry form-->
<h3 class="qTitle"><?php echo $row['whatever']; ?></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
<?php
}
?>

The results are now two entry forms (an improvement) but still not displaying the text of the two rows from the database. Instead of the row text, I get the > character where the text should be. Here is the updated code, adapting the suggestion of #Mr. Alien:
function getQuestions($dbc) {
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
echo '
<div class="entry">
<h3 class="qTitle">'. $row['survey_questions'].'></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" >
<input type="submit" value="Submit" name="submit" >
<input type="hidden" name="questionid" value="questionid" >
<input type="hidden" name="submitted" value="1" >
</form>
</div>
';
}
}

Related

trouble appending/combining two variables

Here's my code, It's a forum, there are posts with dislike buttons,like buttons, and a text box for commenting with each individual post, check the screenshot below. I'm trying to put the $comments variable, which are the comments itself, at the end of $posts so it displays below the posts properly like the like buttons and comment box are displayed.
I'm trying to just get the comments displayed underneath the comment box. If I call displayComments(the function in $comments definition) in the for each loop then the comments get displayed separately from the posts I've tried combining the $comments at the end of $posts with this operator +, and I've tried making a new variable equal to the both of them. Here's the code(not the full file).
<?php
include("connect.php");
include("check.php");
include("Trying.php");
include("Comment.php");
if(isset($_POST['username'])){
if (isset($_POST['post'])) {
if ($_FILES['postimg']['size'] == 0)
{
$postbody = $_POST['postbody'];
$loggedInUserId = check::isLoggedIn();
if (strlen($postbody) > 160 || strlen($postbody) < 1)
{
die('Incorrect length!');
}
connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(),\'\',0,0)', array(':postbody'=>$postbody));
}
}
if (isset($_POST['comment'])) {
$postid = $_GET['postid'];
$commentbody = $_POST['commentbody'];
if (strlen($commentbody) > 160 || strlen($commentbody) < 1) {
die('Incorrect length!');
}
connect::query('INSERT INTO comments VALUES (null,:comment, NOW(), :postid)',array(':comment'=>$commentbody,':postid'=>$postid));
//}
}
$dbposts = connect::query('SELECT * FROM dry_posts ORDER BY id DESC');
$posts = "";
$comments = Comment::displayComments($p['id']);
foreach($dbposts as $p){
if (!connect::query('SELECT post_id FROM dry_likes WHERE post_id=:postid', array(':postid'=>$p['id']))) {
$posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])."
<form action='try.php?postid=".$p['id']."' method='post'>
<input type='submit' name='like' value='Like'>
<span>".$p['likes']." likes</span>
<input type='submit' name='dislike' value='Dislike'>
<span>".$p['dislikes']." dislikes</span>
</form>
<hr /></br />
;
<form action='try.php?postid=".$p['id']."' method='post'>
<textarea name='commentbody' rows='3' cols='50'></textarea>
<input type='submit' name='comment' value='Comment'>
</form>
<hr /></br />
";
//Comment::displayComments($p['id']);
}
else{
$posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])."
<form action='try.php?postid=".$p['id']."' method='post'>
<input type='submit' name='like' value='Like'>
<span>".$p['likes']." likes</span>
<input type='submit' name='dislike' value='Dislike'>
<span>".$p['dislikes']." dislikes</span>
</form>
<hr /></br />
<form action='try.php?postid=".$p['id']."' method='post'>
<textarea name='commentbody' rows='3' cols='50'></textarea>
<input type='submit' name='comment' value='Comment'>
</form>
<hr /></br />
";
//Comment::displayComments($p['id']);
}
}
?>
<form action='try.php' class = "forum" method="post" enctype="multipart/form-data">
<textarea name="postbody" rows="4" cols="60" class = "text"></textarea>
<br />Upload an image:
<input type="file" name="postimg">
<input type="submit" name="post" value="Post">
</form>
<div class="posts">
<?php echo $posts;
?>
</div>
Here's the function I'm calling in the $comments variable
public static function displayComments($postid)
{
$comments = connect::query('SELECT * FROM comments WHERE post_id=:postid',array(':postid'=>$postid));
foreach($comments as $comment)
{
echo $comment['comment']."<hr />";
}
}
I know this won't help a lot of people but I'm just not sure what to do, How do you suggest I get everything to display inline, should rearrange my code?

How to GET data but preserve link array

Let's say I have index.php?couple=old and I want to GET new data (search all couples that are "old" but get new data) for example
<form action="index.php?couple=old" method="get">
<input type="text" name="search" >
<input type="submit" name="search_btn" />
</form>
And someone enters keyword "Smith" it should be index.php?couple=old&search=Smith
couple must be a variable to pass, otherwise PHP does not store it as part of $_GET.
You need:
<form action="index.php" method="get">
<input type="hidden" name="couple" value="old" />
<input type="text" name="search" />
<input type="submit" name="search_btn" />
</form>
maybe little bit more better solution (if you have couple of parameters you want to use).
<form action="index.php" method="get">
<?php foreach($_GET as $name=>$value):?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>" />
<?php endforeach;?>
<input type="text" name="search" />
<input type="submit" name="search_btn" />
</form>

Joomla 1.5 Database Query example needed

I have made a query, using mysql_* way, that select's some values from a table, where row's id is 1. Then I put all those values in variables so I can echo them in to my form. How can I do the same thing using Joomla's database query way ?
Here is an example of my code that working, using mysql_*:
<?php // DATABASE QUERY
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$result=mysql_query($query);
// DATABASE VARIABLES
$countdown_module = mysql_result($result,$i,"countdown_module"); ?>
$hometeam_header = mysql_result($result,$i,"hometeam_header"); ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>
OK I found it!!! Here is an example...
<?php // DATABASE QUERY
$db = JFactory::getDBO();
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$db->setQuery($query);
$rows = $db->loadObjectList();
$itemrow = $rows[0];
// DATABASE VARIABLES
$countdown_module = $itemrow->countdown_module;
$hometeam_header = $itemrow->hometeam_header; ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>

mysql 404 error

have the following code:
<?php
if (isset($_POST['submitted'])) {
$reqwidth = $_POST['reqwidth'];
$reqside = $_POST['reqside'];
$reqrad = $_POST['reqrad'];
$sqlinsert = "INSERT INTO tirelist (width, sidewall, radial) VALUES ('$reqwidth','$reqside', '$reqrad')";
if (!mysqli_query($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "1 record added to database";
}
?>
<html>
<head>
<title>Request Tire Size</title>
</head>
<body>
<h1>Request Tire Size</h1>
<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>Request Tire</legend>
<label>Tire Width: <input type="text" name="reqwidth" /></label>
<label>Tire Sidewall: <input type="text" name="reqside" /></label>
<label>Tire Radial: <input type="text" name="reqrad" /></label>
</fieldset>
<br />
<input type="submit" value="Send Order Request" />
</form>
when i click send order request, I get a 404 error.
i noticed the page is called ~http://localhost/index.php?p=test2~, but when i click it redirects me to ~http://localhost/insert-data.php~
been trying for hours, wondering what i could do to fix it
Change
<form method="post" action="insert-data.php">
to have index.php?p=test2 instead.
In your form, you reference insert-data.php. Does that exist?
If you're trying to submit the form to the same file, you can try to use this:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Try this instead. form method="post" action="/insert-data.php"

rest in the same page PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I have a database and when I sumit info, i get this error:
"Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php:2) in /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php on line 14"
I would like rest in the same page When I insert values to database, this my code:
VER.PHP
<form action="noticias/publicacomentario.php" method="post" id="commentform" onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<fieldset>
<input type="hidden" name="noticia_id" value="<?php echo $id; ?>"><br>
<p><label>NOMBRE *</label>
<input type="text" name="usuario"></p>
<p><label for="email">EMAIL (No se publicará) *</label>
<input type="text" name="email"></p>
<p><label for="comment">COMENTARIO</label>
<textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>
PUBLICARCOMENTARIO.PHP
<?php
require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario'];
$email=$_POST['email'];
$comentario=$_POST['comentario'];
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());
$query = "UPDATE noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
header("Location: ver.php?id=$id")
?>
Any idea? thx!
It says line 2, but there's nothing actually on line 2 of PUBLICARCOMENTARIO.PHP.
Ensure that you haven't got any spaces or new lines above "PUBLICARCOMENTARIO.PHP" before the PHP opening tag.
If PUBLICARCOMENTARIO.PHP only contains PHP (No HTML), then you shouldn't require the closing ?> either.
Basically the error is saying that the header("xxx") can't be output as you've output something else already. Usually it's some HTML or some space before the opening PHP tag.
you should remove blank lines before "<?php" in PUBLICARCOMENTARIO.PHP
Check if PUBLICARCOMENTARIO.PHP is saved as UTF8+BOM and if yes, remove the BOM.
You can do this with Notepad++ or any other good editor.
I put the PUBLICARCOMENTARIO.PHP into VER.PHP
<form action="" method="post" id="commentform" onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<fieldset>
<input type="hidden" name="noticia_id" value="<?php echo $id; ?>"><br>
<p><label>NOMBRE *</label>
<input type="text" name="usuario"></p>
<p><label for="email">EMAIL (No se publicará) *</label>
<input type="text" name="email"></p>
<p><label for="comment">COMENTARIO</label>
<textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>
</div>
<?php
if ($_POST) {
require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario'];
$email=$_POST['email'];
$comentario=$_POST['comentario'];
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());
$query = "UPDATE noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
}
header("Location: $_SERVER[PHP_SELF]?id=$id");
?>
Inside the HEADER("location: ....) i put the $_SERVER[PHP_SELF].
With this works.. but the values ​​entered are not displayed until you refresh the page.. why??