Jump to content

Hmm seemed to work for other pages....


Go to solution Solved by androidd,

Recommended Posts

Ok so more on my lil comic app... (and all was going so well)

 

GOAL: Trying to take a value from a form and create sql statement to look for that comic title in the DB and show what has been entered. (I.E search for Spider-Man -> Shows all Rows that Pertain to Spider-Man)

 

So I can't figure out what is wrong with my code. Soon as I enter in my while statement my php failes however my query seems to be doing just fine. I've used the same kind of template on other pages and it works just fine the only difference is when I added a WHERE statement to my sql query.

      * NOTE: If I take the output of my query and plug it into mysql workbench I get the correct tables and results so I'm yeah kinda confused.

      * NOTE: The way I retrieve the $data variable is from a query from the form page which pulls all the comic names from the DB and displays in a drop down so the values that it passes are the exact same   names / titles from the DB itself.

<?php
require('./scripts/con_db.php');

// Query DB for Comic_Names //

$data = $_POST['comic_name'];
$query = "SELECT * FROM `comic_db`.`comic_db` WHERE `comic_name`='$data'";
$com_sql = mysqli_query($comic_connect, $query);
if (!$com_sql){
        echo "Failed to connect to db" . mysqli_errorno();
        }
?>
<body>
<p align="center"><img src="./images/comic_front.png" /></p>
<table align="center">
<tr>
<?php
while($row = mysqli_fetch_array($com_sql))
        {
        echo "<td>" .$row "</td>";
        }
?>
</tr>
</table>
<br />
<br />
<?php
require('./scripts/footer.php');
?>

I should also say i've tried these other passes as well with no luck :

<?php
while($row = mysqli_fetch_assoc($com_sql))
        {
        echo "<td>" .$row "</td>";
        }
?>

while($row = mysqli_fetch_assoc($com_sql))
        {
        echo "<td>" .$row['comic_name'] "</td>";
        }
?>

<?php
while($row = mysqli_fetch_assoc($com_sql))
        {
        echo "<td>" .$row['$data'] "</td>";
        }
?>

I've also tried something I found in a book that says to do extract($row) then do my echo "<td>" .$row "</td>"; but that didn't work either so I'm missing something

 

here is what the output for the SELECT string gives me as well from the browser which seems to be fine and again if I plug into mysql workbench it displays what I need..

SELECT * FROM `comic_db`.`comic_db` WHERE `comic_name`='Iron Man - V5'

TY for help guys I've try to keep the ??'s down but I'm missing something (probably pretty obvious) so I need some guidence.

 

Link to comment
https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/
Share on other sites

sounds like you need to put the $row into a while loop like

while ($row=mysql_fetch_assoc(________________)){

//output table rows here

}

Thought that was what I had in most of those examples or a variant. Could you explain what you mean a lil more maybe I'm missing it. or show me with the code provided.

 

$query = "SELECT * FROM `comic_db`.`comic_db` WHERE `comic_name`='$data'";

 

Take out the first comic_db and the period.

 

Nope usually I call the scheme then the table just cause I have multiple tables I use so I figured it's easier to make a global connection to the DB then when I need something from the DB Tables I could jump from one to the other without the need to make different connections.. Hence the `comic_db`.`comic_db` <--- also this works when I call the DB comic names to fill out the form as well as the page that does the INSERT statement that inputs the comic variables. So while I see what your saying didn't help in this situation. TY though. 

First of all you are trying to echo out an array. What do you get if you echo $row[0]

 

Second, your understanding of extract() is not correct. The extract() function works as follows

$array = array(
	'foo' => 'Hello world',
	'bar' => 'Foo bar'
);

extract($array);

echo $foo; // Returns Hello world
echo $bar; // Returns Foo bar

you probably have a non-printing character as part of your submitted form data that doesn't match anything when the query runs in the php code but when you copy/paste the echoed query statement, that non-printing character is stripped out.

 

what is your code that is building the form?

