michael b
michael b

Reputation: 169

Database query returns 0 rows even though two entries are present

I'm uploading some files to a database and I would like to have a PHP form that can retrieve the files in question when a serial code is entered. I have two entries in the database, yet my PHP form keeps returning it as 0 rows. I appreciate any help offered. Once again, thanks for the help as I try to join the ranks of those who know php.

The form always returns: "No Data Found. Please check your serial code to ensure that you have not incorrectly entered it. If the code is correct please email the website administrator for further assistance" as if there are no rows.

<?php
if( $_POST )
{
$username="st*****";
$password="*****";
    $con = mysqli_connect("storycodes.db.10339998.hostedresource.com",$username,$password);

    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysqli_select_db($con, "storycodes");

$code = $_POST['codeInput'];
$code = mysqli_escape_string($con, htmlspecialchars($code)); //May not acually need htmlspecialchars
$query = "SELECT story,video FROM `storycodes` WHERE `code` = 'code'";
$result = mysqli_query($con, $query);

  if (mysqli_num_rows($result)) 
  {
    $row = mysqli_fetch_assoc($result);
    mysqli_free_result($result); 
    extract($row);
    echo $story . $video;
  }
   else 
  {
   echo "No Data Found. Please check your serial code to ensure that you have not incorrectly entered it. If the code is correct please email the website administrator for further assistance";
  }         


mysqli_close($con);

}
?>

Database entries: the columns are (if this helps at all): code, email, video, and story

b2348-5dfae-73c0c-57685 s**@yahoo.com ../story_files/story.txt

90a93-785e4-03cad-a18d5 w*@**.com ../video_files/story.txt ../story_files/story.txt

code is being posted by this form:

<link href="/CSS/CSS.css" rel="stylesheet" type="text/css">

<p align="center"><span class="linkText"><a href="/index.html">Home</a> <a href="/contact-us.php">Contact Us</a> <a href="/payments.html">Payments</a></span></p>
<p align="center">&nbsp;</p>
<p align="center"><span class="headingText"><img alt="legendmaker - makes legends: banner" width="728" height="90" /></span></p>
<p align="center">&nbsp;</p>
<div align="center" class="headingText">Enter Your Serial Code Below To Continue Your Adventure!</div>

<p>&nbsp;</p>
<form name="form1" method="post" action="/scripts/stories.php">
  <label>
  <div align="center"><span class="formText">Your Serial Code:
    <input name="codeInput" type="text" id="codeInput" size="23" maxlength="23">
  </span></div>
  </label>
  <div align="center"><span class="formText">
  </span></div>
  <span class="formText"><label> 
  <div align="center"><br>
  </div>
  </label>
  </span>
  <label>
  <div align="center"><br>
      <input type="submit" name="submit" id="submit" value="Submit">
  </div>
  </label>
</form>

<p>&nbsp;</p>
<p class="headingText">&nbsp;</p>
<p align="center" class="headingText">Can't find your code?</p>
<p align="center" class="paragraphText">Request an email with your code below.</p>
<form name="form2" method="post" action="/scripts/code_request.php">
  <label class="formText">
  <div align="center">Email:
    <input type="text" name="email" id="email">
  </div>
  </label>
  <p align="center">
    <label>
    <input type="submit" name="submit2" id="submit2" value="Submit">
    </label>
  </p>
</form>
<p>&nbsp;</p>

NEVER MIND about the HTML, thanks a lot for the help. I got all mixed up!

Upvotes: 0

Views: 176

Answers (2)

schieben
schieben

Reputation: 78

$query = "SELECT story,video FROM `storycodes` WHERE `code` = 'code'";

The $ is absent.

Try this:

$query = "SELECT story,video FROM `storycodes` WHERE `code` = '$code'";

Or this:

$query = "SELECT story,video FROM `storycodes` WHERE `code` = '".$code."'";

Upvotes: 1

Floris
Floris

Reputation: 46375

$query = "SELECT story,video FROM `storycodes` WHERE `code` = 'code'";

Where are you referencing your variable $code in this line?....

Hint - the $ sign matters...

Upvotes: 1

Related Questions