PDA

View Full Version : Problems adding info into database


junky
08-05-2003, 08:16 AM
Hey all
I'm new to this forum and new to PHP as well :)
I'm trying to run a script that adds info to my database by the way of user input,
its an example from a book
being that I'm new to php its hard to problem solve and I am stuck !!

<?php
if (isset($domain) && isset($sex) && isset($mail) ) {
$dberror = "";
$ret = add_to_database($domain, $sex, $mail, $dberror );
if ( ! $ret )
print "Error: $dberror<BR>";
else
print "Thank you very much";
}else{
write_form();
}

function add_to_database($domain, $sex, $mail, &$dberror){
$db = "sample";
$link = mysql_connect("localhost");
if ( ! $link ) {
$dberror = "Couldn't connect to MySQL server";
return false;
}
if ( ! mysql_select_db( $db, $link) ) {
$dberror = mysql_error();
return false;
}
$query = "INSERT INTO domains ( domain, sex, mail )
values( '$domain', '$sex', '$mail' )";
if ( ! mysql_query($query, $link) ) {
$dberror = mysql_error();
return false;
}
return true;
}
function write_form() {
global $PHP_SELF;
print "<form method=\"POST\">\n";
print "<input type\"text\" name\"domain\"> ";
print "The domain you want<p>\n";
print "<input TYPE=\"text\" name=\"mail\"> ";
print "Your mail address<p>\n";
print "<select name=\"sex\">\n";
print "\t<option value=\"F\"> Female\n";
print "\t<option value=\"M\"> Male\n";
print "</select>\n";
print "<input type=\"submit\" value=\"submit!\">\n</form>\n";
}



?>



any help would be appreciated

thanks

Nightfire
08-05-2003, 10:09 AM
What errors are you getting, if any?

raf
08-05-2003, 10:35 AM
the problem is probably that you have an old book (based on an older PHP version like 4.0 or 4.1) that assumes you have register globals set to ON (more info about that here http://be2.php.net/register_globals ) wheras the server you run your PHP's on, has register globals set to OFF.

So assuming this is true, then you need to change all the variables that refer to the form (like $domain) into $_POST['domain'] or $_GET['domain'] depending on which method you used to post the form.

junky
08-05-2003, 10:21 PM
Yeah that's what I was thinking.
It will work and add info to my database if I use this code without the form

$query = "INSERT INTO domains ( domain, sex, mail)
values( '123.com', 'F', 'Abc@321.com')";

But when i try to use $_POST['...'] I will get this error

Parse error: parse error, expecting `')''

Here is how I have the code now





<?php


if (isset($_POST['domain']) && isset($_POST['sex']) && isset($_POST['mail']) ) {
$dberror ="";
$ret = add_to_database($_POST['domain'], $_POST['sex'], $_POST['mail']);
if ( ! $ret )
print "Error: $dberror<BR>";
else
print "Thank you very much";
}else{
write_form();
}

function add_to_database($_POST['domain'], $_POST['sex'], $_POST['mail'], &$dberror){
$db = "sample";
$link = mysql_connect("localhost", $user);
if ( ! $link ) {
$dberror = "Couldn't connect to MySQL server";
return false;
}
if ( ! mysql_select_db( $db, $link) ) {
$dberror = mysql_error();
return false;
}
$query = "INSERT INTO domains ( domain, sex, mail )
values( '$_POST['domain']', '$_POST['sex']', '$POST['mail']' )";
if ( ! mysql_query($query, $link) ) {
$dberror = mysql_error();
return false;
}
return true;

}
function write_form() {
global $PHP_SELF;
print "<form method=\"POST\">\n";
print "<input type\"text\" name\"domain\"> ";
print "The domain you want<p>\n";
print "<input type=\"text\" name=\"mail\"> ";
print "Your mail address<p>\n";
print "<select name=\"sex\">\n";
print "\t<option value=\"F\"> Female\n";
print "\t<option value=\"M\"> Male\n";
print "</select>\n";
print "<input type=\"submit\" value=\"submit!\">\n</form>\n";
}



?>



Thanks

CrAzYCoDeR969
08-05-2003, 10:52 PM
Try putting { } around your $_POST variables within the string. You have to do that when putting array values within strings.


$query = "INSERT INTO domains ( domain, sex, mail )
values( '{$_POST['domain']}', '{$_POST['sex']}',
'{$POST['mail']}' )";


That is where you are getting your parse error from.

junky
08-06-2003, 12:56 AM
Okay I think the problem is when the function add_to_database is called.
when I put $_POST['...'] in there, i get the error

Parse error: parse error, expecting `')'' in c:\phpdev\www\add_to_db.php on line 15

When I don't use $_POST ... the form will display but will not add the data.


just to be sure about the problem
i used phpMyAdmin 2.3.2 that came along with PhpDev 4.23
to set up my table

CREATE TABLE domains (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
domain VARCHAR(20),
sex CHAR(1),
mail VARCHAR(20)
);


I appreciate the help so far
thanks

Nightfire
08-06-2003, 01:05 AM
The way I do it is like this

$domain = $_POST['domain'];
$sex = $_POST['sex'];
$mail = $_POST['mail'];

$query = "INSERT INTO `domains` ( domain, sex, mail) VALUES ( '$domain', '$sex','$mail')";

junky
08-06-2003, 04:22 AM
That woked!

Thanks for all your help:thumbsup: