insert query and then displaying data...frustrating issue

Discussion in 'PHP' started by absentx, Feb 3, 2011.

  1. #1
    Alright I am just stuck.

    Basically I have an admin portion of my website where I can insert products. There are two scripts, one for a certain type of widget and a second scrip for another type of widget.

    I will post the least amount of code that I think will help diagnose the problem.

    Script one is quite simple, it just takes the form data and pops it in the database. The information can then be viewed in a catalog page on the site where it pulls each product by the url_name (lets not argue about why it isn't product id right now):

            
    $query = "INSERT INTO mo (prod_cat,product_name,product_description,url_name,product_code,package_sizes,image,technical,aff) 
    VALUES ('$type','$prodname', '$description','$url','$code','$comma_separated','$picture','$tech','$aff')";
            mysqli_query($dbc, $query);
    
    PHP:
    Script one works fine and everything is viewable on the catalog page perfectly!

    Script two is a little more indepth because you enter the info in the form along with multiple product numbers, then it executes a foreach loop and enters one product in the database for each product number:

    $values = explode("\n", $_POST['widget_numbers']);
    	
    	foreach ($values as $widget_number) {
    		 $widget_prodname = $prodname ." ". $widget_number;
    		 $widget_url = $url.$widget_number;
      		 $widget_code = $widget_number;
    		 $widget_aff = $widget_number ."-".$aff;
    		 $widget_picture = $widget_number.".jpg";
    		 $query = "INSERT INTO mo (prod_cat,product_name,product_description,url_name,product_code,package_sizes,image,technical,aff) VALUES ('$type','$widget_prodname', '$description','$widget_url','$widget_code','$comma_separated','$widget_picture','$tech','$widget_aff')";
            mysqli_query($dbc, $query);
    		
    		
       
    
    	}
    
    PHP:
    You'll have to excuse some of my redundancy, I am quite new at this and putting this together and the syntax is still something I am really trying to get better at. Basically as you can see I need to tag the product number onto the ends of some of the other fields.

    Now here is the issue with script two:

    1. Everything gets entered into the database as it should...ie proper columns, proper data..perfect!
    2. When I go to view one of the products entered with script two on the catalog page it is not pulling anything from the database!! It is like it isn't finding anything.

    Now, here is the debugging I have done.

    1. If I change even ONE CHARACTER in any column of the database in a row of something entered with script two, it all of the sudden works fine and displays on the catalog page!
    2. If I enter just '$url' in the url_name field rather than the ' $filter_url ' (which is the url with a product code at the end) it also works fine.

    So I have it narrowed to some problem with what is going in the url_name field (since that is how my catalog page pulls the info) or some type of wierd character differences that I am not aware of.

    Any help appreciated!
     
    Last edited: Feb 3, 2011
    absentx, Feb 3, 2011 IP
  2. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Where are you calling this variable from?...

    $values = explode("\n", $_POST['filter_numbers']);

    Are you posting an array?
     
    moads, Feb 3, 2011 IP
  3. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah it is from a text area...and it is $widget_numbers that was a typo in the original post

    
    <label for="widget_numbers"class="label1">Widget Numbers:</label>
    <textarea name="widget_numbers" cols="20" rows="5"></textarea>
    
    HTML:
     
    absentx, Feb 3, 2011 IP
  4. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    UPDATE on this.

    Okay so yesterday I ran the script to install eighty new products in the database...again all fields show up in the database fine, as verified through phpmyadmin

    But they still weren't showing up right on the display page.

    So I checked all eighty products in the table and then clicked the edit button to perform a mass edit...I changed nothing on any of them...then hit go.

    Then all of them showed up fine.

    Wierd!! what am I missing!?
     
    absentx, Feb 4, 2011 IP
  5. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Do we need to see more code? What can I do to get some help? I still can't get this to work!
     
    absentx, Feb 6, 2011 IP
  6. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    echo $query; then copy that query into phpmyadmin and tell us what happens. Does that change anything?
     
    ThomasTwen, Feb 6, 2011 IP
  7. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Excellent idea...Let me try it out and I will be back with an answer shortly!
     
    absentx, Feb 6, 2011 IP
  8. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Okay so if I echo the query and then copy it into mysql, it enters everything properly and the information displays as it should on the catalog page!
     
    absentx, Feb 7, 2011 IP
  9. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This problem might have to do with the character set of the connection between your PHP server and your mySQL server. The code below will set the charset of your connection to the charset of your mySQL database. You have to run it after mysql_connect() and before mysql_query() - it only has to be run once.

    $db_charset = mysql_query( "SHOW VARIABLES LIKE 'character_set_database'" );
    $charset_row = mysql_fetch_assoc( $db_charset );
    mysql_query( "SET NAMES '" . $charset_row['Value'] . "'" );
    unset( $db_charset, $charset_row );


    If that doesnt help (which is quite possible), have a look at your script and how it outputs the contents. The problem is there then. Try to write a new very short and simple script that outputs the database contents.
     
    ThomasTwen, Feb 7, 2011 IP
  10. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well I figured out the problem, but I am not quite sure why this mattered.

    all of the variables that were inserted with the query used mysqli_real_escape_string($dbc, trim($_POST['whatever'])); but for some reason I did not have the url variable running through mysqli_real_escape_string...once I made sure the url variable also went through mysqli_real_escape_string then it worked.
     
    absentx, Feb 7, 2011 IP