4, December 2008

need some help with writing to database - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: need some help with writing to database
« previous next »
Pages: [1] Print

Author Topic: need some help with writing to database  (Read 603 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« on: Jan 13, 2006, 07:00:52 PM »

problem with this code is that it only writes the url, title, and short_desc to the database, it wont write the category(s) when selected.

Code:
<?php 
include('submit/config.php');
$include_dir "include";
$settings_dir "settings";
$template_dir "templates";
include 
"$settings_dir/conf.php";
include 
"$include_dir/commonfuncs.php";
if (
$_POST[submit]) { 
// the above line checks to see if the html form has been submitted 
$url $_POST[url]; 
$title $_POST[title]; 
$short_desc $_POST[short_desc]; 

//the above lines set variables with the user submitted information 
if($url==NULL|$title==NULL|$short_desc==NULL) { 
//checks to make sure no fields were left blank 
echo "A field was left blank."
}else{
$query mysql_query("INSERT INTO sites (url, title, short_desc) VALUES('$url','$title','$short_desc')"); 
// inserts the information into the database. 
echo "You have successfully submitted your site to the Leet Link directory. 
Your site will appear automatically after we pick a category based on the content of your site. 
We will index your site as soon as possible, please make sure you have a sitemap.xml file and robots.txt 
file for the best possible index of your site!"
; } 

} else { 
// the form has not been submitted...so now we display it. 

function addsite ($url$title$short_desc$cat) {
global $mysql_table_prefix;
$short_desc addslashes($short_desc);
$title addslashes($title);
$compurl=parse_url("".$url);
if ($compurl['path']=='')
$url=$url."/";
$result mysql_query("select site_ID from sites where url='$url'");
echo mysql_error();
$rows mysql_numrows($result);
if ($rows==) {
mysql_query("INSERT INTO sites (url, title, short_desc) VALUES ('$url', '$title', '$short_desc')");
echo mysql_error();
$result mysql_query("select site_ID from sites where url='$url'");
echo mysql_error();
$row mysql_fetch_row($result);
$site_id $row[0];
$result=mysql_query("select category_id from categories");
echo mysql_error();
while ($row=mysql_fetch_row($result)) {
$cat_id=$row[0];
if ($cat[$cat_id]=='on') {
mysql_query("INSERT INTO site_category (site_id, category_id) values ('$site_id', '$cat_id')");
echo mysql_error();
}
  }

If (!mysql_error()) {
$message =  "<br/><center><b>Site added</b></center>" ;
} else {
$message mysql_error();
}

} else {
$message "<center><b>Site already in database</b></center>";
}
return $message;
}
function 
walk_through_cats($parent$lev$site_id) {
global $mysql_table_prefix;
$space "";
for ($x 0$x $lev$x++)
$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";

$query "SELECT * FROM categories WHERE parent_num=$parent ORDER BY category";
$result mysql_query($query);
echo mysql_error();

if (mysql_num_rows($result) <> '')
while ($row mysql_fetch_array($result)) {
$id $row['category_id'];
$cat $row['category'];
$state '';
if ($site_id <> '') {
$result2 mysql_query("select * from site_category where site_id=$site_id and category_id=$id");
echo mysql_error();
$rows mysql_num_rows($result2);

if ($rows 0)
$state "checked";
}

print $space "<input type=checkbox name=cat[$id] $state>" $cat "<br/>\n";
;
walk_through_cats($id$lev 1$site_id);
}
}


echo (

<center> <div id='submenu'>
<form method=\"POST\">
<input type=hidden name=f value=1>
<input type=hidden name=af value=2>
Site URL: <input type=\"text\" size=\"35\" name=\"url\"><br /> 
Site Title: <input type=\"text\" size=\"15\" name=\"title\"><br /> 
Site short_desc: <textarea rows=4 cols=20 name=\"short_desc\"></textarea><br /> 
"
);?>
Category: <? walk_through_cats(0, 0, '');?>


<?php echo ("
<input name=\"submit\" type=\"submit\" value=\"submit\"> 
</form> 
</center> </div>
"
); }
?>

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8269
42601 credits
Members referred : 3



« Reply #1 on: Jan 13, 2006, 09:06:30 PM »

This is obvious. You don't use the category field in the INSERT statement.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #2 on: Jan 14, 2006, 02:49:45 AM »

categories are on a whole different table, this is really confusing to me, have never worked with more then 1 table at a time

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8269
42601 credits
Members referred : 3



« Reply #3 on: Jan 14, 2006, 05:13:17 AM »

It's ok. Finish what you do on the first table, and continue with the next one using the same connection

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #4 on: Jan 14, 2006, 06:42:09 AM »

so i shuld get rid of the function for addsite, and then add another query for categories table?

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #5 on: Jan 14, 2006, 07:03:51 PM »

here is what i have now but still no luck...

Code:
<head>
<title>Leet Link: Submit Your Site</title>
<link rel="stylesheet" href="style.css" />
<script language="javascript" src="scripts.js"></script>
</head>
<?php 
include('submit/config.php');
$include_dir "include";
$settings_dir "settings";
$template_dir "templates";
include 
"$settings_dir/conf.php";
include 
"$include_dir/commonfuncs.php";

function 
addsite ($url$title$short_desc$cat) {
global $mysql_table_prefix;
$short_desc addslashes($short_desc);
$title addslashes($title);
$compurl=parse_url("".$url);
if ($compurl['path']=='')
$url=$url."/";
$result mysql_query("select site_ID from ".$mysql_table_prefix."sites where url='$url'");
echo mysql_error();
$rows mysql_numrows($result);
if ($rows==) {
mysql_query("INSERT INTO ".$mysql_table_prefix."sites (url, title, short_desc) VALUES ('$url', '$title', '$short_desc')");
echo mysql_error();
$result mysql_query("select site_ID from ".$mysql_table_prefix."sites where url='$url'");
echo mysql_error();
$row mysql_fetch_row($result);
$site_id $row[0];
$result=mysql_query("select category_id from ".$mysql_table_prefix."categories");
echo mysql_error();
while ($row=mysql_fetch_row($result)) {
$cat_id=$row[0];
if ($cat[$cat_id]=='on') {
mysql_query("INSERT INTO ".$mysql_table_prefix."site_category (site_id, category_id) values ('$site_id', '$cat_id')");
echo mysql_error();
}
  }

If (!mysql_error()) {
$message =  "<br/><center><b>Site added</b></center>" ;
} else {
$message mysql_error();
}

} else {
$message "<center><b>Site already in database</b></center>";
}
return $message;
}

function 
walk_through_cats($parent$lev$site_id) {
global $mysql_table_prefix;
$space "";
for ($x 0$x $lev$x++)
$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$query "SELECT * FROM categories WHERE parent_num=$parent ORDER BY category";
$result mysql_query($query);
echo mysql_error();
if (mysql_num_rows($result) <> '')
while ($row mysql_fetch_array($result)) {
$id $row['category_id'];
$cat $row['category'];
$state '';
if ($site_id <> '') {
$result2 mysql_query("select * from site_category where site_id=$site_id and category_id=$id");
echo mysql_error();
$rows mysql_num_rows($result2);
if ($rows 0)
$state "checked";
}
print $space "<input type=checkbox name=cat[$id] $state>" $cat "<br/>\n";
;
walk_through_cats($id$lev 1$site_id);
}
}

function addsiteform() {
echo "
<div id=\"submenu\"><center><b>Add a site</b></center></div>
<br/><div align=center><center><table>
<form method=post>
    <tr><td><b>URL:</b></td><td align =\"right\"></td><td><input type=text name=url size=60 value =\"http://\"></td></tr>
<tr><td><b>Title:</b></td><td></td><td> <input type=text name=title size=60></td></tr>
<tr><td><b>Short description:</b></td><td></td><td><textarea name=short_desc cols=45 rows=3 wrap=\"virtual\"></textarea></td></tr>
<tr><td>Category:</td><td></td><td>
"
;?>
<? walk_through_cats(0, 0, '');?><?php echo "</td></tr>
<tr><td></td><td></td><td><input type=submit id=\"submit\" value=Add></td></tr></form></table></center></div>
"
;
}

switch (
$f) {
default:
addsiteform();
break;
case 
1:
$message addsite($url$title$short_desc$cat);
$compurl=parse_url($url);
if ($compurl['path']=='')
$url=$url."/";
 
$result mysql_query("select site_id from ".$mysql_table_prefix."sites where url='$url'");
echo mysql_error();
$row mysql_fetch_row($result);
if ($site_id != "")
siteScreen($site_id$message);
else
showsites($message);

}
?>

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=1188
Tags : html databases robots.txt Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: need some help with writing to database
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Dec 04, 2008, 09:52:55 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!





Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.