Unable to display post titles with SQL Tables - mysql

I'm trying to display a list of titles in a modify/delete page. They load fine since I get the correct amount of rows, put for some reason I can't the text printed.
Here's the function which corresponds to the issue.
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
And this is the full code:
<?php
class blog {
private $host;
private $username;
private $password;
private $db;
private $link;
public function __construct($host, $username, $password, $db){
$this->db = $db;
$this->link = mysql_connect($host, $username, $password, $db);
mysql_select_db($this->db, $this->link) or die (mysql_error());
}
function get_content($id=''){
if($id !=NULL):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE id = '$id'";
$return = '<p>Vover al Indice</p>';
else:
$sql = "SELECT * FROM content ORDER by id DESC";
endif;
$res = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($res) !=NULL):
while($row = mysql_fetch_assoc($res)){
echo '<h1>'.$row['title'].'</h1>';
echo '<p>'.$row['body'].'</p>';
}
else:
echo '<p>Oops, post not found!</p>';
endif;
if(isset($return)){
echo $return;
}
}
function add_content($p){
$title = mysql_real_escape_string($p['title']);
$body = mysql_real_escape_string($p['body']);
if(!$title OR !$body):
if(!$title):
echo "<p>You have to fill the title.</p>";
endif;
if(!$body):
echo "<p>You have to fill the body.</p>";
endif;
echo '<p>Try again!</p>';
else:
$sql = "INSERT INTO content VALUES (null, '$title', '$body')";
$res = mysql_query($sql) OR DIE (mysql_error());
echo "Added sucessfully!";
endif;
}
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
}// End of Class
?>

You're forgetting an echo in your manage_content() function:
<?php $row['title'] ?>
Should be:
<?php echo $row['title']; ?>
That's part of the problem anyway.

Related

Trying to echo a table but it's not working

The table is called bookings. I'm trying to echo rows from the table, but I'm getting errors in some places according to user_id. Dreamweaver tells me the last few lines have errors, but I'm not sure what's the problem.
<?php
require 'connect.php';
include("auth.php");
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id =". $uid;
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>"Room_name"</th>
<th>"booking_date"</th>
<th>"period_name"</th>
<th>"booking_id"</th>
<th>"Cancel Booking"</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo "</tr>";
echo "</table>";
}
else echo '<p>You have no bookings.</p>';
I have made some change
And here is the code-
require 'connect.php';
include 'auth.php';
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id ='". $uid."'";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>Room_name</th>
<th>booking_date</th>
<th>period_name</th>
<th>booking_id</th>
<th>Cancel Booking</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo '</tr>';
}
echo '</table>';
} else echo '<p>You have no bookings</p>';

exclude status in dropdown list

I'm trying to exclude all values where status is Defective in my mobo table, but this code doesn't work.
It's still showing everything in my Dropdown
<?php
$conn = new mysqli('localhost', 'root', 'admin2018', 'inventory')
or die ('Cannot connect to db');
$result = $conn->query("select mobo, status FROM mobo WHERE NOT 'status = Defective'");
echo "<select name='mobo'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['mobo'];
$name = $row['mobo'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>
nevermind I already fix it using
<?php
$conn = new mysqli('localhost', 'root', 'admin2018', 'inventory')
or die ('Cannot connect to db');
$result = $conn->query("select mobo, status FROM mobo WHERE status!='Defective'");
echo "<select name='mobo'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['mobo'];
$name = $row['mobo'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>

Working script stop working (php,mysql)

I've got a weird problem.
I created a website on localhost. Everything was working fine. I could connect to my mysql db without any problems.
I decided to put it on external server to show it to my friends. I changed $username, $dbname etc.
Then I decided to work little with the website back on localhost. And then it started. When I run my script which adds new records to my db (name, message, email) this shows up.
No database selected
Everything is just the same it was before I decided to put it on the server. What can be the problem?
EDIT: Here's the code of connect.php
<?php
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = new mysqli("$host", "$username", "$password", "$table");
if (!$conn) {
die("Nie udało się połączyć z bazą danych: ".mysqli_connect_error());
}
and here's a script implemented into index.php
<?php
include 'connect.php';
$sql = "SELECT s_id, name, s_like, s_dislike, message FROM suggestions ORDER BY rand()";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$s_id = $row["s_id"];
$s_like = $row["s_like"];
$s_dislike = $row["s_dislike"];
echo '<p>' . $row["message"] . '</p>';
echo '<h1 class="podpis">' . $row["name"] . '</h1>';
mysqli_close($conn);
?>
and here's the cde of comments.php where I see the message about No database selected.
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = mysqli_connect($host, $username, $password, $table);
$query = "INSERT INTO suggestions (name, email, message) VALUES ('$name',
'$email', '$message')";
if (mysql_query($query)) {
echo 'Dodano sugestię!';
header('Location: index.php');
} else{ die(mysql_error());}
mysql_close();
?>

i created a table in database to display photos and i made it to display ,but i want them to display from the last to the first

<?php
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("imagestore",$conn);
if(!$db ){
echo mysql_error();
}
$q = "SELECT * FROM imagetable";
$r = mysql_query("$q",$conn);
if($r)
{
while($row=mysql_fetch_array($r) )
{
header("Content-type: text/html");
echo "</br>";
echo $row['photoname'];
echo "</br>";
$type = "Content-type: ".$row['phototype'];
header($type);
echo "<img src=image.php?fotoid=". $row['fotoid']."width =300 height = 35. 300/>";
}
}
else{
echo mysql_error();
}
?>
I know ORDER by but is not working to display photos as i want in my page .
I am a beginner .
I used $q = "SELECT * FROM imagetable ORDER BY fotoid DESC";

PHP - MySQL - Delete Row

I am not too sure what i am doing wrong. i am trying to delete the entire row with this code but it is not working. No error is happening it prints the line that it was deleted but when i go and have a look it is not working. Any thoughts?
<?
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("theobse1_scores", $con);
$sql="DELETE FROM times WHERE id='$id'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record deleted go back to delete another!";
mysql_close($con)
?>
You used $order instead of your query variable $sql
$sql="DELETE FROM times WHERE id='$id'";
mysql_query($sql);
I got it working using this code!
<?php
$id =$_REQUEST['id'];
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
// sending query
mysql_query("DELETE FROM times WHERE id = '$id'")
or die(mysql_error());
?>
else
{
$qry = "SELECT * FROM my_login WHERE email = '".$email."' LIMIT 1";
$res = mysql_query($qry);
if(mysql_num_rows($res) > 0)
{
echo "Email already exists!";
}
else
{
$qry="INSERT INTO my_login SET name='$name',city='$city',comment='$comt',password='$pass',email='$email'";
mysql_query($qry);
}
}
}
?>
delete.php
<?php
include "connect.php";
$id =$_REQUEST['id'];
// sending query
mysql_query("DELETE FROM utilizatori WHERE id = '$id'")
or die(mysql_error());
?>
is corect, I tested and delete from ID
and here is the button delete: `
<?Delete`?>