www.webdeveloper.com
Results 1 to 6 of 6

Thread: INSERT INTO query problem, site launches < 24 hrs!! Help!

  1. #1
    Join Date
    May 2007
    Posts
    5

    INSERT INTO query problem, site launches < 24 hrs!! Help!

    Hello,

    I can't see a problem in this script but I keep getting insert query failed errors. Here it is, let me know if you see anything wrong, certain items are marked with ****** for security reasons:

    PHP Code:
    <?php
    ############################
    ##   CONNECTION STRING    ##
    ############################

    include '*****connection info******';

    $dbconnection mysql_connect($dbhost,$dbuser,$dbpassword);
    $db mysql_select_db($database,$dbconnection);

    $email $_POST['email'];

    $email substr($email070);

    if(
    ereg("^[-_.A-Za-z0-9]+@([-_A-Za-z0-9]+\.)+([-_A-Za-z0-9]+)$",$email))
    {
        
    $query "INSERT INTO mailinglist (email) VALUES ('$email')";
        
    mysql_query($query) or die('Error, insert query failed');
        
    header("Location: http://www.nancygoldmanart.com/mailinglistsuccess.php");
        return;
    }
    else
    {
        
    header("Location: http://www.nancygoldmanart.com/mailinglisterror.php");
    }
    ?>
    Go to http://www.nancygoldmanart.com and type in an e-mail address and hit submit in order to see the error. Any help is greatly appreciated. This site launches in about 24 hours! Thanks!

  2. #2
    Join Date
    Apr 2007
    Location
    Bourgas, Bulgaria
    Posts
    10
    it's not the same as your's, but working.
    PHP Code:
    <?php
        $mysql 
    mysql_connect('localhost','root','');
        
    $base mysql_select_db('test');
        
            
    $email $_POST['email'];
            
    $email substr($email070); 
            if(isset(
    $_POST['add'])) {
                if(
    eregi("^[-_.A-Za-z0-9]+@([-_A-Za-z0-9]+\.)+([-_A-Za-z0-9]+)$",$_POST['email']))
                {
                
    $insert mysql_query("INSERT INTO `email` (`email`) VALUES ('$email')");
                if(
    $insert == "true") {
                print 
    "u'r email has been inserted";
                }
                }
                else {
                print 
    "error, e-mail has not been inserted";
                }
                }
            else {
    ?>
    <form action="" method="post">
    email:<br/>
    <input type="text" name="email">
    <input type="submit" name="add" value="add">
    </form>
    <?php
    }
    ?>

  3. #3
    Join Date
    Sep 2006
    Location
    Europe
    Posts
    174
    A handy addition to the mysql : query or die(error message) is the php mysql_error function
    http://uk2.php.net/mysql_error
    mysql_query($query) or die('Error, insert query failed <br />'.mysql_error());

    Obviously don't do this on a live site as any error messages are a rich source of clues to design flaws/hacking opportunities.

  4. #4
    Join Date
    May 2007
    Posts
    5
    Here is my new code, I am not getting an error which makes sense cuz i took it out, i am just getting a blank page and the contents of the form still arent being added to the database. I tried mysql_error() and it did not return anything.
    any ideas?
    PHP Code:
    <?php
    ############################
    ##   CONNECTION STRING    ##
    ############################

    include '******';

    $connection mysql_connect($host,$user,$password);
    $db mysql_select_db($database,$connection);

    $email $_POST['email'];

    $email substr($email070);

    if(isset(
    $_POST['Submit'])) { 
                if(
    eregi("^[-_.A-Za-z0-9]+@([-_A-Za-z0-9]+\.)+([-_A-Za-z0-9]+)$",$_POST['email'])) 
                { 
                
    $insert mysql_query("INSERT INTO `mailinglist` (`email`) VALUES ('$email')"); 
                if(
    $insert == "true") { 
        
    header("Location: http://www.nancygoldmanart.com/mailinglistsuccess.php");
                }
                }  
                else { 
        
    header("Location: http://www.nancygoldmanart.com/mailinglisterror.php");     
               } 
               }
    ?>

  5. #5
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    379
    It is likely an error with your database connection as your code seems to be correct. When connecting to the database and inserting into the database, add the mysql_error() function to see what error it gives you.
    The beauty of code is in its simplicity.

  6. #6
    Join Date
    May 2007
    Posts
    5
    I resolved the problem, it was a problem in my database, I had an id column which for some reason was set default to 0 and it was the primary key, every time I would try to make an entry, it wouldn't make it because it would be a duplicate id. I took out the id column and set the email column as the primary key.....problem solved! Thanks for the help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles