Endless Query/Loop in PHP? - php

Hello I'm querying a database of names by the first letter of the last name. However when I excute the query and print the results, the first name prints over and over again when in reality theres more than one name to print out. here is what I have so far. the data passed in is a letter in which it supppose to gather all the last names with that beginning letter. what I'm I doing wrong that can cause this infinite loop?
function displayprofs()
{
print"<div>";
print "<p><a href = '$_SERVER[PHP_SELF]'>return to start</a>\n";
$abc=($_POST['abc']);
print"$abc";
$db = adodbConnect();
$query="Select * FROM Category WHERE Description LIKE '$abc%'";
$result=$db->Execute($query);
$row=$result->FetchRow();
while($row)
{
$name= $row['Description'];
print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";
print"<input type='hidden' name='profy' value='$name'>";
print"<p>$name<input type='submit' name='add' value ='Submit'/></p>\n"; //submit button
print"</form>\n";
//break;
}
print"</div>";
}

You fetch $row once and then start while loop with its condition always true. Missing $row=$result->FetchRow(); inside the while block.

Replace while($row) with while($row=$result->FetchRow())
and remove $row=$result->FetchRow(); you've written before starting while

Since you have while($row) you will iterate over the same row over and over again. Change to while($row=$result->FetchRow()) instead and remove the previous fetch or keep as is and put $row=$result->FetchRow(); in the end of the while block.
Solution 1:
while($row=$result->FetchRow())
{
$name= $row['Description'];
print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";
print"<input type='hidden' name='profy' value='$name'>";
print"<p>$name<input type='submit' name='add' value ='Submit'/></p>\n"; //submit button
print"</form>\n";
}
Solution 2:
$row=$result->FetchRow();
while($row)
{
$name= $row['Description'];
print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";
print"<input type='hidden' name='profy' value='$name'>";
print"<p>$name<input type='submit' name='add' value ='Submit'/></p>\n"; //submit button
print"</form>\n";
$row=$result->FetchRow();
}

By now, you know the while($row) bit is probably a mistake. Just to avoid you having to post a second question in a minute: This isn't the best of ideas:
print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";
Perhaps consider writing this as either:
print "<form method='post' enctype='multipart/form-data' action='{$_SERVER['PHP_SELF']}'>\n";
or:
print "<form method='post' enctype='multipart/form-data' action='".$_SERVER[PHP_SELF]."'>\n";

Related

array of submit buttons in different forms generated with php

I dynamically create html forms with a loop in php and each submit button is assigned a name as part of an array. How can I check which submit button is set and get its value? I tried this code but it doesn't work.
<?php
if($count_eksp){for($i=0; $i<$count_eksp; $i++){
$fusha_eksp = mysql_fetch_row($query1);
echo "<br>$fusha_eksp[2] $fusha_eksp[3]<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'><input name='eksp_edit[]' type='submit' value='$fusha_eksp[0]' height='20' width='20' ><input id='eksp_fshi[]' type='image' src='fshi.png' height='20' width='20'></form>";
}}
?>
<?php
if(isset($_POST['eksp_edit[]'])){
foreach($_POST['eksp_edit'] as $id){
$query = mysql_query("DELETE FROM `fusha_ekspertizes` WHERE `id`='$id'", $db_server);
}
}
?>
You have error in if condition, in the key. Remove [] for that.
if (isset($_POST['eksp_edit'])) {
...
}
The second error is in action attribute, you can have another <? inside echo. When the form is processed on the same page, you can leave this attribute.
echo "[...]<form method='post'><input [...]";

Send column name from button

