Data is not showing after form submited using POST [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a form using a POST method, upon enter a name and clicking search, it is supposed to display the information from the database, with the term you used.
index.php
<form action="search.php" method="POST">
<input type="text" name="search" /><br/><br/>
<input type="Submit" value="Search" />
</form>
Search.php
<?php
if (!$_POST) {
include('index.php');
} else {
?>
<h1>Server name<br />Official ItemDB!</h1>
<br />
<?php
$search = $_POST["search"];
MySQL_connect("localhost", "pernix_items", "#");
MySQL_select_db("pernix_items");
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'");
while($row = mysql_fetch_array($result));
{
?>
<table border="1">
<?
echo '<tr>';
echo '<td>'.$row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['desc'] . '</td>';
echo '</tr>';
}
}
?>
</tr>
</table>
I added the mysql_error(); function to dertime where I was going wrong, to this
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'" or die(mysql_error()));
and gives me this
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/pernix/public_html/tools/item-list/search.php on line 21
My line 21 of search.php
while($row = mysql_fetch_array($result));
So I added another or die print error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
Any ideas?

Although resolved in the comments, reposting it here as a whole answer.
First the or die() line is wrong, as the die statement is executed inside the mysql_query and not as supposed to when and if the query fails, as it should be:
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'") or die(mysql_error());
Secondly you have an extra semicolon right after the while() line making, which should be removed to look like:
while($row = mysql_fetch_array($result))
{}
Last and most importantly you should really convert this code to mysqli or pdo as the mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future.

Related

PHP query not showing all results in html table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
So I have the following query to show all users from my database:
<ul class="names">
<table>
<tr>
<th>Navn</th>
<th>Email</th>
<th>Score</th>
</tr>
<?php
$connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
mysql_select_db('users');
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$row=mysql_fetch_array($result);
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>"; //$row['index'] the index here is a field name
}
mysql_close(); //Make sure to close out the database connection
?>
</table>
</ul>
But for some reason it doesnt show all the data.
In only shows the user with iUserId 2 and not one.
Does anyone have an idea of what might be wrong?
I can log in with iUserId 1's credentials, and it shows fine the info on the login page.
But not here :S
Remove line $row = mysql_fetch_array($result);
Because this line starts to fetching records from your query results. First fetched record is record with id 1 and you do nothing with it.
Then you start echoing other records, but record with id 1 is already skipped.
You fetch the first row before your while loop is defined; once you enter the while loop it fetches the next row, which is the second. Remove the first mysql_fetch_array($result) operation.
Incidentally, also, the original mysql api in PHP is deprecated, it is recommended to use mysqli instead.
I think you should put it this way though
<?php
$connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
mysql_select_db('users');
let all the connection be outside the "ul" tag.
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$row=mysql_fetch_array($result);
?>
<ul class="names">
<table>
<tr>
<th>Navn</th>
<th>Email</th>
<th>Score</th>
</tr>
<?php
while($row){ //Creates a loop to loop through results
echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>"; //$row['index'] the index here is a field name
}
mysql_close(); //Make sure to close out the database connection
?>
</table>
</ul>

php page not showing anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
im trying to create a drop down many and populate the options from fields in a database that i have.
So far i have got the following code:
<?php
session_start();
include_once("config.php");
$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value=$row[Category]>$row[Category]</option>";
}
echo "</select>";
?>
</div>
</body>
</html>
However when i try to view the page nothing seems to happen.
My page remains blank and all i see is the color that i gave to the body in the css file.
Was wondering if anyone knew wh this was happening and if they can sort it out?
Thanks!
Assuming you are getting results from your query. Can find out by print_r($result). Think you are getting death screen because of quotes and missing opening for first option in dropdown.
echo '<select name="dropdown" value=""><option value="">Dropdown</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['Category'] . '">' . $row['Category'] . '"</option>';
}
echo "</select>";

