Results 1 to 7 of 7

Thread: PHP Error

  1. #1
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default PHP Error

    Hey guys...

    I've been getting a "Column count doesn't match value count at row 1" error.

    I'm not sure why it's happening.

    The php is below:
    PHP Code:
    <?php
    if(isset($_POST['add']))
    {
    include 
    'config.php';
    include 
    'opendb.php';

    $name $_POST['name'];
    $company $_POST['company'];
    $street $_POST['street'];
    $apt $_POST['apt'];
    $city $_POST['city'];
    $state $_POST['state'];
    $zip $_POST['zip'];
    $phone $_POST['phone'];
    $ext $_POST['ext'];
    $email $_POST['email'];
    $type $_POST['type'];
    $template $_POST['template'];
    $web1 $_POST['web1'];
    $web2 $_POST['web2'];
    $web3 $_POST['web3'];
    $web4 $_POST['web4'];
    $web5 $_POST['web5'];
    $cms $_POST['cms'];
    $sother $_POST['sother'];
    $othertext $_POST['othertext'];
    $programming $_POST['programming'];
    $domain $_POST['domain'];
    $hosting $_POST['hosting'];
    $management $_POST['management'];
    $seo $_POST['seo'];
    $ses $_POST['ses'];
    $marketing $_POST['marketing'];
    $budget $_POST['budget'];
    $date $_POST['date'];
    $month $_POST['month'];
    $year $_POST['year'];


    $query "INSERT INTO custom (id, name, company, street, apt, city, state, zip, phone, ext, email, type, template, web1, web2, web3, web4, web5, cms, sother, othertext, programming, domain, hosting, management, seo, ses, marketing, budget, date, month, year)
              VALUES ('','
    ${name}','${company}','${street}','${city}','${state}','${zip}''${phone}','${ext}','${email}','${template}','${web1}','${web2}','${web3}','${web4}','${web5}','${cms}','${sother}','${othertext}','${programming}','${domain}','${hosting}','${management}','${seo}','${ses}','${marketing}','${budget}','${date}','${month}','${year}')";
    mysql_query($query) or die(mysql_error());  

    $query "FLUSH PRIVILEGES";
    mysql_query($query) or die('Error, insert query failed');

    include 
    'closedb.php';
    echo 
    "Values Added";
    }
    else
    {
    ?>

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ouch, that's a lot of redundant code!
    Code:
    <?php 
      if(isset($_POST['add'])) { 
        include 'config.php'; 
        include 'opendb.php';
    
        $fields = array('name', 'company', 'street', 'apt', 'city', 'state',
          'zip', 'phone', 'ext', 'email', 'type', 'template', 'web1', 'web2',
          'web3', 'web4', 'web5', 'cms', 'sother', 'othertext', 'programming',
          'domain', 'hosting', 'management', 'seo', 'ses', 'marketing', 'budget',
          'date', 'month', 'year');
        $afields = array();
    
        for($i = 0; $i < count($fields); ++$i)
          $afields[$fields[$i]] = mysql_real_escape_string($_POST[$fields[$i]]);
    
        mysql_query(sprintf(
          'INSERT INTO custom (&#37;s) VALUES (\'%s\')',
          implode(', ', $fields),
          implode('\', \'', array_values($afields))
        )) or die(mysql_error());
    
        mysql_query('FLUSH PRIVILEGES') or die('Error, insert query failed');
    
        include 'closedb.php';
        echo 'Values added';
      } else {
    ?>
    Last edited by Twey; 07-16-2007 at 07:12 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Thanks Twey!

    If you don't mind, could you explain the code a bit. I'm just starting out with PHP and would like to understand what you just did there.

    Also, what exactly was causing the error before?

    Lastly, how can I make it so that the PHP sends me an email whenever this form is submitted?

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

    Default

    I'll save Twey a bit of work.

    And I'll add a mail function in too.

    PHP Code:
    <?php 
      
    if(isset($_POST['add'])) { //if adding data
        
    include 'config.php'//include setup
        
    include 'opendb.php'//open db

        
    $fields = array('name''company''street''apt''city''state',
          
    'zip''phone''ext''email''type''template''web1''web2',
          
    'web3''web4''web5''cms''sother''othertext''programming',
          
    'domain''hosting''management''seo''ses''marketing''budget',
          
    'date''month''year'); //save desired POST vars
        
    $afields = array(); //setup a blank array for use later

        
    for($i 0$i count($fields); ++$i)
        
    //set $i to 0, repeat while $i< the number of fields, add one to $i each loop.
          
    $afields[$fields[$i]] = mysql_real_escape_string($_POST[$fields[$i]]);
           
    //now use the empty array to store each of the fields
           //while escaping the values of the POST vars for safe use in the database
           //if this wasn't done, then you could be hacked, using info in the POST vars
          //such as $_POST['field'] = "value; DELETE * FROM `mytable`";
        
    mysql_query(sprintf//see note below
          
    'INSERT INTO custom (%s) VALUES (\'%s\')',
          
    implode(', '$fields),
          
    implode('\', \''array_values($afields))
        )) or die(
    mysql_error());
    //insert the values into the database, or, if that fails, stop the script and display the error
        
    mysql_query('FLUSH PRIVILEGES') or die('Error, insert query failed');
    //end access
        
    include 'closedb.php'//close connection
        
    echo 'Values added'//display confirm
      
    } else { //otherwise do whatever is after the code here..
    ?>
    Note: sprintf() is a bit confusing, so you can see the page here:
    http://www.php.net/manual/en/function.sprintf.php
    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
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Thanks djr33.

    I'm a little confused. Where is the mail function in all of that?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I was wondering the same...
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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

    Default

    I got distracted, commenting everything.

    Here's the code for that:
    PHP Code:
    ...
        include 
    'closedb.php'//close connection
    mail('your@email.com','Subject-- like Added!','Body content here, including $_POST['var'] if you want, but add them correctly to the string',"From: auto@mailer.com\r\nReply-to: auto@mailer.com\r\nMore headers"); 
    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
  •