I have made a php page. On which I am displaying table 'prod' from my data base.
each row is displayed nicely. Today i tried to add a button named 'rate' at the end of each row of my table. which I did successfully. Now I want to send the the value of the first column of that row to another php page when that button is clicked. I am stuck that how to do so? can you help please ??
I know i have to use the method post in my form and i have to use $_post[that value] on the other php page to inculcate the value for further function.
I just need to ask that where to add the value of my first column in the button line. so that onclick it can send that value. I hope I am clear over this. Thank You very much for help :)
<?php
include("connection.php");
$query = "select * from prod";
$res = oci_parse($conn,$query);
usleep(100);
if (oci_execute($res)){
usleep(100);
print "<TABLE border \"1\">";
$first = 0;
while ($row = #oci_fetch_assoc($res)){
if (!$first){
$first = 1;
print "<TR><TH>";
print implode("</TH><TH>",array_keys($row));
print "</TH></TR>\n";
}
print "<TR><TD>";
print #implode("</TD><TD>",array_values($row));
print "</TD></TR>\n";
echo "<td><form action='detailform.php' method='POST'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
}
print "</TABLE>";
}
?>
you have to add inputs in your form whatever kind u prefer
echo "<td>
<form action='detailform.php' method='POST'>
<input type='hidden' name='your_val_key' value='".$row[your_val_key_in_query]."'> <!-- input hidden, change to text 4 debug -->
<input type='submit' name='submit-btn' value='Rate'/>
</form>
</td></tr>";
and than, in your detailform.php u can get the val with
echo $_POST["your_val_key"];
if u are not sure how much data u send or somthing, try this and u get the full data:
echo "<pre>".print_r($_POST,true)."</pre>";
BTW: why are u mixing print and echo?
Use hidden input
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='col-name' value='you-col-value'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";

Inserting Image to MySQL Database

I am working on a solution that will help me submit specific images from a list of images to a MySQL Database.
My database consists of the following:
Database
id(INT)
photo(BLOB)
caption(VAR)
Code
I am first retreiving images from a list and giving them each a submit button.
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
$pictureImage parses the data URL and then puts it into an actual image.
The submit button is below each of those images.
I am then making it so that when the submit button is pressed, it is added to the database.
if(isset($_POST['submit'])) {
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}
Problem
I am running into an issue where the last image in my list is the one being submitted to the database, rather than the image with the corresponding submit button. How do I make it so that it is grabbing the photo with the corresponding submit button?
Any help would be appreciated greatly.
You need to include any identifier to the image inside the form in order to store it.
For example you can try building the forms like this:
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='hidden' name='imageid' value='{$data->images->thumbnail->url}'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
And when you want to store the image on the form submit you can actually pick up the identifier of the image (in this case I used the URL you posted):
if(isset($_POST['submit'])) {
$sql="INSERT INTO $usertable (image) VALUES ('$_POST[imageid]')";
}
You are not posting any data when you click on your submit buttons.. i would suggest that for each form you would have a hidden field with the url of the image.
something like this:
<form action='tag.php' method='post'>
<input type="hidden" value="{$data->images->thumbnail->url}" name="pic"/>
<input type='submit' name='submit' value='Click Me'>
</form>
Problem
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
By the time this loop is done, you will have the last image in the loop as the value for $pictureImage.
So when it gets to this point, you're $pictureImage is still... the last image.
if(isset($_POST['submit'])) {
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}
Solution
I'm not exactly sure what data value you want to save because right now it looks like the whole tag. But whatever it is, you need to put it into a form field first ...
foreach ($media->data as $data) {
..
echo "<form action='tag.php' method='post'>";
echo "<input type='hidden' name='imageurl' value='<img src=\"{$data->images->thumbnail->url}\">' />";
..
}
and then retrieve it in the POST part of your script:
if(isset($_POST['submit'])) {
$imageurl = $_POST['imageurl'];
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$imageUrl')";
}
This way, it won't always be the last value in your list, rather, it will be the one you selected.

Delete a row(s) from MySQL using an HTML Form

I am having difficulty deleting rows from my database.
I have a delete button in a form that when it is clicked then performs a DELETE FROM query but it doesnt work and Im wondering whether my theory is completly wrong (the theory being that having a form and submit button to INSERT data into a database works so why not use that to delete stuff? This is the code
$league_id = $_GET['id'];
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
if ($_POST['ooops']) { //if the data is rubbish then delete and start again...
$delete_lge_sql = "DELETE FROM st_position WHERE league_id = '$league_id'";
$delete_lge_res = mysqli_query($statto, $delete_lge_sql)
or die(mysqli_error($statto));
}
When I click Delete Entries the page reloads and the URL looks like this
page.php?ooops=Delete+Entries
Many thanks for any help
You missed a double-quote in this statement
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
Change it to
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\" method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
Try :
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"hidden\" name=\"ooops\" value=\"1\" />
<input type=\"submit\" value=\"Delete Entries\">
</form>";
This way, your $_POST['ooops'] variable will be equal to "1". Just make your test on this value and it will be ok.

Passing data between PHP webpages from a dynamically generated list

I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}

Categories