Page 1 of 1

Problem with form select menu ....

Posted: Tue Sep 21, 2004 10:03 am
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]

is it obvious...

Posted: Tue Sep 21, 2004 10:30 am
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

Posted: Tue Sep 21, 2004 10:42 am
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>

Posted: Tue Sep 21, 2004 10:44 am
by feyd
$select in line 56 is never set, nor passed in.

ok..

Posted: Tue Sep 21, 2004 10:47 am
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.

setting select...

Posted: Tue Sep 21, 2004 10:50 am
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!