Results 1 to 9 of 9

Thread: not populating table??

  1. #1
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default not populating table??

    i know this is pretty basic, so i must be overlooking something! this is my code for a tutorial system, to add a tutorial, but it is not populating the mysql table!

    this is my code:
    PHP Code:
    <?php 

    include("include/session.php"); 

    $hostname "localhost"
    $db_user "username"
    $db_password "password";
    $database "database";
    $db_table "tutorials";


    $db mysql_connect($hostname$db_user$db_password);
    mysql_select_db($database,$db);
    ?>
    <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"> 
    <table border="1" width="100%" cellspacing="0" id="table1" style="border-width: 0px" cellpadding="0" height="16"> 
    <tr> 
    <td style="border-style: none; border-width: medium" bgcolor="#3399ff" height="14"><b> <font face="Verdana" size="1" color="#FFFFFF">Submit a tutorial </font></b></td> 
    </tr> 
    </table>
    <? 
    if ($session->logged_in

    // the user is logged in! We continue... 
    if (!$_POST['update']) 


    echo(

    <form method=\"POST\"> 
    <table width=\"100\"> 
    <tr> 
    <td align=\"left\" width=\"25%\"> 
    Name of tutorial <br> 
    </td> 
    <td align=\"left\"> 
    <input type=\"text\" size=\"25\" class='input' maxlength=\"500\" name=\"name\"></td> 
    </tr> 
    <tr> 
    <td align=\"left\" width=\"25%\"> 
    Description <br> 
    </td> 
    <td align=\"left\"> 
    <input type=\"text\" size=\"30\" class='input' maxlength=\"500\" name=\"desc\"></td> 
    </tr> 
    <tr> 
    <td align=\"left\" width=\"25%\"> 
    catagory<br> 
    </td> 
    <td align=\"left\"> 
    <select name=\"type\" class='button'> 
    <option value=\"PHP\">PHP</option> 
    <option value=\"HTML\">HTML</option> 
    <option value=\"Photoshop\">Photoshop</option> 
    <option value=\"CSS\">CSS</option> 
    <option value=\"Fireworks\">Fireworks</option> 
    <option value=\"MySQL\">MySQL</option> 
    <option value=\"Flash\">Flash</option> 
    <option value=\"Javascript\">Javascript</option> 
    <option value=\"Tips\">Site tips 'n' tricks</option> 
    </select></td> 
    </tr> 
    <tr> 
    <td align=\"left\" width=\"25%\"> 
    Tutorial<br></td> 
    <td align=\"left\"> 
    <textarea name=\"tutorial\" rows='15' class='input' cols='50'>Tutorial here</textarea></td> 
    </tr> 
    <td align=\"left\"> 
    <input type=\"submit\" name=\"update\" class='button' value=\"Submit my tutorial!\"></td> 
    </tr> 
    </table> 
    </form> 
    "
    ); 

    else 


    mysql_query("INSERT INTO tutorials(name,desc,type,tutorial,approved,views) values ('".mysql_real_escape_string(stripslashes($_REQUEST['name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['desc']))."','".mysql_real_escape_string(stripslashes($_REQUEST['type']))."','".mysql_real_escape_string(stripslashes($_REQUEST['tutorial']))."',0,0)");

    echo(
    "Thank you $session->username, Your tutorial has been posted for admin review. Check back later to see if it has been approved!"); 
     

    }
    else 

    // They aren't logged in! 
    echo ("Sorry, you MUST be logged in to see this page!"); 

    ?>
    any ideas?
    Last edited by liamallan; 07-20-2010 at 04:52 AM.

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    What do you mean? Is it not showing the table to input the info or is it not INSERTing the data into the mysql table. I see you're using $_REQUEST and not $_POST when inserting that might be it. I find it usually easier to define the POST variables before INSERT so you don't have to concatenate in the string.

  3. #3
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    i have tried using $_POST, and defining the variables, but still get same error message:
    Code:
    ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,type,tutorial,approved,views) values ('jghjhgj','jghjhgh','PHP','Tutorial h' at line 1

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's VERY hard to read that code with all of the functions inside the query. It's much easier to read it if you take the time to use $var = mysql_real_escape_string(....; before the query then $var within the query. This will help you see the problem as well.
    Please, when posting, make the code as easy to read as possible so that it is easy for us to help you. And that means you'll get more help as well.
    In the future for question about MySQL like this, it's best to post the generated query (echo it and cut/paste here), rather than the PHP (or maybe in addition to it) so we know what is generated, since it's hard to tell from the PHP alone.

    Regardless, I believe I found the error.

    You are missing a space between your table name and the open parenthesis for the list. Just a typo, and it should work.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    nah, it wasnt the space, still getting same error. the correct values are showing in the error message but i also have an id field in my table which isnt mentioned in the query, do u think that could cause something like this?

    p.s. i also echoed the variables, which worked fine

  6. #6
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Isn't values in your string supposed to be VALUES

  7. #7
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Thumbs up

    its ok, i got it. thanks for ur help guys.

    it seems, all i was missing was the id.
    here is what i got now:
    PHP Code:
    $id ''
    $name $_POST['name'];
    $tutorial $_POST['tutorial'];
    $approved '';
    $type $_POST['type']; 
    $desc $_POST['desc'];
    $views ''


    mysql_query("INSERT INTO tutorials ( `id` , `name` , `tutorial` , `approved` , `type` , `desc` , `views` ) VALUES ('$id', '$name', '$tutorial', '$approved', '$type', '$desc', '$views')"); 

  8. #8
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    I find it odd sometimes when that is the solution cause I have never included the $id in my INSERT and it works everytime. Must be some different settings on the server.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It depends on the configuration of the database: if it is a required and non auto-generating field, then that will generate an error. If not, it is not required. Always, of course, the values and fields listed in the query must match though, but you can omit both for a field that is not required.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •