[SOLVED] Adding to database wont work ...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
garethnash
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 11:35 am
Location: UK

[SOLVED] Adding to database wont work ...

Post by garethnash »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


The following lines seem to be stopping my script working but I cant work out what id causing it:

Code: Select all

function add_to_database($name, $wlink, $scribe, $scriber, $scriberemail, $hitcount, &$dberror) {
	$name=mysql_real_escape_string($name);
	$wlink=mysql_real_escape_string($wlink);
	$scribe=mysql_real_escape_string($scribe);
	$scriber=mysql_real_escape_string($scriber);
	$scriberemail=mysql_real_escape_string($scriberemail);
	$link=mysql_pconnect("localhost", "**********", "**********");
if (! $link){
	$dberror=mysqlerror();
	return false;
}
I aint touched PHP for couple of months and I cant figure out whats wrong. Any help appreciated. The full code is...

Code: Select all

<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Add Sql</title>
</head>
<body>
<?php
$user="***********";
$pass="***********";
$db="garethna_websitelinks";
$link=@mysql_connect("localhost",$user, $pass);
if (! $link){
die ("Couldnt connect to mysql:".mysql_error());
}
print "<h2>Successfully connected to server</h2>\n\n<br/>";
if (!empty ($_REQUEST['name'])&&
    !empty ($_REQUEST['wlink'])&&
    !empty ($_REQUEST['scribe'])&&
    !empty ($_REQUEST['scriber'])&&
    !empty ($_REQUEST['scriberemail'])){
	//check user input here!
	$dberror="";
	$ret=add_to_database($_REQUEST['name'],
				$_REQUEST['wlink'],
				$_REQUEST['scribe'],
				$_REQUEST['scriber'],
				$_REQUEST['scriberemail'],$dberror);
if (! $ret) {
	print "Error: $dberror<br/>\n";
}else{
	print "Thankyou very much!<br/>\n";
	}
}else {
 write_form();
}

function add_to_database($name, $wlink, $scribe, $scriber, $scriberemail, $hitcount, &$dberror) {
	$name=mysql_real_escape_string($name);
	$wlink=mysql_real_escape_string($wlink);
	$scribe=mysql_real_escape_string($scribe);
	$scriber=mysql_real_escape_string($scriber);
	$scriberemail=mysql_real_escape_string($scriberemail);
	$link=mysql_pconnect("localhost", "**********", "**********");
if (! $link){
	$dberror=mysqlerror();
	return false;
}
if (! mysql_select_db("garethna_websitelinks", $link)){
$dberror=mysqlerror();
return false;
}
$query="INSERT INTO websites (name, link, scribe, scriber, scriberemail,)
	values('$name', '$wlink', '$scriber', '$scriber', '$scriberemail')";
if (! mysql_query($query, $link)){
$dberror=mysql_error();
return false;
}
return true;
}

function write_form(){
print <<<EOF
	<form method="post" action = "{$_SERVER['PHP_SELF']}">
	<p><input type="text" name="name"/>
	Website Name</p>
	
	<p><input type="text" name="wlink"/>
	Link</p>

	<p><input type="text" name="scribe"/>
	Details</p>

	<p><input type="text" name="scriber"/>
	Your name</p>

	<p><input type="text" name="scriberemail"/>
	Your email</p>

	<p><input type="submit" value = "Sumbit it!"/></p>
</form>
EOF;
}
?>
</body>
</html>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


[color=darkgreen][b]feyd[/b] | removed username/password[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the point of connecting at the beginning of the script and inside the function.. with different connections?
garethnash
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 11:35 am
Location: UK

I know I suck lol

Post by garethnash »

I know I suck lol
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

What's the error you're getting?
garethnash
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 11:35 am
Location: UK

Post by garethnash »

Sorted - problem was with my variable names. Thanks for all the help guys!
Post Reply