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
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 . "'"))
{
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)");
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.
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.
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.
$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; } }
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?
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
Bookmarks