you probably have a non-printing character as part of your submitted form data that doesn't match anything when the query runs in the php code but when you copy/paste the echoed query statement, that non-printing character is stripped out.

 

what is your code that is building the form?

 

This is the page that queries the db for all the publishers in the collection then puts them in a drop down then submits to the form (OP coding) to query db to find all the rows for the desired publisher.

// Query DB for Publisher Info //


$query = "SELECT * FROM `comic_info_db`.`comic_name`";
$result = mysqli_query($comic_connect, $query);

?>
<table height="10%">
        <tr>
        <td> </td>
        </tr>
</table>
<p align="center"><img src="/images/comic_front.png" /></p>
<form action="./comic_results.php" method="POST">
<table height=10% align="center">
         <tr>
                <td> Comic Name: </td>
                <td>
                        <select name="comic_name">
                        <option name="Null" value="--">--</option>
                                <?php
                        while($row = mysqli_fetch_array($result))
                        {
                        echo "<option>" . $row['comic_name'] . "</option>\n";
                        }
                                ?>
                        </select>
                </td>
                <td><input name="submit" value="submit" type="submit" /></td>
        </tr>
</table>
</form>

your SELECT query for building the form is not using the same database as your query in the form processing code.

 

yeah it pulls the publishers out of comic_info_db.comic_info and then it posts the data into comic_db.comic_db

 

- As I get new publishers I wanted to add into db and it auto populate my forms.

- The search would pull the exact publisher data that has been inputted into the data base and then post into the main comic database and the query would ask that table what publishers or titles I currently own.

 

I did it this way also so that when I ask the DB what info it has it will have as part of the search the exact string that is contained in the DB

should I be running the $_POST['comic_name'] through anything? Not sure if maybe when it asks the db for the info the search query passes the spaces and dashes as something else? I don't know how to ask this in google lol so I don't know if make sense sry.

yeah I guess I should explain better

 

MySQL DB Info:

----------------------------------

|comic_info_db.comic_info | < ------------- This houses all the publishers and comic names (ex  Marvel,  Spider-Man)

----------------------------------

---------------------------

|comic_db.comic_db | <-------------- Houses all current comics in the collection (This is where I'm trying to get results from)

---------------------------

PhP Pages

-----------------

|Comic_form  |   <----|

Comic_input  |    <---|---------------------------------This group takes info and inputs it into the comic_db.comic_db as I get more comics (Works)   

------------------

---------------------

| Comic_Search |  <------ (Problem Area) * Part of the form queries comic_info_db.comic_info to get the comic names

| Comic_Results | <------- (Problem Area) Shows the search results that is queried from comic_db.comic_db

----------------------

 

So basically what should be happening is I go to store get new comic goto comic_form input all info about comic (Publisher, Comic Name, Comic Number, Misc Info etc..) then input the data into db and then time goes by I go into comic search and select from a drop down (That was populated from the db) and outputs all the comics it finds in the DB pertaining to what was selected in drop down. IE. drop down menu selects Superior Spider-Man and shows results.

 

I don't want to type my values and possible mess up so I already added all the publishers and all the comic names into the db those values are what populate in the drop downs, I did this to make sure I didn't have to deal with Spider Man, Spider-Man, spider man, spider-Man, etc.... it would show only the data that was in DB and search for that data exactly as it exists in the DB.

 

What I posted in the posts a couple before this shows a query to comic_info_db.comic_info to get the pub to make drop down then that form sends it to the OP form to process results. so it's a circular thing

 

hope this helps explain the structure of what I'm trying to do... Sorry so long.

  • Solution
<?php
while ($row = mysqli_fetch_assoc($com_sql)){
        print "<tr>\n";
        foreach ($row as $col=>$val){
                print " <td>$val</td>\n";
        }
        print "</tr>\n\n";
}
?>

 

This is what worked. Found it in a book PhP5/MySQL Programming for Absolute Beginner (which I am ) XD

 

 

Thanks for all the help everyone!

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.