PHP Won't Insert Data Into Mysql Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
justinram33
Forum Newbie
Posts: 5
Joined: Fri Oct 17, 2008 10:04 pm

PHP Won't Insert Data Into Mysql Database

Post by justinram33 »

Hello, I've probably spent the last 6 hours trying to figure this one out :banghead: . I'm fairly new to PHP and i prefer to learn by looking at code, removing it parts, seeing what it changes, then reinserting it. It may be an odd way to learn, but i find it to be the best way i've found lol. Anyway here is my problem. I found this tutorial on how to make a photo gallery using PHP. I thought "what the hell" and got down to work. I have completed the tutorial and have it working fine, what i want to do now, is insert new albums, which it does not talk about in the tutorial. First off, i'll explain my Tables (sorry for the bad quality lol)

Tutorial: http://www.sitepoint.com/article/php-ga ... m-minutes/

-------------------------------------------------------
| Field Name | Field Type |
-------------------------------------------------------
| category_id | Integer |
-------------------------------------------------------
| category_name | Text |
-------------------------------------------------------

Here is my "add_category.html":

Code: Select all

 <html><body>    <form type="POST" action="/photo1/add_category.php">    <table>    <tr>    <td>        Name: <input type="text" name="cateogry_name" />     </td>    <td>        <input type="submit" />    </td>    </tr>    </table>    </form></body></html> 
And here is my "/photo1/add_category.php":

Code: Select all

 
<?php
 
    $mysql_link = mysql_connect("localhost", "justin", "password");   
 
    mysql_select_db("gallery") or die("Could not select database");
 
mysql_query( "INSERT INTO gallery_category(`category_name`) VALUES('".addslashes( $_POST[category_name] )."' )" );
?>
 

The Way i look to see if any data was added is there is page called "preupload.php" that has you select a category... Obviously something is going on, because i used to just see "My First Gallery" (I inserted that via terminal) And now i just see blank spaces... If anyone would care to see my "preupload.php" script here is it....

Code: Select all

 
<?php
 
    include("/var/www/photo1/config.inc.php");
 
 
 
    // initialization
 
    $photo_upload_fields = "";
 
    $counter = 1;
 
 
 
    // default number of fields
 
    $number_of_fields = 5; 
 
 
 
// If you want more fields, then the call to this page should be like, 
 
// preupload.php?number_of_fields=20
 
 
 
    if( $_GET['number_of_fields'] )
 
    $number_of_fields = (int)($_GET['number_of_fields']);
 
 
 
    // Firstly Lets build the Category List
 
 
 
    $result = mysql_query( "SELECT category_id,category_name FROM gallery_category" );
 
    while( $row = mysql_fetch_array( $result ) )
 
    {
 
$photo_category_list .=<<<__HTML_END
 
    <option value="$row[0]">$row[1]</option>\n
 
__HTML_END;
 
    }
 
    mysql_free_result( $result );   
 
 
 
// Lets build the Photo Uploading fields
 
    while( $counter <= $number_of_fields )
 
    {
 
$photo_upload_fields .=<<<__HTML_END
 
<tr>
 
    <td>
 
         Photo {$counter}:
 
        <input name=' photo_filename[]' type='file' />
 
    </td>
 
</tr>
 
<tr>
 
    <td>
 
         Caption:
 
        <textarea name='photo_caption[]' cols='30' rows='1'></textarea>
 
    </td>
 
</tr>
 
__HTML_END;
 
    $counter++;
 
    }
 
 
 
// Final Output
 
echo <<<__HTML_END
 
<html>
 
<head>
 
    <title>Lets upload Photos</title>
 
</head>
 
<body>
 
<form enctype='multipart/form-data' action='upload.php' method='post' name='upload_form'>
 
<table width='90%' border='0' align='center' style='width: 90%;'>
 
<tr>
 
    <td>
 
        Select Album
 
        <select name='category'>
 
            $photo_category_list
 
        </select>
 
    </td>
 
</tr>
 
<tr>
 
    <td>
 
        <p>&nbsp;</p>
 
    </td>
 
</tr>
 
 
 
<!-Insert the photo fields here -->
 
$photo_upload_fields
 
 
 
<tr>
 
    <td>
 
            <input type='submit' name='submit' value='Add Photos' />
 
    </td>
 
</tr>
 
</table>
 
</form>
 
</body>
 
</html>
 
__HTML_END;
 
?>
 
 

Sorry for having such a vage post, But i'd very very very much appreciate any help on the matter!!!!! Again, MANY thanks!!!!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP Won't Insert Data Into Mysql Database

Post by josh »

$_POST[ foo ] should be written $_POST['foo'], you should use mysql_real_escape_string for escaping instead of addslashes. Sounds like the query is executing but inserting blank data, do you see the rows in the database ( you use phpmyadmin? ). Try putting the query in a variable and outputting it, and see if it looks right. Might just be an error on your display page too. I know how you feel ( debugging for hours )
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP Won't Insert Data Into Mysql Database

Post by JAB Creations »

Do what I do...

Set the query to a variable instead of directly executing mysql_query and then echo that variable to the browser (presuming this is on a local test machine of course).

Then after you reload the page and copy the query use phpMyAdmin's SQL tab to execute the code. You'll get a nice debug message.
killingbegin
Forum Newbie
Posts: 20
Joined: Sun Feb 08, 2009 5:07 am

Re: PHP Won't Insert Data Into Mysql Database

Post by killingbegin »

Code: Select all

 
 <?php
   $cat_name = addslashes($_POST['category_name']);
 
   $mysql_link = mysql_connect("localhost", "justin", "password");  
  
    mysql_select_db("gallery") or die("Could not select database");
 
    $strquery = "INSERT INTO gallery_category(category_name) 
       VALUES( '$cat_name' )" ;
 
    if (!mysql_query($strquery,$mysql_link))
        {
            die('Error: ' . mysql_error());
        }
    else 
        {
            echo ("Category name added with value: ".$cat_name);
        }
  mysql_close($con)
 
 
 
 
 
 
 
 ?>
 
 
 
 
 
 
 
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP Won't Insert Data Into Mysql Database

Post by josh »

Give a man a fish, he eats for a day...
justinram33
Forum Newbie
Posts: 5
Joined: Fri Oct 17, 2008 10:04 pm

Re: PHP Won't Insert Data Into Mysql Database

Post by justinram33 »

God, i feel like an idiot :banghead: . Last night i sayed up tell 2:30 trying to figure this one out and it's so easy... First, i thought, "hey, why don't i just assign a varible, and stick it in there, see if it does anything." And sure enough, it inserts it correctly, so long story short i messed up my HTML form like this...

So this is what i had it...

Code: Select all

 <html><body>    <form type="post" action="/photo1/add_category.php">         Name: <input type="text" name="test" />          <input type="submit" />     </form></body></html> 
But it isn't

Code: Select all

 <form type="post" action="/photo1/add_category.php"> 
 
It's

Code: Select all

 <form method="post" action="/photo1/add_category.php"> 
Thank for all of your help, and now i finally feel the pain of debuggin lol.
Post Reply