i have made i small membersearch for my site that looks for first name oder second name in my database. all members are inserted by the same way but i get not always a result back. if no member with a typed name is found than i get a message. that seams to work, but some times i get no message or if there are more than one results i get not all of them.
here is my code i use.
<form id="tfnewsearch" method="POST" action="admin_search_member.php">
<input id="tfq" class="tftextinput4" name="q" size="21" maxlength="240" value="Mitglied suchen..." />
<input type="submit" name="startsuche" value=" " id="tfbutton4">
</form>
and that's my query
if(isset($_POST['startsuche'])) {
$sql = "SELECT * FROM Mitglieder WHERE vorname LIKE '%".mysql_real_escape_string(trim($_POST['q']))."%' OR nachname LIKE '%".mysql_real_escape_string(trim($_POST['q']))."%'";
$result = mysql_query($sql) OR die("<pre>\n".$sql." </pre>\n".mysql_error());
$row = mysql_fetch_array($result);
}
if (mysql_num_rows($result) == 0) {
?>
<div id="body_box_tabs">
<div class="tabcontents">
<div id="view1">
<p style="color: #003137; font-weight: bold;">Das gesuchte Mitglied existiert nicht!</p>
<input class="button-link" type="submit" value="Zurück"/>
</div>
</div>
</div>
<?php
} else {
echo $_POST['q'];
?>
<div id="body_box_tabs">
<div class="tabcontents">
<div id="view1">
<?php
while($row = mysql_fetch_array($result)) {
echo "show me the found member";
}
}
?>
I hope some one can help me with this.
You're basically fetching and then discarding the first row of the query result. This will cause the "does not exist" message to be shown when there are no rows, will cause no output when there's one row returned, and will cause (n-1) outputs when n>1 rows are found. You will never see the first row of the result this way.
Either remove the first $row = mysql_fetch_array($result);, or change your loop:
do {
echo "show me the found member";
} while($row = mysql_fetch_array($result));
In this code block:
<?php
while($row = mysql_fetch_array($result)) {
echo "show me the found member";
}
}
?>
Do this:
<?php
while($row = mysql_fetch_array($result)) {
echo $row['field_name'];
}
}
?>
That should do the trick...
Related
I'm creating a comment system in my project. I want each posted question to have a comment. I was able to post the comment, but I am having a problem displaying all comment to it's respective answers. I was able to display only one row but not the row of the comment. I try to use a while loop nested inside the while that echo's each question, but it hangs. When I use if it only displays the first row of the comment of each question.
So my question is how can I display them all?
<div class="answer">
<?php
include 'db.php';
$sql = "select * from answers where questionrid IN(select id from question where id='$qid')";
$result = mysqli_query($con,$sql);
if($result){
while($ro = mysqli_fetch_assoc($result)){
?>
<div class="a_view">
<pre>
<?php
echo $ro["answer"];
?>
</pre>
</div>
<div class="ans_comment">
<?php
if($ro["id"]){
$id = $ro["id"];
$sqli = "SELECT * FROM comments WHERE answerid='$id'";
$query = mysqli_query($con,$sqli);
$row = mysqli_fetch_assoc($query);
$num = mysqli_num_rows($query);
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
}
?>
</div>
<div class="add"><div class="coment">add a comment</div> <div id="coment">
<form class="cform" method="post" action="acomment.php">
<textarea type="text" name="comment" class="tcomment" placeholder="add your comment here,your is
required to give correction or more information about the problem"></textarea><br><br>
<input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid">
<input type="submit" value="Post your comment">
</form>
</div></div>
<?php
}
}else{
echo "no record";
}
?>
<?php
$con->close();
?>
This is the section that made it hang
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
when I use if, it only echos one row.
Instead of
while($row){
do it just like you're doing in the while loop above
while($row = mysqli_fetch_assoc($query)){
The way you have it now, $row is never changing, and therefore always evaluates to true, leaving you stuck in your loop.
<!DOCTYPE html>
<html>
<head>
<title>Talenquiz2</title>
</head>
<body>
<?php
if (isset($_GET["controleer"]))
{
$vraag = $_GET["vraag"];
$juistantwoord = $_GET["juistantwoord"];
$foutantwoord1 = $_GET["foutantwoord1"];
$foutantwoord2 = $_GET["foutantwoord2"];
$con = mysql_connect("localhost","root","");
mysql_select_db("dbproject", $con);
$result = mysql_query("SELECT * FROM tblquizvragen");
while($row = mysql_fetch_array($result))
{
if ($row['vraag'] == $vraag)
{
if ($row['juistantwoord'] == $juistantwoord)
{
echo "Juist!<br />";
}
else
{
echo "Fout!<br />";
}
}
}
mysql_close($con);
echo "\n<hr />\n";
}
$aantalvragen=1;
$con = mysql_connect("localhost","root","");
mysql_select_db("dbproject", $con);
$result = mysql_query("SELECT * FROM tblquizvragen WHERE id='". $aantalvragen . "';");
$row = mysql_fetch_array($result);
The program is a quiz, it asks 5 questions with 3 chckbox 1 is corect and 2 are incorrect.
for ($aantalvragen=1; $aantalvragen<=5; $aantalvragen++)
{
$row = mysql_fetch_array($result);
}
her i lin
$vraag = $row['vraag'];
$juistantwoord = $row['juistantwoord'];
$foutantwoord1 = $row['foutantwoord1'];
$foutantwoord2 = $row['foutantwoord2'];
mysql_close($con);
?>
<form>
It doensn't show the values of the rows in my browser it shows only an open text and an open checkbox.
<input type="text" name="vraag" value="<?php echo $vraag; ?>" /><br />
<input type="checkbox" name="juistantwoord" value="<?php echo $juistantwoord; ?>" /><br />
<input type="checkbox" name="foutantwoord1" value="<?php echo $foutantwoord1; ?>" /><br />
<input type="checkbox" name="foutantwoord2" value="<?php echo $foutantwoord2; ?>" /><br />
<input type="submit" value="Controleer je antwoord" name="controleer" />
</form>
</body>
</html>
Your code is incorrect for fetching from the db
for(...) {
$row = mysql_fetch_array(...);
}
You simply loop over 5 lines of results, regardless of how many there may be, and assign the row array to $row... but do so for EVERY row without ever using them. So you end up trashing the first n-1 rows and come out of the loop with only row n saved.
If you're wrong with how many rows of data you're expecting, your 5-item loop may have only a 4-item result set to deal with, and the final row $row1 value will be the boolean FALSE that msyql_fetch returns when there's no more data.
Try something like this instead:
while($row = mysql_fetch_assoc($result)) {
echo ..... your stuff here ...
}
Far more reliable, doesn't depend on there being a known number of rows available, and will not output anything if there's no data at all.
So I have a form that users fill out with some radio buttons. The values from the radio buttons get passed to MySQL. I now want to pull those values from the database, display them in a table on a different page, and apply different styles to them with span tags.
Here's the code from the form:
<input class="radio_style" type="radio" name="job_type" value="fulltime"/>Full-time<br/>
<input class="radio_style" type="radio" name="job_type" value="parttime"/>Part-time<br />
Here's the code for the page where I want to display it:
<div class='job_type_div'>
<?php if($job_type=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php if($job_type=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
<?php } ?>
</div>
So ideally, the "fulltime" value will have one style and the "parttime" value will have another style. But when I try running this code, nothing happens. I'm definitely connecting to the database correctly. And the row name is properly labelled "job_type". Any ideas on where I might be going wrong? Any help would be greatly appreciated :)
First of all, your form should be something like so:
<form action="page_you_want_to_display.php" method="POST">
<label for="type">Job Type:</label>
<label for="fulltime">
<input class="radio_style" id="fulltime" name="job_type" type="radio" value="fulltime">
Fulltime
</label>
<label for="parttime">
<input class="radio_style" id="parttime" name="job_type" type="radio" value="parttime">
Part Time
</label>
<input name="submitted" type="submit" value="Submit">
</form>
The page you want to display on should look something like this:
if(isset($_POST["submitted"])){
$job_type = $_POST['job_type'];
echo '<div class="job_type_div">';
if($job_type=='fulltime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='fulltime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="fulltime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
} elseif ($job_type=='parttime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='parttime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="parttime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
}
echo '</div>';
}
and CSS:
.fulltime {
margin:0px;
padding:5px;
width:300px;
background:#9C0;
color:#fff;
}
.parttime {
margin:0px;
padding:5px;
width:300px;
background:#069;
color:#fff;
}
Tested:
Hope this helps
may be problem in your php. Is there some logic?
$job_type=null;
if($job_type=='fulltime'){
...
if($job_type=='parttime'){
...
}
}
did you set $job_type variable? May be you need something like this:
<div class='job_type_div'>
<?php if($row['job_type']=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php } elseif($row['job_type']=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
</div>
I don't believe that the conditions will work the way you implemented it, try doing it like this:
<?php
echo "<div class='job_type_div'>";
if($job_type=='fulltime') {
echo "<span class='job_type_style'>"
//etc...
While you fetching your array from Data Base you need to use MYSQL_BOTH, to fetch columns by Name.
mysql_fetch_array($res, MYSQL_BOTH)
So you should have something like this:
$job_type_query = "SELECT * FROM `job`; ";
$res = mysql_query($job_type_query) or die(mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_BOTH))
{
echo $row['job_type'];
}
form.php
if you initially set one 'selected' in your form, dont need to check if its set, simple set db like so:
...
mysql_query("UPDATE X SET (job_type='{$_GET['job_type']}') WHERE Y");
...
display.php
As you will probably be making a stylesheet, reference the selectors with the job_type labels, which you put in your database
while($row = mysql_fetch_assoc($resultsource))
echo "<div class='job_type_div'> <span class='{$row['job_type']}_style'>{$row['job_type']}</span>";
This is a simple question I believe, but can't figure it out yet.
I have a text area that after submit goes to a database, and then I echo this text on a page, but here is the problem, say the person writes on the textarea:
Hi Robert,This is just a test!.
Jason.
And the message goes to the database just like that, but when I echo that, I get:
Hi Robert, This is just a test!.
Jason.
This is the form:
<textarea name="newMessage" wrap="hard" cols="30" rows="3"></textarea>
<input type="submit" name="submit" value="Ingresar"> </>
This is the code I use to display the text:
<?php
while($row = mysql_fetch_assoc($messages)){
echo $row['mensaje']."<br/>";
}
?>
This is what I use to insert the code:
if(isset($_POST['submit'])){
$check4LB = $_POST['newMessage'];
while($letter = mysql_fetch_assoc($check4LB)){
if($letter=' '){
$letter='<br/>';
}
} /////I know this is not write bu is the idea i thgouht at least
$query = mysql_query("SELECT (ifnull(max(idRegistro),0) + 1) as id FROM messages");
$row = mysql_fetch_array($query);
$idMax = $row['id'];
$insertMessage = "INSERT INTO messages(idRegistro, mensaje) VALUES ('".$idMax."','".$letter."')";
mysql_query($insertMessage) or die(mysql_error());
echo "<meta http-equiv=Refresh content=\"0 ; url=".$_SERVER['PHP_SELF']."\">";
}
try echo nl2br($row['mensaje']);
Use nl2br() on the output from the database.
Try this
<?php
while($row = mysql_fetch_assoc($messages)){
echo str_replace("\r",'<br/>',$row['mensaje']);
}
?>
i'm a bit of PHP noob, so sorry if this is a daft question, but I just can't figure this out by myself so any help would be greatly appreciated!
I am trying to create a modify page for an events web application. It seems to be working except when I try to validate the final if/else statement.
This statement returns the value of $row[0] and checks if its == NULL. If NULL, it should return an echo 'this event does not exist' to the user, if there is a value, it presents the user with a matrix of text boxes that they can change the data in.
Currently it works fine for the else statement when there is data found, but doesnt recognise the original if when there is no data. Coupled with that, the footer at the bottom of the page disappears!
Here is the main body of the code, i have highlighted the problem area. I understand that there is probably a more effective and efficient way of doing it all, but please keep it simple as I'm still learning. Thanks again. Dan
<div class="round">
<div id="main" class="round">
<span class="bold">MODIFY EVENT</span><br /><br />
On this page you can modify or delete the events that are stored on the database.<br />
Be aware that you cannot undo the DELETE function.<br />
<br />
Find Event:<br />
<table>
<form name="form1" id="form1" method="post" action="
<?php echo $_SERVER["PHP_SELF"]; ?>" >
<tr><th>By Date:</td><td><input type="text" name="modifyDate"
id="modifyDate" value="dd/mm/yy" /></td></tr>
<tr><th>By Name:</td><td><input type="text" name="modifyName" id="modifyName"
value="" /></td></tr>
<tr><th>Find All:</th><td><input type="checkbox" name="modifyAll"
id="modifyAll" /><td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Search" /></td></tr>
</form>
</table>
<?PHP
if(!isset($_POST['modify'])){
if(isset($_POST['submit'])){
$moddate = $_POST['modifyDate'];
If($moddate == "dd/mm/yy"){
$date = "";
}
else{
$newDate = str_replace("/",".",$moddate);
$wholeDate = $newDate;
$dateArray = explode('.', $wholeDate);
$date = mktime(0,0,0,$dateArray[1],$dateArray[0],$dateArray[2]);
}
$name = $_POST['modifyName'];
$all = $_POST['modifyAll'];
$host = "localhost";
$user = "user";
$password = "password";
$db = "database";
$con = mysql_connect($host,$user,$password) or die('Could not connect to Server');
$dbc = mysql_select_db($db, $con) or die('Could not connect to Database');
if($all != 'on'){
$q = "SELECT * FROM events WHERE date = '$date' || title = '$name' ";
}
else{
$q = "SELECT * FROM events";
}
$result = mysql_query($q);
$row = mysql_fetch_array($result) or die(mysql_error());
//THIS IS THE PROBLEM HERE!!!!
if($row[0]==NULL){
echo 'This event does not exist';
}
else{
?>
<form name="form1" id="form1" method="post" action="
<?phpecho $_SERVER['PHP_SELF']; ?>" >
<?PHP
$result = mysql_query($q) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
$initialDate = date('d/m/y', $row['date']);
$ID = $row['ID'];
echo '<input type="text" name="inputEmail" id="inputEmail" value="'.$initialDate.'" />';
echo '<input type="checkbox" value=$ID name="toModify[]" style = "visibility: hidden;" /><br /><br />';
}
echo '<input type="submit" name="modify" value="Modify" />
<br /><br />';
}
}
}
else{
//modify database and return echo to user
}
?>
</div>
<div id="clear"></div>
</div>
</div>
</div>
<div id="footer" class="round shadow"></div>
</body>
</html>
mysql_fetch_array($result) returns false if there are no rows, so it's possible that even if there is nothing in the result, it's still returning a false and your if($row[0] == null) is evaluating to false, also.
In other words, you should be doing a more robust test on the return results from your query to catch fringe cases.
As others have mentioned or implied, here are some things you could / should be doing:
test for rows returned, not values in rows
test for existence of variable, not content of variable
check the database for errors returned
Is the field in the table it's pulling $row[0] from set to NOT NULL? If it is, it will never be a null value. Try instead something like (if empty($row[0]))
You could check the number of result rows to see if there are any events:
$result = mysql_query($q);
$numrows = mysql_num_rows ($result);
if($numrows === 0){
...