www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: A long script that needs to be checked...

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    A long script that needs to be checked...

    I wrote a script that inserts information submitted from a form into the database. Attached is the form. I checked the script twice, but I can't find any mistakes. I'm sure the script has several errors because I always get the following error:
    Code:
    Fatal error: Maximum execution time of 30 seconds exceeded in /home/linksku1/public_html/wp-admin/process.php on line 144
    PHP Code:
    <?php
    //includes the WordPress functions
    include ('../wp-blog-header.php');
    //loads phpBB stuff
    define('IN_PHPBB'true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH '../forum/';
    $phpEx substr(strrchr(__FILE__'.'), 1);
    include(
    $phpbb_root_path 'common.' $phpEx);

    $user->session_begin();
    $auth->acl($user->data);
    $user->setup();

    if (
    $user->user_id == anonymous) {
    wp_redirect('http://linksku.com/forum/ucp.php?mode=login');
    }

    $title =        $_POST['post_title'];
    $url =            $_POST['post_url'];
    $content =        $_POST['post_content'];
    $tag =            $_POST['post_tag'];
    $category =        $_POST['post_category'];
    if ( 
    $_POST['post_slug'] ) 
    $slug =            $_POST['post_slug'];
    $author =        $_POST['post_author'];
    if ( 
    $_POST['option_comment'] ) 
    $option_comment =    $_POST['option_comment'];
    else
    $option_comment =    'closed';

    if ( !isset( 
    $title,$content,$tag,$category,$tag,$author )) {
    wp_redirect('http://linksku.com/wp-admin/new-post.php?missing=yes');
    }

    //Slug
    if (!isset($slug)) {
    $slug strtolower($title);
    $slug preg_replace('/(\s)+/',' ',$slug);
    $slug preg_replace('/\s/','-',$slug);
    $slug preg_replace('/(a)|(an)|(and)|(are)|(as)|(at)|(be)|(by)|(com)|(for)|(from)|(i)|(in)|(is)|(it)|(of)|(on)|(or)|(that)|(the)|(this)|(to)|(was)|(what)|(when)|(where)|(who)|(will)|(with)|(you)|(your)/i','',$slug);
    }
    $slug mysql_real_escape_string($slug);

    //Title
    if (preg_match('/^(what)|(how)|(why)|(should)|(could)|(would)|(if)|(can)/i',$title)) {
    $title ucfirst($title);
    }
    $title mysql_real_escape_string($title);

    //URL
    if (!preg_match('/^(http:\/\/)|(https:\/\/)/i',$url)) {
    $url 'http://' $url;
    }
    if (
    $url=='http://'){
    $url '';
    }
    $url preg_replace('/&/','&amp;',$url);
    $url mysql_real_escape_string($url);

    //Main Content
    $content preg_replace('/</','&lt;',$content);
    $content preg_replace('/>/','&gt;',$content);
    $content mysql_real_escape_string($content);

    //Tags
    $tags preg_replace('/^,/','',$tags);
    $tags preg_replace('/,$/','',$tags);
    $tags $tags ',';
    $num_tags preg_replace('/[^,]+/','',$tags);
    $num_tags strlen($num_tags);
    $each_tag = array();
    while( 
    $num_tags 

       
    $each_tag[] = preg_replace('/,.*/','',$tags);
       
    $tags preg_replace('/^[^,]+/','',$tags);
       
    $tag substr($tags,2,25);
       
    $num_tags $num_tags 1;
    }
    $tags $each_tag;

    //Category
    switch($category) {
    case 
    1:
        
    $category 'technology';
        break;
    case 
    2:
        
    $category 'entertainment';
        break;
    case 
    3:
        
    $category 'sports';
        break;
    case 
    4:
        
    $category 'news';
        break;
    case 
    5:
        
    $category 'science';
        break;    
    case 
    6:
        
    $category 'lifestyle';
        break;    
    case 
    7:
        
    $category 'hobbies';
        break;    
    case 
    8:
        
    $category 'jokes';
        break;
    case 
    9:
        
    $category 'others';
        break;
    case 
    10:
        
    $category 'links';
        break;
    }

    //Date & Time
    $year =            date('Y');
    $month =        date('m');
    $day =            date('d');
    $hour =            date('H');
    $minute =        date('i');
    $second =        date('s');

    $gmt_year =        gmdate('Y');
    $gmt_month =        gmdate('m');
    $gmt_day =        gmdate('d');
    $gmt_hour =        gmdate('H');
    $gmt_minute =        gmdate('i');
    $gmt_second =        gmdate('s');

    $post_date sprintf('%d-%d-%d %d:%d:%d',$year,$month,$day,$hour,$minute,$second);
    $post_date_gmt sprintf('%d-%d-%d %d:%d:%d',$year,$month,$day,$hour,$minute,$second);


    $con mysql_connect('localhost','user','pass');
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
      
    //Slug
    $num 1;
    mysql_select_db("linksku1_wrdp1"$con);
    while(
    mysql_query("SELECT post_title FROM wp_posts WHERE post_name='" $slug "'")){
    preg_replace('/\d$/','',$slug,1);
    $slug $slug $num;
    $num $num 1;
    }
    //Permalink
    $guid 'http://linksku.com/' $month '/' $slug;

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_posts (post_author,post_date,post_date_gmt,post_content,post_title,comment_status,post_name,post_modified,post_modified_gmt,guid,link,cat)
    VALUES (
    $author,$post_date,$post_date_gmt,$content,$title,$option_comment,$slug,$post_date,$post_date_gmt,$guid,$url,$category)");

    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT ID FROM wp_posts ORDER BY ID desc limit 1");
    $row mysql_fetch_array($result);
    $ID $row['ID'];

    foreach ( 
    $tag as $tag_name )
    {
    $tag_name mysql_real_escape_string($tag_name);
    mysql_select_db("linksku1_wrdp1"$con);
    if (
    mysql_query("SELECT name FROM wp_terms WHERE name='" $tag_name "'"))
    {

    //term_id
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_id FROM wp_terms WHERE name='" $tag_name "'");
    $row mysql_fetch_array($result);
    $term_id $row['term_id'];

    //term_taxonomy
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy='post_tag' AND term_id='" $term_id "'");
    $row mysql_fetch_array($result);
    $term_taxonomy_id $row['term_taxonomy_id'];

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("UPDATE wp_term_taxonomy SET count=count+1
    WHERE term_taxonomy_id=
    $term_taxonomy_id");

    //term_relationship
    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_term_relationships
    VALUES (
    $ID,$term_taxonomy_id,0)");
    }
    else
    {
    //term_taxonomy
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_taxonomy_id FROM wp_term_relationships ORDER BY term_taxonomy_id desc limit 1");
    $row mysql_fetch_array($result);
    $term_taxonomy_id $row['term_taxonomy_id'];

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_term_relationships
    VALUES (
    $ID,$term_taxonomy_id,0)");

    //term_id
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_id FROM wp_term_taxonomy ORDER BY term_id desc limit 1");
    $row mysql_fetch_array($result);
    $term_id $row['term_id'];

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_term_taxonomy
    VALUES (
    $term_taxonomy_id,$term_id,'post_tag','',0,1)");

    //term
    $tag_slug preg_replace('/\s/','-',$tag_name);
    $tag_slug strtolower($tag_slug);
    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_terms
    VALUES (
    $term_id,$tag_name,$tag_slug,0)");
    }
    }

    mysql_close($con);
    unset(
    $con);
    wp_redirect($guid);
    ?>
    I think the error lies in this statement:
    PHP Code:
    mysql_query("SELECT post_title FROM wp_posts WHERE post_name='" $slug "'"
    Do I have to include the apostrophes?


    You don't have to read through everything. Just let me know of any errors if you see one.
    Attached Images Attached Images
    Last edited by narutodude000; 07-11-2010 at 09:51 PM.

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Maybe some Basic Debugging 101 ideas might help you debug your code.

  3. #3
    Join Date
    Mar 2010
    Posts
    701
    PHP Code:
    <?php                                                                                                                                                                                                                                                                   
    include ('../wp-blog-header.php');
    define('IN_PHPBB'true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH '../forum/';
    $phpEx substr(strrchr(__FILE__'.'), 1);
    include(
    $phpbb_root_path 'common.' $phpEx);

    // Start session management
    $user->session_begin();
    $auth->acl($user->data);
    $user->setup();

    if (
    $user->user_id == anonymous) {
    wp_redirect('http://linksku.com/forum/ucp.php?mode=login');
    }

    $title =        $_POST['post_title'];
    $url =            $_POST['post_url'];
    $content =        $_POST['post_content'];
    $tag =            $_POST['post_tag'];
    $category =        $_POST['post_category'];
    if ( 
    $_POST['post_slug'] ) 
    $slug =            $_POST['post_slug'];
    $author =        $_POST['post_author'];
    if ( 
    $_POST['option_comment'] ) 
    $option_comment =    $_POST['option_comment'];
    else
    $option_comment =    'closed';

    if ( !isset( 
    $title,$content,$tag,$category,$tag,$author )) {
    wp_redirect('http://linksku.com/wp-admin/new-post.php?missing=yes');
    }

    //Slug
    if (!isset($slug)) {
    $slug strtolower($title);
    $slug preg_replace('/(\s)+/',' ',$slug);
    $slug preg_replace('/\s/','-',$slug);
    $slug preg_replace('/(a)|(an)|(and)|(are)|(as)|(at)|(be)|(by)|(com)|(for)|(from)|(i)|(in)|(is)|(it)|(of)|(on)|(or)|(that)|(the)|(this)|(to)|(was)|(what)|(when)|(where)|(who)|(will)|(with)|(you)|(your)/i','',$slug);
    }
    $slug mysql_real_escape_string($slug);

    //Title
    if (preg_match('/^(what)|(how)|(why)|(should)|(could)|(would)|(if)|(can)/i',$title)) {
    $title ucfirst($title);
    }
    $title mysql_real_escape_string($title);

    //URL
    if (!preg_match('/^(http:\/\/)|(https:\/\/)/i',$url)) {
    $url 'http://' $url;
    }
    if (
    $url=='http://'){
    $url '';
    }
    $url preg_replace('/&/','&amp;',$url);
    $url mysql_real_escape_string($url);

    //Main Content
    $content preg_replace('/</','&lt;',$content);
    $content preg_replace('/>/','&gt;',$content);
    $content mysql_real_escape_string($content);

    //Tags
    $tag_name preg_replace('/^,/','',$tags);
    $tag_name preg_replace('/,$/','',$tag_name);
    $tag_name $tags ',';
    $num_tags preg_replace('/[^,]+/','',$tag_name);
    $num_tags strlen($num_tags);
    $tag = array();
    while( 
    $num_tags 

       
    $tag[] = preg_replace('/,.*/','',$tag_name);
       
    $tag_name preg_replace('/^[^,]+/','',$tag_name);
       
    $tag_name substr($tag_name,2,25);
       
    $num_tags $num_tags 1;
    }

    //Category
    switch($category) {
    case 
    1:
        
    $category 'technology';
        break;
    case 
    2:
        
    $category 'entertainment';
        break;
    case 
    3:
        
    $category 'sports';
        break;
    case 
    4:
        
    $category 'news';
        break;
    case 
    5:
        
    $category 'science';
        break;    
    case 
    6:
        
    $category 'lifestyle';
        break;    
    case 
    7:
        
    $category 'hobbies';
        break;    
    case 
    8:
        
    $category 'jokes';
        break;
    case 
    9:
        
    $category 'others';
        break;
    case 
    10:
        
    $category 'links';
        break;
    }

    //Date & Time
    $year =            date('Y');
    $month =        date('m');
    $day =            date('d');
    $hour =            date('H');
    $minute =        date('i');
    $second =        date('s');

    $gmt_year =        gmdate('Y');
    $gmt_month =        gmdate('m');
    $gmt_day =        gmdate('d');
    $gmt_hour =        gmdate('H');
    $gmt_minute =        gmdate('i');
    $gmt_second =        gmdate('s');

    $post_date sprintf('%d-%d-%d %d:%d:%d',$year,$month,$day,$hour,$minute,$second);
    $post_date_gmt sprintf('%d-%d-%d %d:%d:%d',$year,$month,$day,$hour,$minute,$second);


    $con mysql_connect('localhost','user','pass');
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
      
    //Slug
    $num 1;
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT post_name FROM wp_posts WHERE post_name='$slug'");
    $row mysql_fetch_array($result);
    while(
    $row['post_name'] == $slug){
    preg_replace('/\d$/','',$slug,1);
    $slug $slug $num;
    $num $num 1;
    }
    //Permalink
    $guid 'http://linksku.com/' $month '/' $slug;

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_posts (post_author,post_date,post_date_gmt,post_content,post_title,comment_status,post_name,post_modified,post_modified_gmt,guid,link,cat)
    VALUES (
    $author,$post_date,$post_date_gmt,$content,$title,$option_comment,$slug,$post_date,$post_date_gmt,$guid,$url,$category)");

    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT ID FROM wp_posts ORDER BY ID desc limit 1");
    $row mysql_fetch_array($result);
    $ID $row['ID'];

    foreach ( 
    $tag as $tag_name )
    {
    $tag_name mysql_real_escape_string($tag_name);
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT name FROM wp_terms WHERE name='$tag_name'");
    $row mysql_fetch_array($result);

    if (!isset(
    $row['name']))
    {
    //term_taxonomy
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_taxonomy_id FROM wp_term_relationships ORDER BY term_taxonomy_id desc limit 1");
    $row mysql_fetch_array($result);
    $term_taxonomy_id $row['term_taxonomy_id'];

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_term_relationships
    VALUES (
    $ID,$term_taxonomy_id,0)");

    //term_id
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_id FROM wp_term_taxonomy ORDER BY term_id desc limit 1");
    $row mysql_fetch_array($result);
    $term_id $row['term_id'];

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_term_taxonomy
    VALUES (
    $term_taxonomy_id,$term_id,'post_tag','',0,1)");

    //term
    $tag_slug preg_replace('/\s/','-',$tag_name);
    $tag_slug strtolower($tag_slug);
    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_terms
    VALUES (
    $term_id,$tag_name,$tag_slug,0)");
    }
    else
    {
    //term_id
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_id FROM wp_terms WHERE name='$tag_name'");
    $row mysql_fetch_array($result);
    $term_id $row['term_id'];

    //term_taxonomy
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy='post_tag' AND term_id='$term_id'");
    $row mysql_fetch_array($result);
    $term_taxonomy_id $row['term_taxonomy_id'];

    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("UPDATE wp_term_taxonomy SET count=count+1
    WHERE term_taxonomy_id='
    $term_taxonomy_id'");

    //term_relationship
    mysql_select_db("linksku1_wrdp1"$con);
    mysql_query("INSERT INTO wp_term_relationships
    VALUES (
    $ID,$term_taxonomy_id,0)");
    }
    }

    mysql_close($con);
    unset(
    $con);
    wp_redirect($guid);
    ?>
    I found a few mistakes, but I still cannot get this to work. I think the INSERT INTO isn't even working.

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    I think you have a little confusion here too:
    $row = mysql_fetch_array($result);
    while($row['post_name'] == $slug){
    Usually you have the while statement advance the result row. The way it probably doesn't work the way you'd expect (or it might enter an endless loop depending on the values).

    I'm also seeing several lines like:
    mysql_select_db("linksku1_wrdp1", $con);
    If you aren't changing databases you only need to call that function once when you connect to the database initially.

    This line:
    $result = mysql_query("SELECT ID FROM wp_posts ORDER BY ID desc limit 1");
    Makes me think you are actually looking for "mysql_insert_id()" which returns the auto_increment value for the last inserted row.

    You also have some major loopage with multiple queries. I'd recommend writing out the logic with pencil first to make sure your plan is most efficient. It is much harder to "fix" something with flawed or inefficient logic than it is to find the correct syntax.

  5. #5
    Join Date
    Mar 2010
    Posts
    701
    Thanks criterion9, I fixed the last 2 errors. But I don't understand what you meant about the first error.
    PHP Code:
    $num 1;
    mysql_select_db("linksku1_wrdp1"$con);
    $result mysql_query("SELECT post_name FROM wp_posts WHERE post_name='$slug'");
    $row mysql_fetch_array($result);
    while(
    $row['post_name'] == $slug){
    preg_replace('/\d$/','',$slug,1);
    $slug $slug $num;
    $num $num 1;

    The slug is the URL friendly version of the post title. In the WordPress table, the column is called post_name. The above script (supposedly) creates a unique slug by adding incremented numbers after the slug.

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Theoretically this will only return a single row, correct?
    "SELECT post_name FROM wp_posts WHERE post_name='$slug'"
    Then there is no need for a while loop. A simple if statement checking the returned rows would suffice. (This does not re-execute the query so if you already had renamed a slug you would have 2 with the same number at the end).

    If you used LIKE rather than '=' and added a % at the end you would then have a list of posts. This would require you to advance the index within the while loop rather than prior to it.

    Does that make sense?

  7. #7
    Join Date
    Mar 2010
    Posts
    701
    Would this work?
    PHP Code:
    $num 1;
    //this means that it will loop infinitely, right? 
    while(1){
    $result mysql_query("SELECT post_name FROM wp_posts WHERE post_name='$slug'");
    //if the slug is already used
    if($row mysql_fetch_assoc($result)){
    //this is useless during the first loop
    preg_replace('/\d$/','',$slug,1);
    $slug $slug $num;
    $num $num 1;
    }
    else
    {
    break;
    }


  8. #8
    Join Date
    Mar 2010
    Posts
    701
    I also realized that this part doesn't work:
    PHP Code:
    if ( !isset( $title,$content,$tag,$category,$author )) {
    wp_redirect('http://linksku.com/wp-admin/new-post.php?missing=yes');

    I changed it to the following, but it still doesn't work.
    PHP Code:
    if ( !isset( $title ) | !isset( $content ) | !isset( $tag ) | !isset( $category ) | !isset( $author )) {
    wp_redirect('http://linksku.com/wp-admin/new-post.php?missing=yes');


  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    All "|" should be "||".

  10. #10
    Join Date
    Mar 2010
    Posts
    701
    Thanks, I fixed it.

    I removed the redirect at the end so that I can debug more easily. I'm going to echo variables throughout the script and see what happens.

  11. #11
    Join Date
    Mar 2010
    Posts
    701
    I got a error here:
    PHP Code:
    mysql_query("INSERT INTO wp_posts (post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,comment_status,post_name,post_modified,post_modified_gmt,guid,link,cat)
    VALUES (
    $author,$post_date,$post_date_gmt,$content,$title,'',$option_comment,$slug,$post_date,$post_date_gmt,$guid,$url,$category)") or die(mysql_error()); 
    Code:
    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 '2010-07-12 20:51:09,2010-07-12 20:51:09,content,subject,'',open,slug,2010-07-12 ' at line 2
    The only possible error that I can think of is that there's a auto-incremented ID for each post. Do I have to insert it?

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    What format is your column expecting for the dates?

  13. #13
    Join Date
    Mar 2010
    Posts
    701
    The type is datetime. Default is 0000-00-00 00:00:00 (which is also the structure).

  14. #14
    Join Date
    Jan 2009
    Posts
    3,346
    Try adding single quotes around the dates then.

  15. #15
    Join Date
    Mar 2010
    Posts
    701
    Code:
    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 ''2010-07-12 21:55:38','2010-07-12 21:55:38',content,subject,'',open,slug,'2010-0' at line 2

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