SQL Help - Newbie - php

I'm picking up the pieces for a friend. His website used to work and pull featured products from his database, but it doesn't seem to be working. Its just showing the Error!!
Any help would be appreciated.... I'm not really up to date with SQL.
Here's the code:
<?php
require_once('const.php');
$link = dbConnect();
$query = "SELECT *
FROM vehicle_tbl, manufacturer_tbl
LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
WHERE vehicle_tbl.manufacturer_id = manufacturer_tbl.manufacturer_id AND
vehicle_tbl.vehicle_feature2 = '1'
GROUP BY vehicle_tbl.vehicle_id
ORDER BY RAND()
LIMIT 1";
$result = false;
$result = #mysql_query($query, $link);
$fmain = false;
if (($result) && (#mysql_num_rows($result) > 0)) {
$fmain = #mysql_fetch_array($result, MYSQL_ASSOC);
#mysql_free_result($result);
}
$query = "SELECT *
FROM vehicle_tbl, manufacturer_tbl
LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
WHERE vehicle_tbl.manufacturer_id = manufacturer_tbl.manufacturer_id AND
vehicle_tbl.vehicle_feature1 = '1'
GROUP BY vehicle_tbl.vehicle_id
ORDER BY RAND()
LIMIT 6";
$offers = false;
$offers = #mysql_query($query, $link);
function nextOffer() {
global $offers;
if ($offers && ($row = mysql_fetch_array($offers))) {
if (! isset($row['image_name'])) { // no image
$image = 'images/noimagesml.jpg';
} else {
$image = 'images/vehicles/sml/'.stripslashes($row['image_name']);
}
$title = stripslashes($row['manufacturer_name']).' '.stripslashes($row['vehicle_model']);
$price = number_format((float) $row['vehicle_price_pcm'], 2);
$id = (int) $row['vehicle_id'];
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="contenthead"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="11" height="40" align="left" valign="top"><img src="images/featre_left_hd.gif" width="11" height="23"></td>
<td width="100%" align="left" valign="middle" class="contenthead">'.$title.'</td>
<td width="11" height="40" align="right" valign="top"><img src="images/featre_rght_hd.gif" width="11" height="23"></td>
</tr>
</table></td>
</tr>
<tr>
<td class="contentpane"><table width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td align="center" valign="middle"><img src="'.$image.'" width="100" height="58" class="bordered" alt="'.$title.'"></td>
</tr>
<tr>
<td align="center" valign="top" class="princing">from just &pound'.$price.' pcm</td>
</tr>
<tr>
<td align="right" valign="middle"><img src="images/more_butt.gif" width="54" height="20" border="0"></td>
</tr>
</table></td>
</tr>
</table>';
} else {
echo 'Error!!';
}
}
?>

Display mysql errors like this:
mysql_query($query, $link) or die(__FILE__ . ' Line ' . __LINE__ . ': ' . mysql_error());
This should help you debug.
P.S. the mysql_* functions are deprecated - http://php.net/manual/en/mysqlinfo.api.choosing.php

Related

Pagination is only changing the page number, not the content

I have a table with a list of users and I want to create pagination for this table.
However, when I click page number 2 the page changes but not the contents of the list of users.
<?php
session_start();
require_once "connect.php";
//Database connect
$database = #new mysqli($host, $db_user, $db_password, $db_name);
//Limit users
$start = 0;
$limit = 3;
//Question to -> Database
$sql = ("SELECT * FROM users LIMIT $start, $limit");
$adds = $database->query($sql);
//Return records
$rows = mysqli_num_rows($adds);
echo "Rows found in database.. ".$rows."<br /><br />";
if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}
else{
$id=1;
}
if ($rows >= 1)
{
//the beginning of the table
echo<<<END
<table width="1000" align="center" border="1" bordercolor="#d5d5d5" cellpadding="0" cellspacing="0">
<tr>
<td width="50" align="center" bgcolor="e5e5e5"></td>
<td width="50" align="center" bgcolor="e5e5e5">IDUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">NazwaUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">EmailUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">DataRejestracji</td>
<td width="20" align="center" bgcolor="e5e5e5">Indetyfikator grupy</td>
</tr>
<tr>
END;
}
while($score = mysqli_fetch_assoc($adds)) {
$row_score_id = $score['id'];
$row_score_user = $score['user']." ";
$row_score_register = $score['dataRes']. " ";
//continued table
echo<<<END
<td width="50" align="center"><a class="button_red" href="delete_user.php?id=''">Delete</a></td>
<td width="50" align="center">$row_score_id</td>
<td width="100" align="center">$row_score_user</td>
<td width="100" align="center"></td>
<td width="100" align="center">$row_score_register</td>
<td width="100" align="center"></td>
</tr>
END;
}
$pagnSql = ("SELECT * FROM users");
$pagnBase = $database->query($pagnSql);
$pagnRows = mysqli_num_rows($pagnBase);
$total=ceil($pagnRows/$limit);
if($id>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<a href='?id=".($id-1)."' class='button'>PREVIOUS</a>";
}
if($id!=$total)
{
////Go to previous page to show next 10 items.
echo "<a href='?id=".($id+1)."' class='button'>NEXT</a>";
}
for($i=1;$i<=$total;$i++)
{
if($i==$id) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
}
?>
You have to put the $start from GET
Change this
$start = 0;
To:
$start = $_GET[id];

