I'm trying to pull data from my DB to a html form, I have managed to so for a table in a while loop, but not for a form. Here is my code:
if(isset($_POST['search'])){
$quote = $_POST['quote'];
}
$stmt = "SELECT * FROM dbo.tblVersions2 WHERE QuoteNumber LIKE '".$quote."'";
$result = odbc_exec($conn, $stmt) or die('ODBC Error: ' . odbc_errormsg($conn) );
while (odbc_fetch_row($result)) // while there are rows
{
// What do i out here? I only know echo "<td>" . odbc_result($result, "QuoteNumber") . "</td>";
// which is to display tabular data, but how with a form??
}
?>
I've tried declaring a variable in the while loop, such as $Address1 = -'Address1]; then declaring in the form value=" but it doesn't seem to work, please help!
Related
I'm trying to fill in the different options on a form calling objects from a database using a different php file. Here is my code:
PHP Code (functions.php):
<?php /* Gets values from sql and inserts into html options values */
function selectOptions($column, $table){
// fetch records
$sql = "SELECT" . $column. "FROM" . $table. "ORDER BY Id";
$result = $conn->query($sql);
//check there are more than 0 rows
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo '<option>' .$row['$column']. '</option>';
}
}
else {
echo '<option>No results!</option>'; } } ?>
HTML code:
<?php
require "php/dbconnect.php";
require "php/functions.php";
?>
<select class="form-control">
<option>SUBJECT</option>
<?php
selectOptions("Subjects", "general_subjects");
?>
</select>
I use this function multiple times for different forms, so I figured having it in its own file would be the best approach but it does not work. When I use the function without variables inside the index.php file, it seems to work. I don't know what I am doing wrong
EDIT: Image of what it should be doing Image of index.php
Add spaces to your sql query. Also you need to access the $column variable properly. You shouldn't wrap it around single quotes as it is a variable. You also have a variable scope issue as $conn does not exist
function selectOptions($conn, $column, $table) {
// fetch records
$sql = "SELECT `" . $column . "` FROM `" . $table . "` ORDER BY Id"; #add spaces
$result = $conn->query($sql);
//check there are more than 0 rows
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo '<option>' . $row[$column] . '</option>'; #remove single quotes
}
} else {
echo '<option>No results!</option>';
}
}
When you call the above function, add the echo keyword
<?php
echo selectOptions($conn, "Subjects", "general_subjects"); #echo the function
?>
Would you try editing your function.php sql part like this
$sql = "SELECT " . $column. " FROM " . $table. " ORDER BY Id";
i think the spaces is the reason that it's not returning data
I have an query that shows data from db. I use while loop to display data. Problem is that I can call (echo) result only once.
Here is my code:
$ime_ = "SELECT * FROM `users` WHERE '" . ($_COOKIE['username']) . "' = user_username";
$ime_result = $mysqli->query($ime_);
and later in my html I use this result as:
<?php
if ($ime_result->num_rows > 0)
while($row = $ime_result->fetch_assoc()) {
echo "<h2>" . $row["Ime"] . "</h2>";
}
?>
This work ok, but I want to use this result to display many times in my html. And when copy while loop again later in html no result is given.
Store the string with data from while() into a variable, than apply echo to that variable as many times as you like..
$re_out ='';
if($ime_result->num_rows > 0){
while($row = $ime_result->fetch_assoc()) {
$re_out .="<h2>". $row["Ime"] ."</h2>";
}
}
echo $re_out;
//etc..
echo $re_out;
<?php
if ($ime_result->num_rows > 0)
$ime_result->data_seek(0); // seek to row no. 1
while($row = $ime_result->fetch_assoc()) {
echo "<h2>" . $row["Ime"] . "</h2>";
}
?>
Haven't run script. Try if it works.
I am having problem trying to get a web url working within php, I am basically getting a list of content displayed, the list content becomes a links to a result webpage (detail.php). I want to display the result webpage with more detailed info. on the same webpage with a back button to original list.
=====
my list webpage code
=====
$c = 0; //Variable to keep count of categories
$servicetype = '$brand'; //variable declaration last displayed servicetype.
$strSQL = "SELECT * FROM <tablename> ORDER BY serviceType, serviceName ASC";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
//If the servicetype name has changed, display it and update the tracking variable
if ($servicetype != $row['serviceType']){
$servicetype = $row['serviceType'];
//If this isn't the first category, end the previous list.
if ($c>0) echo "</ul>";
echo '<h3>'.$row['serviceType'].'</h3><ul>'; // subheading & related content.
$c++;
}
$strName = $row['serviceName'];
$strLink = "<a href = 'detail.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
// Close the database connection
mysql_close();
?>
====
my detail.php code below
=====
$strSQL = "SELECT * FROM gu_service_cat WHERE id = " . $_GET["id"];
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
// Write the detail data of the ID
echo '<h3>ID:</h3>' . $row['ID'] . ' ' . $row['guUrl'] . "</dd>";
echo "<dt>availability:</dt><dd>" . $row["availability"] . "</dd>";
}
//echo '<h3>'.$row['serviceType'].'</h3><ul>';
// Close the database connection
mysql_close();
?>
</dl>
<p>
Return to the list
I do see you incorectly using variable variables.
This is incorrect:
$servicetype = '$brand';
if ($servicetype != $row['serviceType'])
What you need to do is assign the variable $servicetype a string, in this case 'brand' and use that in the if statement:
$servicetype = 'brand';
if ($$servicetype != $row['serviceType'])
Notice the double dollar sign. Read more about Variable Variables.
I have a bunch of items I list on a page and each of these items have an id associated with them on there link.
For instance, when you hover over the items you get a url of items.php?id=5.
I have my while loop which produces all of the items on this page including the links, how do I carryover that ID into my other code?
Here is my code that brings up all the items.
while ($results = mysqli_fetch_assoc($query)) {
echo
"<li>
<img style='width: 200px; height: 200px' src='images/inventory/" . $results['Image'] . "'/>" .
"<div class='infoContainer'>" .
"<a class='productLink' href='selectItems.php?status=active&id=" . $results['ID'] . "'>" . $results['ProductName'] . "</a>" .
"<br />" .
"<i>Currently Available </i>" .
"<i class='quantity'>" . $results['Quantity'] . "</i>" .
"</div>" .
"</li>";
}
Now I simply want to put each items attributes into a dialog box. How do I carryover an id from above to get the right data back?
<div id="dialog" title="A title">
<?php
$sql = "SELECT * FROM `my_table` WHERE `id` = ";
$query = mysqli_query($sql) or die(mysqli_error());
var_dump($sql);
$results = mysqli_fetch_assoc($query);
var_dump($results);
?>
</div>
You should call a javascript function and send id in it..
donot forget to include the jquery files..
this method loads the data using AJAX..
<script>
function getData(idtosend){
$.post('getdata.php', id: idtosend, function (response){
$("#dialog").html(response);
});
}
</script>
Here is the code of getdata.php file:
<?php
//Connect mysql here
$id = mysql_real_escape_string($_POST['id']);
$sql = "SELECT * FROM `my_table` WHERE `id` = $id";
$query = mysqli_query($sql) or die(mysqli_error());
var_dump($sql);
$results = mysqli_fetch_assoc($query);
var_dump($results);
?>
in PHP you cat use ob_start() - this is output buffer for echo operations.
after save result in variable or array and put it into needful block on page
I need help displaying data from mysql to a webpage, I am coding in php.
My database consists of products which are cars(same type e.g Chevy), right now I have 2 rows (I can add more if I want to), each cars contains the image path, and description.
I can show one row (car) but I am unable to show all rows. I know I have to go through a loop to get all the data from the cars database but I am not sure how to implement it.
This is what I have so far. Assuming I already connected to my database
note: the image path I would like to show the picture in my website.
This is how i would like it to display in my webpage:
$query = "SELECT * FROM cars where cars.carType = 'Chevy' AND \
cars.active = 1";
$numberOfFieds = mysqli_num_fields($result);
$numberOfRows = mysqli_num_rows($result);
/* Gets the contents */
$row = mysqli_fetch_row($result);
$rows = mysqli_fetch_assoc($result);
$fieldcarssontable = array_keys($row);
echo "<table>";
while($row = mysqli_fetch_assoc($result)){
echo "<th>" . $fieldcarssontable[imgPath] . "</th>";
echo "<th>" . $fieldcarssontable[description] . "</th>";
}
echo "</tr>";
echo "</table>";
Just add a while loop. mysqli_fetch_assoc returns a row and moves the internal pointer to the next row until all rows are fetched, then it returns false and the while loop will stop
Pseudo syntax to understand while
while ( this is true ) {
execute this
}
So on your case you can say
while ( $row = mysqli_fetch_assoc( $result ) ) {
// process/output $row
}
mysqli_fetch_assoc and mysqli_fetch_row literally do the same, assoc gives you the array with your result field names as index so this is simpler to access ( $row['name'] rather than $row[0] when using fetch_row )
Have fun! :)
EDIT
// connect to your database server
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
// an error occured
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
// build your query
$query = "SELECT
* # select actual fields instead of *
FROM
cars
WHERE
cars.carType = 'Chevy'
AND
cars.active = 1";
// execute query
$result = mysqli_query($link, $query );
if ( !$result ) {
die( 'no result' );
}
// number of fields
$numberOfFields = mysqli_num_fields($result);
// the field names
$fieldNames = mysqli_fetch_fields($result);
// number of result rows
$numberOfRows = mysqli_num_rows($result);
// watch the content of fieldName and compare it with the table header in the output
print_r( $fieldName );
echo "<table>\n";
// table header, not neccessary to put this into a loop if the query isn't dynamic
// so you actually know your field names - you can echo the header without any variable.
// for the sake of learning about loops I added this
foreach( $fieldNames as $index => $fieldName ) {
echo "\t<th>field #" $index . ", name:" . $fieldName . "</th>\n";
}
// now it's time to walk through your result rows, since we only need to check for "true" a while loop does best
while ( $row = mysqli_fetch_assoc( $result ) ) {
echo "\t<tr><td>" . $row['imgPath'] . "</td><td>" . $row['description'] . "</td></tr>\n";
}
echo "</table>\n";
// remove the result from memory
mysqli_free_result( $result );
mysqli_close( $link );
You misspelled $numberOfFields in your loop, which means you're using a different variable for your loop control. Your loop won't work.
I recommend turning on the error reporting so PHP can catch this stuff for you.
Use this... just while loop
<?php
// Array
while($result = mysql_fetch_assoc($result)) {
//show you fields
echo $result["FieldName"];
}
?>
Or use this proper
<?php
// Edit it as per your query
$query = "SELECT * FROM cars";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while($row = $result->fetch_assoc()) {
//show you fields
echo $row["Name"];
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>