I am beginner to PHP and mySQL. I am creating a navigation trough my database. I have two tables set up one is the main nav items and one is the sub nav items. They are connected by the subject_id. I am using a loop to display them. The first table displays and the second table leaves space for where the information should be but it does not show up. I think it must be something in the SQL settings but I have no idea. Here is my code(i know the database is connected):
<?php require_once("includes/connection.php") ?>
<?php require_once("includes/functions.php") ?>
<?php include("includes/headder.php") ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if(!$subject_set){
die("database query failed: " . mysql_error());
}
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li>" . $subject["menu_name"] . "</li>";
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);
if(!$page_set){
die("database query failed: " . mysql_error());
}
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li>" . $page["menu_name"] . "</li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<h2>Content Area</h2>
</td>
</tr>
</table>
<?php include("includes/footer.php") ?>
Since you mentioned that white space is showing where the data should be, it appears that rows are indeed found. Consequently, your select statement should be ok. This most likely means that the index "menu_name" can't be found on the $page record.
Check to make sure that "menu_name" is indeed a column of the pages table.
To test what valid columns the $page record has you can use the following inside the while $page loop:
var_dump($page);
If the subject id is an integer you do not need single quotes in the where clause. However, if the subject_id contains any non-integer characters, your select statement would need to be:
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = '{$subject["id"]}'", $connection);
Related
the below to functions contain the code to insert into the sql database but sadly the db is still unable to load it to the database.
if (isset($_POST['register'])){
if(registerNewUser($_POST['inv_amount_expected'],$_POST['uname'],$_POST['passwo rd'],$_POST['email'])){
echo "You can now log-in to your account.
<a href='./index.php'>Click here to login.</a>
";
}else {
echo "Registration failed! Please try again.";
show_registration_form();
}
} else {
// has not pressed the register button
show_registration_form();
}
function registerNewUser($inv_amount_expected,$uname,$password,$email)
{
$sql = sprintf("insert into borrow (inv_amount_expected,uname,password,email) value ('&inv_amount_expected','&uname','&password','&email')",
mysql_real_escape_string($username), mysql_real_escape_string(sha1($password . $seed))
, mysql_real_escape_string($email), mysql_real_escape_string($code));
if (mysql_query($sql))
{
$id = mysql_insert_id();
return true;
}
else
{
return false;
}
return false;
}
could you help me out in where i am going wrong since im unable to understand.
i'm still an amature in php so please help me out.
I am putting my ideas together and at the moment i have the following:
-a mysql database with 2 tables.
-the first table "downloads" contains 3 rows, each with a unique id that and each represent the type of file being downloaded. e.g. application or theme.
-the second table contains the information about each download, these fields are the models supported by the download, the title, a picture, a brief description and a download link. each download has a unique id.
Now what i am trying to do is insert the data into a set of divs, these divs are as follows:
<div class="dlcontainer">
<div class="dlitem">
<div class="dltitle"><php $row['title'] ?></div>";
<div class="dlimage"><php $row['image'] ?></div>";
<div class="dldescription"><php $row['description'] ?></div>";
<div class="dllink"><php $row['downlink'] ?></div>";
</div>
</div>
As you can see i have been trying to populate the divs with information called from the database and that was my first attempt.
I didn't feel like i was going about this with the right approach, so i ended up with this, which also does not seem to work:
<?php
$con = mysql_connect("test","test","test");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM `content` WHERE pid = '2' AND models = 'all' OR models = $chosen");
while($row = mysql_fetch_array($result))
{
echo "<div class=\\"dlcontainer\\">";
echo "<div class=\\"dlitem\\">";
echo "<div class=\\"dltitle\\">$row['title']</div>";
echo "<div class=\\"dlimage\\">$row['image']</div>";
echo "<div class=\\"dldescription\\">$row['description']</div>";
echo "<div class=\\"dllink\\">$row['downlink']</div>";
echo "</div>";
echo "</div>";
}
mysql_close($con);
?>
Once i get this initial download working, i can then set up a loop that will display all the contents that match belong to a specific "pid" and either match a "models" value of "all" or one that has been selected by the user.
I am using phpmyadmin and had mySQL database working with my php until I started adding foreign key constraints. Then all of the sudden it stopped working. Now I'm backtracking to the very beginning when I ran my first query, but it still will not work (even though it used to). When I call for the mysql_error() nothing appears.
It seems so simple but I do not know what is going wrong. I even deleted out all of the tables from my database except the subjects table.
manage_content page (which should read my table):
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/header-home.php");?>
<?php
// 2. Perform database query
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
confirm_query($result);
?>
<div id="main">
<div id="navigation">
<ul class="subjects">
<?php
// 3. Use returned data (if any)
while($subject = mysqli_fetch_assoc($result)) {
// output data from each row
?>
<li><?php echo $subject["first_name"] . " (" . $subject["id"] . ")"; ?></li>
<?php
}
?>
</ul>
</div>
<div id="page">
<h2>Manage Content</h2>
</div>
</div>
<?php
// 4. Release returned data
mysqli_free_result($result);
?>
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?>
Functions page:
<?php
function confirm_query($result_set) {
if (!$result_set) {
die("Database query failed: " . mysql_error());
}
}
?>
Please help! I'm new and I know this is incredibly simple. I just don't know what I'm missing!
Because when you do query you call mysqlI_query() function, but when you wanna get error, you do it with mysql_query(), try to change that!!
mysqli_error()
I have a page displaying random bible quotes from which you can also search. The quotes on this page are displayed in dynamic link format, e.g. bible-query.php?id=200
On the search results page, I have put a link each at the bottom and top of the page to help the users get back to the random display page. These links are dynamic links. The only problem is, I can only get the top link to display the dynamic link, the bottom one just loads a page with no quotes.
What I want is to have the bottom and top 'Back' links display the same dynamic link location.
Here is the code I have for the search results:
<html>
<font face="arial">
<title>BQuotes: Random Bible Verses</title>
<?php
// db requirements
$db_host="localhost";
$db_username="username";
$db_password="password";
$db_name="name";
$db_tb_name="table";
$db_tb_atr_name="line";
$db_tb_atr_name2="book";
$db_tb_atr_name3="cap";
$db_tb_atr_name4="verse";
//do search task
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE
$db_tb_atr_name like '%".$query."%' OR $db_tb_atr_name2 like '%".$query."%'
OR $db_tb_atr_name3 like '%".$query."%' OR $db_tb_atr_name4 like '%".$query."%'");
echo "Search Results<ol>";
// new bible query section begins
define ('HOSTNAME', 'localhost');
define ('USERNAME', 'username');
define ('PASSWORD', 'password');
define ('DATABASE_NAME', 'name');
$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die ('I cannot connect to
MySQL.');
mysql_select_db(DATABASE_NAME);
$query = "SELECT id,book,cap,verse,line FROM table ORDER BY RAND() LIMIT 1 ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
}
//mysql_free_result($result);
//mysql_close();
//bible query new section ends
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<li>";
echo substr($data_fetch[$db_tb_atr_name2], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name3], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name4], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</li><hr/>";
}
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
echo "</ol>";
//mysql_close();
?>
</font>
</html>
Please help!
Your while loop is fetching the row using mysql_fetch_array. On the first time it runs it retrieves a result and so the value of $row is set to an array and the conditional is "true" so the bit in the while loop runs. However, on the second run-through there is no second row and so $row is set to false and the while loop doesn't run. This means that after the while loop $row is an empty variable. When it gets to the second calling of that array it is empty. By removing the while loop the $row variable is only fetched once and the first row is returned. This is all you need.
Just change this bit:
while ($row = mysql_fetch_array($result)) {
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
}
to:
$row = mysql_fetch_array($result);
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center>";
and both parts should work.
Change your code to this. No need for a while loop since you are limiting 1 row in your query. Also I changed the php mysql function that you should use.
$query = "SELECT id,book,cap,verse,line FROM table ORDER BY RAND() LIMIT 1 ";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
Noticed I used mysql_fetch_assoc
The thing is that I have made application form which saves in db. And I want on other page to display just some rows of the mysql table in php table where the first column is hyperlink to the single application as it is in the form. My question is how it can automatically make hyperlinks and pages for each form?
This is my code for now
mysql_connect("host", "user", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$data = mysql_query("SELECT * FROM applications ") or die(mysql_error());
echo ' <table width="760" border=1>
<tr>
<th>Заявление<br>От дата:</th>
<th>От/До</th>
<th>Статус</th>
</tr>';
while($info = mysql_fetch_array( $data ))
{
echo '
<tr>
<td>'.$info['today'] .'</td>
<td>От '.$info['data1data'] .'.'.$info['data1mesec'] .'.'.$info['data1god'] .' до '.$info['data2data'] .'.'.$info['data2mesec'] .'.'.$info['data2god'] .' </td>
<td>';
if($info['status'] == 1) {
echo '<img src="Images/approved.jpg" />';
}
else {
echo '<img src="Images/declined.jpg" />';
}
echo ' </td>
</tr> ';
}
echo '</table>';
The result I am trying to get is a table with 3 columns and rows for every application and from the first column of the application I get redirected to a single webpage for that application form and this link to be auto made by code something like "applications.php?id=[application id]"
I don't understand so much but:
<?php
$query = mysql_query("SELECT * FROM applications");
while($t = mysql_fetch_array($query)){
echo ''.$t['name'].'<br/>';
}
?>
Don't know what's the problem.
I seem to be stuck on a concept in database query and website development.
I have a website that will reflect what data is stored in a database and the website needs to change depending on that data: therefore, my menu system will not be hardcoded in. It builds my menu system based off a query of all the models in my database. The action of clicking on the menu will show tables without changing the page (a simple javascript "showtables" function). like so:
function showTables(TABLE_NAME)
{
if(TABLE_NAME != "PRINTER_TABLE")
{
document.getElementById("PRINTER_TABLE").style.display ="none";
}
if(TABLE_NAME != "show_ALL_PRINTERS")
{
document.getElementById("show_ALL_PRINTERS").style.display ="none";
}
document.getElementById(TABLE_NAME).style.display ="block";
}
I did not include all of my other if statements because there are about 15 of them. These statements will hide everything and the only show the formatted table of "TABLE_NAME" at the end of that script.
My problem is that, if all of the data will not be hardcoded in either HTML or PHP, I need to pass into my function "showTables" a model type or ID that will come from my query.
My Menu system code snipet:
<li class="hasmore"><span>Printer Parts</span>
<ul class="dropdown">
<?php
include 'connection.php';
$query = "SELECT * FROM all_printers";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
#echo "<h3>" . $row['printer_model'] . "</h3>";
echo "<li>" .$row['printer_model']."</li>\n";
}
?>
This puts the model into the menu system and the "PRINTER_TABLE" will access my showTables function, which then will show that table. But there are many different printer models and I need to tell my table query what specific model to get info on.
I hope this makes sense as there is a lot of logic behind it. Maybe there is an easier way..?
In my tables I have:
<table id="table">
<thead>
<tr>
<th scope="col" id="table">Type</th>
<th scope="col" id="table">Size</th>
<th scope="col" id="table">S/N</th>
<th scope="col" id="table">Model</th>
<th scope="col" id="table">Connection Type</th>
<th scope="col" id="table">Surplus</th>
<th scope="col" id="table">Amount</th>
</tr>
</thead>
<tbody>
<?php
include 'connection.php';
$query_misc = "SELECT * FROM all_parts WHERE part_type='MISC'";
$result_misc = mysql_query($query_misc);
while($row = mysql_fetch_array($result_misc))
{
echo "<tr>";
echo "<td><div align=\"center\">".$row['part_type']."</div></td>";
echo "<td><div align=\"center\">".$row['part_size']."</div></td>";
echo "<td><div align=\"center\">".$row['part_sn']."</div></td>";
echo "<td><div align=\"center\">".$row['part_model']."</div></td>";
echo "<td><div align=\"center\">".$row['part_connection']."</div></td>";
echo "<td><div align=\"center\">".$row['part_surplus']."</div></td>";
echo "<td><div align=\"center\">".$row['part_temp_amount']."/".$row['part_amount']."</div></td>";
echo "</tr>";
}
..... etc
Any ideas?
EDIT:
I do now know how to put what I click on, into the url properly..
<?
$query = 'SELECT printer_id, printer_model FROM printer_table WHERE 1 ORDER BY name';
$products = mysql_query($query);
while ($product = mysql_fetch_assoc($products)) :
{
echo "<li>" .$product['printer_model']."</li>\n";
}
?>
or
<li><?=$product['printer_model']?></li>
doesnt work properly, because i need to call the suggested part list php part, but do not know where to do so..
EDIT:
I placed that php within a div and the echos will fill out my table, but how am I suppose to call that div to only run when that onclick action? I know that is not how divs work, It would seem I would need a JS function but I know you cannot do php within JS. AJAX perhaps?
EDIT----------------------------------------------------
The call here to the DB is correct, tested it manually without any errors, but I am still receiving a syntax error upon loading my home page. Here is my call to the DB via PHP...
<?
if (!isset($_GET['action']))
{
//If not isset -> set with dumy value
$_GET['action'] = "undefine";
}
include 'connection.php';
$query = 'SELECT ppart_table.* FROM ppart_table LEFT JOIN printer_part_relation ON ppart_table.part_id = printer_part_relation.part_id WHERE printer_part_relation.printer_id ='.mysql_real_escape_string($_GET['printer_id']);
$parts = mysql_query($query) or die("Query failed with error: ".mysql_error());
while ($row = mysql_fetch_assoc($parts))
{
echo table blah blah
}
?>
I have tried supressing all errors via this:
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
And the syntax error at page load still exist.
My home page of this site does NOT have the "product_id=" after mysite.php
I would recommend using PHP's PDO class for database access. I would also recommend using JQuery for all of you javascripting. It makes things VERY simple. It would also be advantagious to keep your display code away from your business logic (two languages should for the most part not be in the same source file ie: HTML/PHP/Javascript). I will stick with what is familiar for now however. perhaps something like this?
menu
<ul>
<?
$query = 'SELECT id, name FROM product_category_table WHERE 1 ORDER BY name';
$product_categories = mysql_query($query);
while ($product_category = mysql_fetch_assoc($product_categories)) :
?>
<li>
$product_category['name']
<ul id="<?= $product_category['id'] ?>" style="display: none;">
<?
$query = 'SELECT id, name FROM product_table WHERE category_id = ' . $product_category['id'] . ' ORDER BY name';
$products = mysql_query($query);
while ($product = mysql_fetch_assoc($products)) :
?>
<li><?= $product['name'] ?></li>
<?
endwhile;
?>
</ul>
</li>
<?
endwhile;
?>
</ul>
javascript
function toggleVisibility(category) {
var element = document.getElementById(category);
if (element.style.display == 'none') {
element.style.display = 'block';
} else {
element.style.display = 'none';
}
}
part list
<?
$query = 'SELECT part_table.* FROM part_table LEFT JOIN product_to_part_associations_table ON parts_table.id = product_to_part_associations_table.part_id WHERE product_to_part_associations_table.product_id = ' . mysql_real_escape_string($_GET['product_id']);
$parts = mysql_query($query);
while ($part = mysql_fetch_assoc($parts)) {
echo part table blah blah
}
?>
db tables
product_category_table
id, name, etc
product_table
id, category_id, name, etc
part_table
id, name, etc
part_to_product_association_table
id, product_id, part_id
set the proper indexes on your table columns for speed. the part_to_product_association table allows for a many to many relationship between records in the product table and records in the parts table (needed in the case one part could be used in multiple products, and one product can use multiple parts).