php and mysql. view topics date

I can final exams assignment of lecturers to create a website forum.
and now there is a problem in my coding.
please fix.
when creating a topic / thread, and we opened the topic / threadnya, there is a date that tell us when the topic / thread made.
but in my coding, showing all existing date in the database.
<?php
session_start();
if($_SESSION['logged'] == true)
{
if(isset($_SESSION['username']))
{ echo $username=$_SESSION["username"];
}else
{
header('Location:login.php');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!==CSS>
<style>
#clock{
pointer-events: none
}
</style>
<!close css>
<img src="head1.jpg" alt="icon" width="100%" height="110">
<img src="line1.jpg" alt="icon" width="100%" height="20">
<title>Venray</title>
<body background="alienbackground.jpg">
<font face="comic sans MS">
<!==Table Login==>
<table border="0" style="width:99%" bgcolor="#424242" align="center">
<tr>
<table border="0" style="width:98%" bgcolor="#424242" align="center">
<tr>
<td> <img src="start.gif" alt="icon" width="26" height="26"> </td>
<td rowspan="2" align="right">
<table border="0" bgcolor="#2E2E2E">
<tr>
<td><b>
<?php
{
echo "<tr><td><b><font color=green> Welcome, ";
echo $username;
echo '<span>, [Log Out]</span></li>';
echo "</tr></td></b></font>";
}
?>
</b></td>
</tr>
<tr>
<td id=clock align="right"><b><iframe src="http://free.timeanddate.com/clock/i4eh41xm/n108/tlsg/fn7/fs12/tct/pct/ftb/tt0/tw1/tm1/th1" frameborder="0" width="182" height="20" allowTransparency="true"></iframe>
</b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top"><b><font size="4">Community</font></b></td>
</tr>
</table>
</tr>
<p style="color:#ff0000">
<!==Table please welcome==>
<table border="0" style="width:98%" bgcolor="#424242" align="center">
<tr>
<td align="right">
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require("conn.php");
$con = mysql_connect($server,$user,$pass);
$db = mysql_select_db($db);
$sql = "select * from d_admin order by username";
//echo $sql;
$ambil_data = mysql_query($sql);
while($data = mysql_fetch_array($ambil_data))
$result = mysql_query("SELECT * FROM d_admin");
$rows = mysql_num_rows($result);
echo "Member " . $rows . " * ";
?>
Post 1 * Topics 1 </td>
</tr>
</table>
<br>
<table border="1" width="98%" height="6%" align="center" bgcolor=#424242 >
<tr>
<td align="left" ><img src="hometopic.gif" width="40" height="40"> </td>
</tr>
<tr>
<table border="1" width="98%" height="40" align="center" bgcolor="#86B404" bordercolor=green>
<tr>
<th width="70%" align="left"><img src="open.png" width="40" height="40">Posting Display</th>
<td align="right"><img src="replay.png" width="150" height="40"></td>
</tr>
</tr>
<tr>
<table border="1" width="98%" height="70" align="center" bgcolor=#424242>
***<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$con = mysql_connect($server,$user,$pass);
$db = mysql_select_db($db);
$sql = "select * from tabel_topik ";
$ambil_data = mysql_query($sql);
while($data = mysql_fetch_array($ambil_data))
{
echo '<tr><th align="left" valign="top" colspan="2" >'.$data['date'].'</th></tr>';
echo '</tr>';
}
?>***
<tr><th align="center" width="15%">Venray</th><td align="left" width="85%">title</td></tr>
<tr><th align="top" width="15%">join dates</th><td align="left" rowspan="2" valign="top">awd</td></tr>
<tr><th align="center" valign="top" width="15%" height="400">9 Post</th></tr>
<tr>
<td align="left">*</td>
<td align="right" width="85%"><img src="replay.png" width="150" height="40"></td></tr>
</table>
</tr>
</table>
</body>
</font>
</head>
</html>
Get forum specific datas by passing the ID or any similar field in the query for starters. Then if you are looking to format the way in which the date is displayed, you could use the php date function like this:
while($data = mysql_fetch_array($ambil_data))
{
echo '<tr><th align="left" valign="top" colspan="2" >'.date('jS M, Y',strtotime($data['date'])).'</th></tr>'; //format the date as required
echo '</tr>';
}

trying to select a certain category from database

im a real newbie when it comes to sql but im trying to pick up the pieces of my friends site... It all seems to be working fine apart from the left hand navigation to certain categories.
This is the site: http://tyresinwigan.co.uk/new/
The individual manufacturers should point to each manufacturer direct but they seem to be listing the results for all manufacturers.
Here is the code from the search.php:
<?php
require_once('const.php');
$link = dbConnect();
$manufacturer_id = 0;
$name = '';
if (isset($_GET['make']) && is_numeric($_GET['make'])) {
$manufacturer_id = (int) $_GET['make'];
}
$query = "SELECT manufacturer_name FROM manufacturer_tbl WHERE manufacturer_id = $manufacturer_id";
$result = false;
$result = #mysql_query($query, $link);
if (($result) && (#mysql_num_rows($result) > 0)) {
$row = #mysql_fetch_array($result, MYSQL_ASSOC);
$name = stripslashes($row['manufacturer_name']);
}
$query = "SELECT *,
v.vehicle_id AS vehicle_id_alias
FROM vehicle_tbl AS v
LEFT JOIN image_tbl AS i ON v.vehicle_id = i.vehicle_id
GROUP BY v.vehicle_id
HAVING v.manufacturer_id = $manufacturer_id";
$offers = false;
$offers = #mysql_query($query, $link);
$items = 0;
if ($offers) $items = mysql_num_rows($offers);
function nextOffer() {
global $offers;
global $items;
$items --;
if ($offers && ($row = mysql_fetch_array($offers))) {
if (! isset($row['image_name'])) { // no image
$image = 'images/noimagesml.jpg';
} else {
$image = 'images/vehicles/sml/'.stripslashes($row['image_name']);
}
$title = stripslashes($row['manufacturer_name']).' '.stripslashes($row ['vehicle_model']);
$price = number_format((float) $row['vehicle_price_pcm'], 2);
$id = (int) $row['vehicle_id_alias'];
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="contenthead"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="11" height="40" align="left" valign="top"><img src="images/featre_left_hd.gif" width="11" height="23"></td>
<td width="100%" align="left" valign="middle" class="contenthead">'.$title.'</td>
<td width="11" height="40" align="right" valign="top"><img src="images/featre_rght_hd.gif" width="11" height="23"></td>
</tr>
</table></td>
</tr>
<tr>
<td class="contentpane"><table width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td align="center" valign="middle"><img src="'.$image.'" width="100" height="58" class="bordered" alt="'.$title.'"></td>
</tr>
<tr>
<td align="center" valign="top" class="princing">from just &pound'.$price.' pcm</td>
</tr>
<tr>
<td align="right" valign="middle"><img src="images/more_butt.gif" width="54" height="20" border="0"></td>
</tr>
</table></td>
</tr>
</table>';
} else {
echo ' ';
}
}
?>
Change:
$query = "SELECT * FROM vehicle_tbl LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
WHERE vehicle_tbl.manufacturer_id = $manufacturer_id
GROUP BY vehicle_tbl.vehicle_id";
to:
$query = "SELECT * FROM vehicle_tbl LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
GROUP BY vehicle_tbl.vehicle_id
HAVING vehicle_tbl.manufacturer_id = $manufacturer_id";
You are looking at the wrong query. The problem isnt in the left menu, but the query on the resulting page. You need to look at that one, and make sure it is formatted properly.

how to make table sorting and searching by

I a newbie with this language and I do not know how to make it.
So in here I have script like this :
<?php
include_once "library/inc.sesadmin.php";
include_once "library/inc.library.php";
$row = 20;
$hal = isset($_GET['hal']) ? $_GET['hal'] : 0;
$pageSql = "SELECT * FROM kategori";
$pageQry = mysql_query($pageSql, $koneksidb) or die ("error paging: ".mysql_error());
$jml = mysql_num_rows($pageQry);
$max = ceil($jml/$row);
?>
<table width="700" border="0" cellpadding="2" cellspacing="1" class="table-border">
<tr>
<td colspan="2" align="right"><h1><b>DATA KATEGORI</b></h1></td>
</tr>
<tr>
<td colspan="2"><img src="images/btn_add_data2.png" height="25" border="0" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><table class="table-list" width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<th width="32" align="center"><b>No</b></th>
<th width="93" align="center"><b>Kode</b></th>
<th width="444"><b>Nama Kategori </b></th>
<th width="93" align="center"><b>Jumlah Barang</b> </th>
<td width="47" align="center" bgcolor="#CCCCCC"><b>Ubah</b></td>
<td width="52" align="center" bgcolor="#CCCCCC"><b>Hapus</b></td>
</tr>
<?php
$kategoriSql = "SELECT kategori.*, (SELECT COUNT(*) FROM barang WHERE barang.kd_kategori=kategori.kd_kategori) As qty_barang
FROM kategori ORDER BY kd_kategori ASC LIMIT $hal, $row";
$kategoriQry = mysql_query($kategoriSql, $koneksidb) or die ("Query kategori salah : ".mysql_error());
$nomor = 0;
while ($kategoriRow = mysql_fetch_array($kategoriQry)) {
$nomor++;
$Kode = $kategoriRow['kd_kategori'];
?>
<tr>
<td align="center"><b><?php echo $nomor; ?></b></td>
<td align="center"><b><?php echo $kategoriRow['kd_kategori']; ?></b></td>
<td><?php echo $kategoriRow['nm_kategori']; ?></td>
<td align="center"><?php echo $kategoriRow['qty_barang']; ?></td>
<td align="center"><img src="images/btn_edit.png" width="20" height="20" border="0" /></td>
<td align="center"><img src="images/btn_delete.png" width="20" height="20" border="0" alt="Delete Data" /></td>
</tr>
<?php } ?>
</table></td>
</tr>
<tr>
<td><b>Jumlah Data :</b> <?php echo $jml; ?> </td>
<td align="right"><b>Halaman ke :</b>
<?php
for ($h = 1; $h <= $max; $h++) {
$list[$h] = $row * $h - $row;
echo " <a href='?page=Data-Kategori&hal=$list[$h]'>$h</a> ";
}
?>
</td>
</tr>
</table>
I want to make table sorting and search by at this table, but I do not know how to make it.
Can anyone help me for this simple project??
I really appreciate your big help in here..
I'm not going to give you the exact code because it's not how we do things in SO,
However I will share with you the main idea of how to accomplish it.
For each column add a link, for example:
<td><a href='?orderby=id'>ID</a></td>
<td><a href='?orderby=name'>Name</a></td>
When id and name are fields in the table (SQL & html).
Before the query you check if there's a LEGIT value for orderby ($_GET['orderby'])
and if so add it to the ORDER BY ... in the query.
Please notice that you're using mysql_* which is not safe (especially in this case),
consider using mysqli_* or PDO (you can google it)
EDIT
You can add &direction= to the columns' links to set the direction (DESC / ASC)

Same results from Database

I need to add "Last member topics" in the members profile, so that it shows the last 5 topics that member wrote.
I used this code:
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
$rslast = mysql_fetch_array($last_topic);
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
$i=0;
while ($i < $result){
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited
:
'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>
';
++$i;
}
}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}
It works, but showing the same topic in the 5 rows??!!
any solutions?
its because you are assigning a $rslast one time and then using this variable in loop ,
try this
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
while($rslast = mysql_fetch_array($last_topic)) {
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited
:
'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>
';
}
}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}
You are getting same value because your $result var has total rows from query which in this case is 5 while you are saving the value to variable $rslast, instead use while loop so that you can loop over array values.
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
while($rslast = mysql_fetch_array($last_topic,MYSQL_ASSOC)) {
//^-----while Loop added to loop through arrays, ^----associative array
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited :'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>';
}}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}

Categories