How to fetch MySQL data in PHP table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to fetch some records from MySQL view table in my web page by PHP. I am using this following code:
$result="SELECT method,test_name,test_code FROM vtest_ord ORDER BY
test_ord_id DESC";
echo"<table border='1'><thead><tr><th>Method</th><th>Test Name</th><th>Test
Code</th></tr></thead>";
while ($row=$mysql_fetch_assoc($result)) {
echo "<tr><td>";
echo "<td>" . $row['method'] . "</td>";
echo "<td>" . $row['test_name'] . "</td>";
echo "<td>" .
$row['test_code'] . "</td>";
echo "</tr></td>"; }
echo '</table>';
but I am getting this error:
Fatal error: Function name must be a string in
C:\xampp\htdocs\test2\ord_view.php on line 211 Method Test Name Test
Code (table header shows)
You have some problem with the PHP syntax.
mysql_fetch_assoc is a function and you called it like a variable (which means nothing here).
You try to call this function with a string, but you need a ressource, obtain via mysql_query.
You don't need to write multiple echo statement, thanks to the concatenation syntax.
A potential correct code is:
$sql="SELECT method, test_name, test_code FROM vtest_ord ORDER BY test_ord_id DESC";
echo "[Debug] MethodTest NameTest Code";
$result = mysql_query($sql) or exit(mysql_error()); // Display SQL error if raised
while ($row=mysql_fetch_assoc($result)) {
echo $row['method'] . " " . $row['test_name'] . " " . $row['test_code']; echo "";
}
I also advise you to drop mysql_* and use PHP PDO, since the first one is deprecated for years now.
You need basic php logic. you have some serious errors in your code. please try to read basic of programming.
some errors:
1) $mysql_fetch_assoc is not a variable it is a function mysql_fetch_assoc(); you can not use $ in this name.
2) you need to execute query first then you can fetch.
$link = mysql_connect('host_name','db_user_name','db_password'); //fill correct credentials
mysql_select_db('your_db_name'); //set your database name
$sql="SELECT method,test_name,test_code FROM vtest_ord ORDER BY
test_ord_id DESC";
echo"<table border='1'><thead><tr><th>Method</th><th>Test Name</th><th>Test
Code</th></tr></thead>";
$result = mysql_query($sql); // execute query
while ($row=$mysql_fetch_assoc($result)) {
echo "<tr><td>";
echo "<td>" . $row['method'] . "</td>";
echo "<td>" . $row['test_name'] . "</td>";
echo "<td>" .
$row['test_code'] . "</td>";
echo "</tr></td>"; }
echo '</table>';
You forgot to call mysql_query method.
Then you could use the returned resource with the mysql_fetch_assoc function in which you have a typo. You're treating it as a variable and not a function.
Try this, You need execute the query using mysql_query, Also, mysql_fetch_assoc not a $mysql_fetch_assoc function
$query ="SELECT method,test_name,test_code FROM vtest_ord ORDER BY test_ord_id DESC";
$result = mysql_query($query) or die(mysql_error());
Should be
while ($row=mysql_fetch_assoc($result)) {
instead of
while ($row=$mysql_fetch_assoc($result)) {
Note: Use mysqli_* functions or PDO instead of using mysql_* functions(deprecated)
Here you can find good tutorial for connecting mysql database: http://us2.php.net/manual/en/mysqli.query.php

Make Query Data Become Link [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
As mentioned in the title, I would like to know how can I make the queried data become a link to another php file. For example, referring to the pic attached below, I would like to show all the cities of United States by just simply click on it and same goes with United Kingdom. I want to make like when I click on any of the state, the php file link to it will automatically know which state I've selected and generate its corresponding cities.
The following is my code:
<html>
<head><title>State</title></head>
<body>
<?php
$dbh = pg_connect("host=localhost dbname=state user=postgres");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
$sql = "SELECT * FROM state ";
echo "<table>";
echo "<table border=\"1\" align=\"center\">";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>State</th>";
echo "</tr>";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
while ($column = pg_fetch_array($result)) {
echo "<tr>";
echo "<td>".$column[0]."</td>";
echo "<td>".$column[1]."</td>";
echo "</tr>";
}
echo "</table>";
pg_free_result($result);
pg_close($dbh);
?>
</body>
</html>
in your current php file:
// Replace your while statement in your code above with this...
$row_counter = 0;
while ($row = pg_fetch_array($result,$row_counter,PGSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td><a href='state.php?id=".$row['id']."'>".htmlentities($row['state'])."</a></td>";
echo "</tr>";
$row_counter++;
}
And in another php file, let's say state.php, you could run something like this:
$state_id = $_GET['state_id'];
// connect to database...
$sql = "SELECT * FROM some_table WHERE state_id = '".pg_escape_string($state_id)."'";
// run your query...
Note: htmlentities will prevent XSS problems and pg_escape_string will help prevent SQL injection (but research prepared statements for a better approach).

mysqli dynamic generated table with link that opens form with all values from row

I have this table:(megaoverzicht.php) (I left out the part where it connects to the db)
echo "<table border='1'><tr><th>Formulier Id</th><th>Domeinnaam</th><th>Bedrijfsnaam</th><th>Datum</th><th>Periode</th><th>Subtotaal</th><th>Dealernaam</th><th>Offerte Maken</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['formuliernummer'] . "</td>";
echo "<td>" . $row['domeinnaam'] . "</td>";
echo "<td>" . $row['bedrijfsnaam'] . "</td>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['periode'] . "</td>";
echo "<td> € " . $row['subtotaal'] . "</td>";
echo "<td>" . $row['dealercontactpersoon'] . "</td>";
echo "<td><a href='offertemaken.php?id=" . $row->id . "'>Offerte Maken </a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I want to open offertemaken.php when the user clicks on Offerte Maken. It needs to open the form with the data from that row(id).
This is the code from (offertemaken.php)(I left out the part where it connects to the db)
<?php
$id=$_POST['id'];
$data = 'SELECT * FROM cypg8_overzicht WHERE id="$id"';
$query = mysqli_query($con,$data) or die("Couldn't execute query. ". mysqli_error());
$data2 = mysqli_fetch_array($query);
?>
<form>
<div class="formcontainer" onmousemove="">
<input type="text" name="datum" id="datum" value="<?php echo $data2[datum]?>">
<input type="text" name="formuliernummer" id="formuliernummer" value="<?php echo $data2[formuliernummer]?>">
<input type="text" name="periode" id="periode" value="<?php echo $data2[periode]?>">
<input type="text" name="domeinnaam" id="domeinnaam" value="<?php echo $data2[domeinnaam]?>">
<input type="text" name="bedrijfsnaam" id="bedrijfsnaam" value="<?php echo $data2[bedrijfsnaam]?>">
<input type="text" name="dealercontactpersoon" id="dealercontactpersoon" value="<?php echo $data2[dealercontactpersoon]?>">
</div><!--/.formcontainer-->
</form>
I cant get it to work. I am missing something I think! I make an error in the codes below:
echo "<td><a href='offertemaken.php?id=" . $row->id . "'>Offerte Maken </a></td>";
$id=$_POST['id'];
$data = 'SELECT * FROM cypg8_overzicht WHERE id="$id"';
I have been looking at a lot of tutorials but cant understand what i am doing wrong. Here a list to show that i am not just asking but actually have been looking for a solution by myself.
http://www.daniweb.com/web-development/php/threads/341921/-php-mysqli-update-database-using-id-syntax-help-requested-
http://www.codeofaninja.com/2012/01/phpmysqli-update-record.html
I have looked at many more but i don’t want to bother all of you with an extreme long list of links. And i am not allowed because my rep is not big enough! Dont downvote me please!
Question
I want to open offertemaken.php when the user clicks on Offerte Maken. It needs to open the form with the data from that row(id)?
Edit 1 Getting closer to the endresult
I found out(thanks to Cuba32) that the link in megaoverzicht.php was doing nothing so i changed the following
<a href='offertemaken.php?id=" . $row->id . "'>
to
<a href='offertemaken.php?id=" . $row['id'] . "'>
Now it is creating these kind of links:
something/formulieren/overzichten/offertemaken.php?id=24
This is a good thing(i think) but the form that opens is blank so offertemaken.php is doing nothing with the id???
Edit 2 (Thanks to Cube32)
Since yesterday the code has changed quite a bit. I belive that megaoverzicht.php is finished it sends the link as described in edit 1. The only problem is know in offertemaken.php. Below i will put in the code.
$con = mysqli_connect($server,$username,$password,$database);
if (!$con){
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,$database);
$id=$_GET['id'];
if($data = mysqli_prepare($con, 'SELECT * FROM cypg8_overzicht WHERE id="?"'))
{
/* bind parameters for markers */
mysqli_stmt_bind_param($data, "s", $id);
/* execute query */
mysqli_stmt_execute($data);
$data2 = mysqli_stmt_fetch($data);
But this code gives me the following error.
Warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in line 31. Line 31:
mysqli_stmt_bind_param($data, "s", $id);
I dont know how to solve this part. I will offcourse be looking on the internet to try and find a solution but if anyone knows it please post it. Thanks in advance.
Edit 3<= No more error (Thanks to Your Common Sense)
by changing WHERE id="?"' into WHERE id=?' i no longer have the error. But still it is not showing anything in the input fields
Edit 4<= Getting to confused and going back to original code.
Thanks for everyone who got me so far. But I can't see the forest anymore through the trees. I am going back to the original code and try to solve that. So the code is now as follows:
$id=$_GET['id'];
$data = 'SELECT * FROM cypg8_overzicht WHERE id="$id"';
$query = mysqli_query($con,$data) or die("Couldn't execute query. ". mysqli_error());
$data2 = mysqli_fetch_array($query);
error_reporting(E_ALL);
But this gives the following errors inside the input fields:
Notice: Use of undefined constant formuliernummer - assumed 'formuliernummer' in offertemaken.php on line 37
This error goes for all the input fields.
Edit 5
Fixed this by changing <?php echo $data2[formuliernummer]?> to <?php echo $data2['formuliernummer']?> but it is still not showing the information.
Edit 6 THE SOLUTION
I added the answer to the question below. Just look for answer written by HennySmafter.
Thanks to:
Cube32, SITDGNymall, Your Common Sense. Thanks all of you for helping me find the solution.
It took me a while but i found the answer.
megaoverzicht.php
echo "<td><a href='offertemaken.php?id=" . $row['id'] . "'>Offerte Maken </a></td>";
offertemaken.php
// Check whether the value for id is transmitted
if (isset($_GET['id'])) {
// Put the value in a separate variable
$id = $_GET['id'];
// Query the database for the details of the chosen id
$result = mysqli_query($con,"SELECT * FROM cypg8_overzicht WHERE id = $id");
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = "Invalid query: " . mysqli_error($result) . "\n";
$message .= "Whole query: " . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(),etc.
while ($row = mysqli_fetch_assoc($result)) {
echo $row['formuliernummer'] . "\n";
echo $row['domeinnaam'] . "\n";
echo $row['bedrijfsnaam'] . "\n";
echo $row['datum'] . "\n";
echo $row['periode'] . "\n";
}
} else {
die("No valid id specified!");
}
It is not showing the values in the input boxes because there are no input boxes into the echo but those can be easily added I imagine.
In reference to the edit 1:
You are referencing the variables by association, but are outputing the mysql as a default array. instead of
$data2 = mysqli_fetch_array($query);
Try this:
$data2 = mysqli_fetch_assoc($query);
Or:
$data2 = mysqli_fetch_array($query, MYSQLI_ASSOC);
Also, do you have error reporting turned on? If so, then if the array contains no data you should be getting warnings of some kind. If not, a good test is:
error_reporting(E_ALL);
This will warn you about any places where a variable is unset or a array is empty. Another good test is to simply echo out your query, which will tell you if there's any errors in the query itself(which can save some time). If you're not going to go the Prepared Statements route(which is highly encouraged), you can simply echo out $data into your script.

Categories