PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 12th, 2004, 11:01 AM
nmphpwebdev nmphpwebdev is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 6 nmphpwebdev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Insert query doesn't work

I am having trouble getting this simple insert query to work.

What small thing am i over looking??

PHP Code:
<?php

$content_title 
'Home Page Administration';
$page_title 'Index Page Options';

//include( '../includes/adminheader.html' );
include_once( '../includes/dbconnect.php' );

if( isset( 
$_POST['action']) == 'Submit Info' ) { //Handle the form

// Create a function for escaping the data.
        
function escape_data ($data) {
                global 
$dbc// Need the connection.
                
if (ini_get('magic_quotes_gpc')) {
                        
$data stripslashes($data);
                }
                return 
mysql_real_escape_string($data);
        } 
// End of function.

//Create a blank variable
$message NULL;

//Check for content to be submitted.
if( empty( $_POST['content'] )) {
        
$c FALSE;
        
$message .='<p><font color="red">You can\'t display a blank home page!!</font></p>';
        } else {
                
$c escape_date$_POST['content'] );
                }

//Check for an image to be posted.
if( empty( $_POST['img'] )) {
     
$i FALSE;
     
$message .='<p><font color="red">You can\'t have a missing image</font></p>';
     } else {
     
$i $_POST['img'];
             }

//Check for img description
if( empty( $_POST['imgdesc'] )) {
        
$imgdesc FALSE;
        
$message .='<p><font color="red">We need a description for this image!!</font></p>';
        } else {
                
$imgdesc escape_data$_POST['imgdesc'] );
                }

//Check for date modified date
if ((preg_match("/(\d{1,2})-(\d{1,2})-(\d{2,4})/"$_POST['datemod']))) {

$timepieces explode("-"$_POST['datemod']);
$month $timepieces[0];
$day $timepieces[1];
$year $timepieces[2];

$dm date("Y-m-d"mktime(000$month$day$year));

} else {

if  ((
preg_match("/(\d{1,2})\/(\d{1,2})\/(\d{2,4})/"$_POST['datemod']))) {

$timepieces explode("/"$_POST['datemod']);
$month $timepieces[0];
$day $timepieces[1];
$year $timepieces[2];

$dm date("Y-m-d"mktime(000$month$day$year));

} else {

$message .= "When do you want this to show as the index page?";

}
}

if( 
$c && $i && $dm && $imgdesc )
    {
             
$sql "INSERT INTO indexpage
             ( id , content , datemod , img , imgdesc , imgupload_date )
             VALUES
             ( \'\', \'$c\', \'$dm\', \'$i\', \'$imgdesc\', \'$now()\' )"
;
              
$result = @mysql_query$sql );
            
printf ("Updated records: %d\n"mysql_affected_rows());
             print( 
$sql );
            echo 
'<p>Your changes have been added</p>';

            } else {
                   echo 
"<p>The changes could not be submitted: ' .   mysql_error() . ' </p>";
                   }
}
//Print the messge if there is one
if( isset( $message )) {
        echo 
'<font color=\"red\">' $message '</font>';
        }
?>
<form action="adminhome.php" method="POST">
 <table border="1" width="100%">
        <tr>
            <td valign="top">Enter content to be displayed on home page:</td>
            <td><textarea cols="25" rows="10" name="content"></textarea></td>
            </tr>
        <tr>
            <td>Is there an image to be displayed?</td>
            <td><input type="file" name="img" /></td>
         </tr>
         <tr>
             <td>Image description<br /><small>What will be printed for the "ALT" tag</small></td>
             <td><input type="text" name="imgdesc" /></td>
         </tr>
         <tr>
             <td>Date Added:</td>
             <td><input type="text" name="datemod" /></td>
         </tr>
         <tr>
             <td align="center" colspan="2">&nbsp;</td>
             </tr>
            <tr>
                <td colspan="2">
                    <div align="center"><input type="submit" name"action" value="Submit Info" /></div>
            </td>
        </tr>
    </table>
 </form>
<?php
include( '../includes/footer.html' );
?>

Reply With Quote
  #2  
Old October 12th, 2004, 12:00 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 330 Viper_SB User rank is Private First Class (20 - 50 Reputation Level)Viper_SB User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 4 h 51 m 6 sec
Reputation Power: 5
I guess you mean the only query on that page.
What is the problem? Does the query not run? Does the query give errors?

One thing I noticed was your query you have extra backslashes, not needed how you're doing it.
PHP Code:
 $sql "INSERT INTO indexpage
