Jump to content

$query returning no database results


randal_138

Recommended Posts

Hello all,

 

Trying to figure out how to display database results so that they are numbered (for editing and deletion purposes).  I want to be able to edit the message text and update the database information with the UPDATE command, but have not gotten that far yet.

 

Current problem is that I am returning no results.  Here is my script:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My Profile</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>My Profile </h1>
<a href="member-index.php">Home</a> | <a href="member-profile.php">My Profile</a> | Update Posts | <a href="logout.php">Logout</a>

<br /><br />

<?php
$subject = $_POST['subject'];
$message_text = $_POST['message_text'];

//Connect to mysql server
$link = mysql_connect('XXXXXX', 'XXXXXX', 'XXXXXX');
if(!$link)
{
   die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db('ryan_iframe');
if(!$db)
{
   die("Unable to select database");
}

// validate incoming values
$subject = (isset($_GET['subject'])) ? (int)$_GET['subject'] : 0;
$message_text = (isset($_GET['message_text'])) ? (int)$_GET['message_text'] : 0;

ob_start();

$id = $_GET['SUBJECT'];
$query = sprintf( "
SELECT
   SUBJECT, MSG_TEXT, UNIX_TIMESTAMP(MSG_DATE) AS MSG_DATE
FROM
   FORUM_MESSAGE
WHERE
   SUBJECT = '$id'
ORDER BY
   MSG_DATE DESC",
   DB_DATABASE,
   DB_DATABASE,
   $subject,
   $subject);
$result = mysql_query($query) or die(mysql_error());
$num = mysql_numrows($result);
mysql_close();

$i = 0;
while ($i < $num)
{
   $subject = mysql_result($result, $i, "SUBJECT");
   $message_text = mysql_result($result, $i, "MSG_TEXT");
   
   echo '<div style="width: 400px;padding:20px;">';
   echo '<table border=0 width="400px">';
   echo '<tr>';
   echo '<td style="vertical-align:top;width:auto;">';
   echo 'Date: ';
   echo '</td>';
   echo '<td style="vertical-align:top;width:320px;">';
   echo date('F d, Y', $row['MSG_DATE']) . '</td>';
   echo '</tr>';
   echo '<tr>';
   echo '<td style="vertical-align:top;width:auto;">';
   echo 'Subject: ';
   echo '</td>';
   echo '<td style="vertical-align:top;width:320px;">';
   echo '<div>' . htmlspecialchars($row['SUBJECT']) . '</div>';
   echo '</td>';
   echo '</tr>';
   echo '<tr>';
   echo '<td style="vertical-align:top;width:auto;">';
   echo 'Message: ';
   echo '</td>';
   echo '<td style="vertical-align:top;width:320px;">';
   echo '<div>' . htmlspecialchars($row['MSG_TEXT']) . '</div>';
   echo '</td>';
   echo '</tr>';
   echo '<tr>';
   echo '<td style="vertical-align:top;width:auto;">';
   echo '</td>';
   echo '<td style="vertical-align:top;width:320px;text-align:center;">';
   echo '<form method="post">';
   echo '<input type="hidden" name="update" value="true" />';
   echo '<input type="submit" value="Update" />';
   echo '         ';
   echo '<input type="hidden" name="delete" value="true" />';
   echo '<input type="submit" value="Delete" />';
   echo '</form>';
   echo '</td>';
   echo '</tr>';
   echo '</table>';
   echo '<br />';
   echo '<hr />';
   echo '</div>';
   
      ++$i;
}

?>

</body>
</html>

 

Thanks in advance.

 

Ryan

Link to comment
Share on other sites

It shows my html coding, but the PHP is gone and not querying the database. 

 

Here is the code that works, but does not incorporate the $i listing element:

 

<?php
$subject = $_POST['subject'];
$message_text = $_POST['message_text'];

//Connect to mysql server
$link = mysql_connect('localhost', 'ryan_ryan', 'nealon');
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db('ryan_iframe');
if(!$db)
{
die("Unable to select database");
}

// validate incoming values
$subject = (isset($_GET['subject'])) ? (int)$_GET['subject'] : 0;
$message_text = (isset($_GET['message_text'])) ? (int)$_GET['message_text'] : 0;

ob_start();

$query = sprintf('
SELECT
SUBJECT, MSG_TEXT, UNIX_TIMESTAMP(MSG_DATE) AS MSG_DATE
FROM
FORUM_MESSAGE
WHERE
SUBJECT = %d OR
MSG_TEXT = %d
ORDER BY
MSG_DATE DESC',
DB_DATABASE,
DB_DATABASE,
$subject,
$subject);

$result = mysql_query($query) or die(mysql_error());
$num=mysql_numrows($result);

while ($row = mysql_fetch_assoc($result))
{
echo '<div style="width: 400px;padding:20px;">';
echo '<table border=0 width="400px">';
echo '<tr>';
echo '<td style="vertical-align:top;width:auto;">';
echo 'Date: ';
echo '</td>';
echo '<td style="vertical-align:top;width:320px;">';
echo date('F d, Y', $row['MSG_DATE']) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="vertical-align:top;width:auto;">';
echo 'Subject: ';
echo '</td>';
echo '<td style="vertical-align:top;width:320px;">';
echo '<div>' . htmlspecialchars($row['SUBJECT']) . '</div>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="vertical-align:top;width:auto;">';
echo 'Message: ';
echo '</td>';
echo '<td style="vertical-align:top;width:320px;">';
echo '<div>' . htmlspecialchars($row['MSG_TEXT']) . '</div>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="vertical-align:top;width:auto;">';
echo '</td>';
echo '<td style="vertical-align:top;width:320px;text-align:center;">';
echo '<form method="post">';
echo '<input type="hidden" name="update" value="true" />';
echo '<input type="submit" value="Update" />';
echo '         ';
echo '<input type="hidden" name="delete" value="true" />';
echo '<input type="submit" value="Delete" />';
echo '</form>';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br />';
echo '<hr />';
echo '</div>';
}
mysql_free_result($result);

?>

 

When I incorporate the $i = 0 and other script to create rows/lists, it no longer queries the database.  Check my initial post for the scripting that does not work.

 

Thanks again!

 

Ryan

Link to comment
Share on other sites

So what your saying is that I can only do one?  The problem is that I want to pull up the message_text associated with the subject and modify that.  Can't I just assign numerical values to rows with rows containing the subject, message_text, delete button and edit button?  That way I can delete a post with one button and query the DB with the other post so that I can use the UPDATE script?

 

Hope I explained this so that you understand.

 

Thanks again.

 

Ryan

Link to comment
Share on other sites

No, that isn't what I'm saying. Following the logic in your code, these lines will take the incoming form values and assign the values to the $subject and $message_text variables.

$subject = $_POST['subject'];
$message_text = $_POST['message_text'];

 

Then further along in the code, these lines override that assignment, and assign the the variables either the integer value of $_GET['subject'] and $_GET['message_text'] OR zero.

$subject = (isset($_GET['subject'])) ? (int)$_GET['subject'] : 0;
$message_text = (isset($_GET['message_text'])) ? (int)$_GET['message_text'] : 0;

 

So, the reason I asked if both $subject and $message_text are expected to be numeric is that in the script, they will be numeric, even though  by the names $subject and $message_text they sound as though they should be strings.

Link to comment
Share on other sites

There is progess.  I had some nice syntax errors that I had to fix in order for the results to display.  The $i function is working, but now I am having some more coding errors.  Easier stuff this time.  I can't delete the row from the table.  The code is very short and is as follows:

 

<?php

if($_POST['delete']) // from button name="delete"
{
for($i=0;$i<$count;$i++)
{
	$del_id = $i;
	$sql = "DELETE FROM FORUM_MESSAGE WHERE id='$del_id'";
	$result = mysql_query($sql) or die (mysql_error());
}

if($result)
{
	header('Location: message-deleted.php');
}
}

?>

 

Thanks again!

 

Ryan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.