I have been trying to fix my issue on this since morning :)
I have two tables. table1= post table2=users. now I need to extract data from both tables. searched how to use INNER JOIN, found some codes. I run the query inside phpmyadmin and it works fine here is my sql query
SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id
FROM post INNER JOIN users
ON post.post_creator=users.id
WHERE post.post_creator=users.id and post.topic_id=19
ORDER BY post.post_date DESC
but when I use this sql query inside my php it gives me 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 'INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC' at line 1
below is my php code
<?php
include_once './forum_Scripts/connect_to_MySql.php';
$cid = $_GET['cid'];
if(isset($_SESSION['uid'])){
$logged = " | <a href ='create_topic.php?cid=".$cid."'>Click here to create a new topic</a> ";
}else{
$logged = " | Please log in to create topics in this forum.";
}
$tid = $_GET['tid'];
$sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 1){
echo "<table width='100%'> ";
if(isset($_SESSION['uid'])){
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /> | <a href = 'http://amaforum.net63.net/'>Return to Forum Index</a><hr />";
}else{
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td>";}
while ($row = mysql_fetch_assoc($res)){
$sql2 = "SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id"
. "FROM post INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC" ;
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2))
{
echo "<tr><td valign='top' style='border:2px solid #000000'><div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".$row2['post_content']."</div></td>"
. "<td width='200' valign='top' style='border: 1px solid #000000;'>"
. "<input id='".d_text."' type='".text."' name='".the_creator."' value='".$row2['post_creator']."' >"
. "echo '$user_info' "
. "</td></tr>"
. "<tr><td colspan='2'><hr /></td></tr> ";
}
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1 ";
$res3 = mysql_query($sql3) or die (mysql_error());
}
echo "</table>";
}else{
echo "<p>This topic does not exist</p>";
}
mysql_close();
?>
I can get it to work and I really need help from you guys..
Thanks in advance
This should solve your problem (I've just added the space that was missing when you concatenated the two lines of your sql query):
<?php
include_once './forum_Scripts/connect_to_MySql.php';
$cid = $_GET['cid'];
if(isset($_SESSION['uid'])){
$logged = " | <a href ='create_topic.php?cid=".$cid."'>Click here to create a new topic</a> ";
}else{
$logged = " | Please log in to create topics in this forum.";
}
$tid = $_GET['tid'];
$sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 1){
echo "<table width='100%'> ";
if(isset($_SESSION['uid'])){
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /> | <a href = 'http://amaforum.net63.net/'>Return to Forum Index</a><hr />";
}else{
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td>";}
while ($row = mysql_fetch_assoc($res)){
$sql2 = "SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id "
. "FROM post INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC" ;
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2))
{
echo "<tr><td valign='top' style='border:2px solid #000000'><div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".$row2['post_content']."</div></td>"
. "<td width='200' valign='top' style='border: 1px solid #000000;'>"
. "<input id='".d_text."' type='".text."' name='".the_creator."' value='".$row2['post_creator']."' >"
. "echo '$user_info' "
. "</td></tr>"
. "<tr><td colspan='2'><hr /></td></tr> ";
}
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1 ";
$res3 = mysql_query($sql3) or die (mysql_error());
}
echo "</table>";
}else{
echo "<p>This topic does not exist</p>";
}
mysql_close();
?>
To avoid issues like this in the future, it's always good practice to log the sql that's being executed in your code somewhere, or even just echo it onto the web page while testing. That way you'll very quickly debug issues like this.
Related
The following php script is used to retrieve set of data(OrderNo.) from a database! when the delete link is clicked i want to remove the row in which that specific delete link exists and transfer the data to the 'transfer' table!
<?php
require("includes/db.php");
$sql="SELECT * FROM `order` ";
$result=mysqli_query($db,$sql);
echo"<head>";
echo'
<link rel="stylesheet" href="view.css">
<head>
';
echo"</head>";
echo "<body >";
echo "<table border=1 cellspacing=0 cellpadding=4 > " ;
echo"<tr bgcolor=grey>";
echo"<td align=center>";
echo "<font size=4>";
echo "<B>";
echo "Order No.";
echo "</B>";
echo"</td>";
echo"</tr>";
while($row=mysqli_fetch_array($result))
{
echo"<tr>";
echo"<td align=center>";
echo $row["OrderNo."];
echo "<br>";
echo"</td>";
echo "<td align=center>";
echo "<a href='delete.php?del=";
echo $row['OrderNo.'];
echo "'>delete</a>";
echo "<br>";
echo"</td>";
echo"</tr>";
}
echo"</table>";
?>
The following php script is executed when a delete link is clicked! when only the query for the delete function was executed it worked but after insertion of another query to transfer the values to 'transfer' table was written a syntax
error occured.
<?php
include("includes/db.php");
if( isset($_GET['del']) )
{
$id = $_GET['del'];
$sql1="INSERT INTO transfer CompletedNo SELECT (OrderNo.) FROM `order` WHERE `OrderNo.` = '$id' ";
$res1= mysqli_query($db,$sql1) or die("Failed".mysqli_error($db));
$sql2= "DELETE FROM `order` WHERE `OrderNo.` = '$id' ";
$res2= mysqli_query($db,$sql2) or die("Failed".mysqli_error($db));
}
?>
please help me to correct it the error is only on line
$sql1="INSERT INTO transfer CompletedNo SELECT (OrderNo.) FROM `order` WHERE `OrderNo.` = '$id' ";
You need to use Parenthesis around the Insert column list
INSERT INTO transfer (CompletedNo)
SELECT `OrderNo.` FROM `order` WHERE `OrderNo.` = '$id'
I have the following query below and I am having a hard time figuring out the best way to add in a database table to it. I want to be able to add the table forum_categories to it and just select the id from that. I'm not sure how to have two of the same column names in a query, but that is the only field I need from that table.
How can I add forum_categories to this query..
$query2 = mysqli_query($con,"SELECT * FROM forum_topics ORDER BY topic_reply_date DESC LIMIT 3")
And then fetch only the id from it and be able to output it with having the same column name?
<?php
$con = mysqli_connect("localhost", "", "", "");
$query2 = mysqli_query($con,"SELECT * FROM forum_topics ORDER BY topic_reply_date DESC LIMIT 3")
or die ("Query2 failed: %s\n".($query2->error));
$numrows2 = mysqli_num_rows($query2);
if($numrows2 > 0){
$topics .= "<table width='100%' style='border-collapse: collapse;'>";
//Change link once discussion page is made
$topics .= "<tr style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Replies</td><td width='65'
align='center'>Views</td></tr>";
$topics .= "<tr><td colspan='3'><hr /></td></tr>";
while($row2 = mysqli_fetch_assoc($query2)){
$tid = $row2['id'];
$title = $row2['topic_title'];
$views = $row2['topic_views'];
$date = $row2['topic_date'];
$creator = $row2['topic_creator'];
$topics .= "<tr><td><a href='forum_view_topic.php?tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted
by: ".$creator." on ".$date."</span></td><td align='cener'>0</td><td align='center'>".$views."</td></tr>";
$topics .= "<tr><td colspan='3'><hr /></td></tr>";
}
$topics .="</table>";
echo $topics;
} else {
echo "<p>There are no topics in this category yet.</p>";
}
?>
Here is your corrected SQL query:
SELECT t.*, c.id AS cid FROM forum_topics AS t, forum_categories AS c ORDER BY t.topic_reply_date DESC LIMIT 3
hi i already can make search. now how i want to put that data based on their role, based on this picture i want to make admin ,admin , user, user,user. need to use sort by ? i dont know.
and this my coding search . where should i put sort by ?
<?php
include 'config1.php';
if(isset($_POST['search'])){
$searchTerm = $_POST['search'];
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
echo "<table height = '30%'border='1'>";
if($count == 0)
{
echo "NO ID REGISTERED!";
}
else
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td width='5%'><b>USER ID:</b> {$row['userid']} </td>";
echo "<td width='5%'><b>USER NAME :</b> {$row['username']} </td>";
echo "<td width='5%'><b>USER EMAIL:</b> {$row['useremail']} </td>";
echo "<td width='5%'><b>USER ROLE:</b> {$row['userrole']} </td>";
echo "<td width='5%'><b>USER DIVISION:</b> {$row['userdiv']} </td>";
echo "<td width='5%'><b>USER DEPARTMENT:</b> {$row['userdepartment']} </td>";
echo "</tr>";
}
}
echo"</table>";
}
?>
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole";
Also can use ASC or DESC for displaying in ascending or descending order after ORDER BY.
eg.
Select * from users WHERE choice = 'PHP' ORDER BY id ASC;
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' order by fieldWhichYouWant";
OR
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole ASC";
or add ASC or DESC as Ascending or descending oder to sort.
Replace fieldWhichYouWant with your requirement.
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("dentalclinic") or die(mysql_error());
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE appointment SET appointmentstatusid='$_POST[appointmentstatusid]'";
mysql_query($UpdateQuery);
};
$sql = "SELECT * from appointment a join appointmentstatus s on (a.appointmentstatusid=s.appointmentstatusid) join patient p on (a.patientid=p.patientid)";
$query = mysql_query($sql) or die(mysql_error());
echo "<table border=1>
<tr>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>APPOINTMENT STATUS</th>
<th>UPDATE</th>
</tr>";
while($record = mysql_fetch_array($query)){
echo "<form action=editstatus.php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name=firstname value=".$record['firstname']."></td>";
echo "<td>"."<input type=text name=lastname value=".$record['lastname']."></td>";
echo "<td>";
$query2 = "SELECT * from appointmentstatus";
$result = mysql_query($query2);
echo "<select name=appointmentstatusid>";
while ($line = mysql_fetch_array($result)) {
echo "<option value=".$line['appointmentstatusid'].">";
echo $line['appointmentstatus'];
echo "</option>";
}
echo "</select>";
echo "</td>";
echo "<td>"."<input type=submit name=update value=update"."></td>";
echo "</tr>";
echo "</form>";
}
echo "</table>"
?>
every time i update appointmentstatusid of patient1, it affects the appointmentstatus of other patients(patient2,patient3...). i tried adding the code WHERE appointmentstatusid='$_POST[appointmentstatusid]' on the update but when i do, it wont update anymore.
You must have a valid and matching id(s) for the row(s) you're trying to affect. How you determine that is up to you. Then, do something like:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
To update a specific record, your query should have a WHERE clause specifying what to update
$UpdateQuery = "UPDATE appointment SET appointmentstatusid='$_POST[appointmentstatusid]' WHERE `appointmentid` = 1";
Please Note: passing the $_POST[appointmentstatusid] variable directly from $_POST can make your code vulnerable for sql injections. try
$appointmentstatusid = (int) mysql_real_escape_string($_POST[appointmentstatusid]);
$UpdateQuery = "UPDATE appointment SET appointmentstatusid='".appointmentstatusid ."' WHERE `appointmentid` = $id";
OR practice using database abstraction libraries.
try this on line 6
$UpdateQuery = "UPDATE appointment SET willChangeColumn = 'newValueForThatColumn' WHERE appointmentstatusid=".$_POST['appointmentstatusid'];
I have multiple tables I am joining to use in results for a second query and nesting the second results inside the first results.
I am using the following code:
$result = mysqli_query($con,"SELECT info.lotto_id, info.name, info.number_balls, info.number_bonus_balls, info.db_name, country.name_eng AS country, currency.name AS currency, currency.symbol AS symbol, next.draw_date AS next_draw, next.jackpot AS next_jackpot
FROM info
LEFT JOIN country ON info.country_id = country.id_country
LEFT JOIN currency ON info.currency_id = currency.currency_id
LEFT JOIN next ON info.lotto_id = next.lotto_id
WHERE (info.active='1')
ORDER BY next_jackpot DESC");
while($lotto = mysqli_fetch_array($result))
{
echo "<table border='0' width='600px' align='center'>";
echo "<tr>";
echo "<td>";
echo "<h1>Results for:</h1>";
echo "</td>";
echo "<td align='right'>";
echo "<p><img src='images/". $lotto['lotto_id'] ."_big.png' alt='". $lotto['name'] ." Results'/></p>";
echo "</td>";
echo "</tr>";
echo "</table>";
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
while($draw = mysqli_fetch_array($result2))
{
echo "<table class='results' align='center'>";
echo "<tr>";
$draw['display_date'] = strtotime($draw['date']);
$lotto['cols'] = $lotto['number_balls'] + $lotto['number_bonus_balls'];
echo "<td class='date' colspan='".$lotto['cols']."'>".date('D M d, Y', $draw['display_date']). "</td>";
if ($draw[jp_code] < "1")
{
echo "<td class='winner' align='center'>Jackpot Amount</td>";
}
else
{
echo "<td class='rollover' align='center'>Rollover Amount</td>";
}
It is giving me the following error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/content/95/11798395/html/results/info_mysqli.php on line 59
This relates to my results2 query. Can somebody please suggest what I am doing wrong.
Thank you.
Change:
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
to:
$result2 = mysqli_query($con, "SELECT * FROM {$lotto['db_name']} ORDER BY date DESC LIMIT 3");
if ($result === false) {
exit("Error: " . mysqli_error($con));
}