( id , content , datemod , img , imgdesc , imgupload_date )
VALUES
( \'\', \'$c\', \'$dm\', \'$i\', \'$imgdesc\', \'$now()\' )"
;

// should be
$sql "INSERT INTO indexpage
(content, datemod, img, imgdesc, imgupload_date)
VALUES
('$c', '$dm', '$i', '$imgdesc', now())"
;
// NOTE I removed id because you aren't setting it to anything, it isn't needed as it should autoincrement (if set) 

Reply With Quote
  #3  
Old October 12th, 2004, 12:09 PM
nmphpwebdev nmphpwebdev is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 6 nmphpwebdev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Viper_SB
I guess you mean the only query on that page.
What is the problem? Does the query not run? Does the query give errors?

One thing I noticed was your query you have extra backslashes, not needed how you're doing it.
PHP Code:
 $sql "INSERT INTO indexpage
( id , content , datemod , img , imgdesc , imgupload_date )
VALUES
( \'\', \'$c\', \'$dm\', \'$i\', \'$imgdesc\', \'$now()\' )"
;

// should be
$sql "INSERT INTO indexpage
(content, datemod, img, imgdesc, imgupload_date)
VALUES
('$c', '$dm', '$i', '$imgdesc', now())"
;
// NOTE I removed id because you aren't setting it to anything, it isn't needed as it should autoincrement (if set) 


sorry about the bogus extra quotes. I saw those after i posted and removed them.

i am not getting any errors from the query. i know the query is good, i matched against another insert query that i have that does almost the same thing.

I am not event getting the print statement to display in the browser.

This is my updated query:
PHP Code:
if( $c && $i && $dm && $imgdesc )
    {
             
$sql "INSERT INTO indexpage
             ( content , datemod , img , imgdesc , datetodisplay )
             VALUES
             ( '$c', now(), '$i', '$imgdesc', '$dm' )"
;
              
$result = @mysql_query$sql );

            echo 
'<p>Your changes have been added</p>';
            print ( 
$sql );
            } else {
                   echo 
"<p>The changes could not be submitted: ' .   mysql_error() . ' </p>";
                   }


Reply With Quote
  #4  
Old October 12th, 2004, 12:23 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 330 Viper_SB User rank is Private First Class (20 - 50 Reputation Level)Viper_SB User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 4 h 51 m 6 sec
Reputation Power: 5
It's most likly one of your variables that isn't being set. i.e $c $i $dm or $mgdesc
Make sure each of them is valid, the only thing I see if that $dm isn't being set if
you get to $message .= "When do you want this to show as the index page?";

Reply With Quote
  #5  
Old October 12th, 2004, 12:40 PM
nmphpwebdev nmphpwebdev is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 6 nmphpwebdev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry

I ran through the variable line by line, reordered them in my insert statement, and still nothing.

Reply With Quote
  #6  
Old October 12th, 2004, 12:46 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 330 Viper_SB User rank is Private First Class (20 - 50 Reputation Level)Viper_SB User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 4 h 51 m 6 sec
Reputation Power: 5
use a print or echo and find out where your script is running i.e right above
if( $c && $i && $dm && $imgdesc )
put
print 'goes here<br>';
and right inside it put
print 'goes inside<br>';

then you can see if your code is getting inside that if statement, if it is then something else is wrong, if it's not then that is the problem, if it doesn't reach your first if then something else is wrong.

Reply With Quote
  #7  
Old October 15th, 2004, 03:19 AM
daedalus daedalus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 daedalus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:


if( $c && $i && $dm && $imgdesc )
{
$sql = "INSERT INTO indexpage
( content , datemod , img , imgdesc , datetodisplay )
VALUES
( '$c', now(), '$i', '$imgdesc', '$dm' )"
;
$result = @mysql_query( $sql );

echo
'<p>Your changes have been added</p>';
print (
$sql );
} else {
echo
"<p>The changes could not be submitted: ' . mysql_error() . ' </p>";
}
}



The mysql_error() is only called when there's actually no query done ...

It should look more like this:
if( $c && $i && $dm && $imgdesc )
{
$sql = "INSERT INTO indexpage
( content , datemod , img , imgdesc , datetodisplay )
VALUES
( '$c', now(), '$i', '$imgdesc', '$dm' )"
;
$result = @mysql_query( $sql ) or die('Error: '.mysql_error());

echo
'<p>Your changes have been added</p>';
print (
$sql );
} else {
// one of these ($c$i $dm $imgdesc) is not set...

}
}


Then you should get the mysql_error() .... if there's no error, this mean that one of your variables doesn't pass the "if" test ... try to echo them to see what they hold

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Insert query doesn't work


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway