Problem with form select menu ....

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

Problem with form select menu ....

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]


Ok this code wont work for me. It should enable the dropdown in the form to pick which table the data in the form is added to. It should either go to the table "cichilds" or the table "websites". Doesnt work at alll for me. Any help much appreciated:

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";
$link2=@mysql_connect("localhost",$user, $pass);
if (! $link2){
die ("Couldnt connect to mysql:".mysql_error());
}
print "<h2>Successfully connected to server</h2>\n\n<br/>";
if (!empty ($_REQUEST['name'])&&
    !empty ($_REQUEST['link'])&&
        !empty ($_REQUEST['scribe'])&&
                !empty ($_REQUEST['scriber'])&&
    !empty ($_REQUEST['scriberemail'])){
	//check user input here!
	$dberror="";
	$ret=add_to_database($_REQUEST['name'],
				$_REQUEST['link'],
				$_REQUEST['scribe'],
				$_REQUEST['scriber'],
								$_REQUEST['select'],
				$_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, $link, $scribe, $scriber, $scriberemail, &$dberror){
	$name=mysql_real_escape_string($name);
	$link=mysql_real_escape_string($link);
	$scribe=mysql_real_escape_string($scribe);
	$scriber=mysql_real_escape_string($scriber);
	$select=mysql_real_escape_string($select);
	$scriberemail=mysql_real_escape_string($scriberemail);
	$link2=mysql_pconnect("localhost", "*********", "*********");
if (! $link2){
	$dberror=mysqlerror();
	return false;
}
if (! mysql_select_db("garethna_websitelinks", $link2)){
$dberror=mysqlerror();
return false;
}
$query="INSERT INTO $select (name, link, scribe, scriber, scriberemail)
	values('$name', '$link', '$scribe', '$scriber', '$scriberemail')";
if (! mysql_query($query, $link2)){
$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"/>
	Name</p>
	
	<p><input type="text" name="link"/>
	Website address</p>

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

	<p><input type="text" name="scriberemail"/>
	Your email</p>
	
<select name="select">
  <option>cichilds</option>
  <option>websites</option>
</select>

	<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]
garethnash
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 11:35 am
Location: UK

is it obvious...

Post by garethnash »

I think with my limited PHP knowledge I am doing something obvious wrong but cannt foigure out what it is. Is it ok to call on the INSERT line a variable that was defined in the form is this my problem. This is really doing my head in now lol :x
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Try adding values to your select options....
e.g

Code: Select all

<select name="select">
  <option value="cichilds">cichilds</option>
  <option value="websites">websites</option>
</select>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$select in line 56 is never set, nor passed in.
garethnash
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 11:35 am
Location: UK

ok..

Post by garethnash »

Well I definitely needed to do that but its still not working. I preume that somewhere I have changed select into a vvariable and didnt need to or vice versa.
garethnash
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 11:35 am
Location: UK

setting select...

Post by garethnash »

Have to admit I cant work out where I need to set select if the code hasnt already done it?

I did put the options values in as suggested!
Post Reply