Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL INSERT from Form Fields

Status
Not open for further replies.

Harlequ1n

Technical User
Jul 1, 2004
12
0
0
GB
I'm trying to capture data from a members registration form and insert this into a MySQL database using PHP but keep getting errors although the code seems to read OK.

I can echo values from the database and get no connection errors so it has to be the data insertion portion of the code that's incorrect. Can anyone help with this...?

<?php

/* MySQL Connection Variables */
$host="localhost";
$user="arras_WebMaster";
$password="qwerty";
$dbase="arras_Members";

/* MySQL Connection String */
$Connection=mysql_connect ("$host", "$user", "$password")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$dbase");

echo "database status: alive";

/* User Registration Form */
echo "<form action = '??????????.php' method='POST'>";
echo "<pre>UserID: \t\t<input type = 'text' name = 'TXT_UserID'></pre>";
echo "<br>";
echo "<pre>Password: \t \t<input type = 'text' name = 'TXT_UserPassword'></pre>";
echo "<br>";
echo "<pre>a little about me: \t<input type = 'text' name = 'TXT_Comments'></pre>";
echo "<br>";
echo "<pre>more about me: \t<input type = 'text' name = 'TXT_FurtherComments'></pre>";
echo "<br>";
echo "<pre>\t\t\t<input type='submit' value='Submit Details'<pre>";
echo "</form>";

/* Data Insertion */
$NewUserID= $_POST['TXT_UserID'];
$NewUserPassword= $_POST['TXT_UserPassword'];
$NewUserComments= $_POST['TXT_Comments'];
$NewUserFurtherComments=$_POST['TXT_FurtherComments'];
$sql = "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments)"
VALUES('$NewUserID','$NewUserPassword','$NewUserComments','$NewUserFurtherComments');

?>

The error message I receive makes no sense either:
Parse error: parse error, unexpected '[' in /home/arras/public_html/members/Register.php on line 69

That line is actually a closing HTML TAG.

Any help would be greatly appreciated.


-----------------------
[colorface] Michael Mason [colorface]
-----------------------
 
Dont take this as an insult, but you code as badly as me which means that the following should work for you.

Code:
$sql = "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments) VALUES ('$NewUserID','$NewUserPassword','$NewUserComments','$NewUserFurtherComments')";

Apparently its infinately more acceptable to crrectly concatenate the variables into the sql query string like

Code:
$sql = "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments)
    VALUES('".$NewUserID."','".$NewUserPassword."','".$NewUserComments."','".$NewUserFurtherComments."')";




______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
You have a misplaced double quote (") in the following line:
Code:
    $sql = "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments)"
    VALUES('$NewUserID','$NewUserPassword','$NewUserComments','$NewUserFurtherComments');

Your closing quote should be before the semi-colon not before the word VALUES

Ken
 
KarveR

I tried your addition and the page displays fine and when I click the submit button it takes me through to the designated page but doesn't commit the data to the database.

Oh, and thanks for the compliment. My VB coding is much much worse and I still have to get my teeth into VBS for a new server box [colorface]

This is what I have now...

<?php

/* MySQL Connection Variables */
$host="localhost";
$user="arras_WebMaster";
$password="qwerty";
$dbase="arras_Members";

/* MySQL Connection String */
$Connection=mysql_connect ("$host", "$user", "$password")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$dbase");

echo "database status: alive";

/* User Registration Form */
echo "<form action = 'Logged_In.php' method='POST'>";
echo "<pre>UserID: \t\t<input type = 'text' name = 'TXT_UserID'></pre>";
echo "<br>";
echo "<pre>Password: \t \t<input type = 'text' name = 'TXT_UserPassword'></pre>";
echo "<br>";
echo "<pre>a little about me: \t<input type = 'text' name = 'TXT_Comments'></pre>";
echo "<br>";
echo "<pre>more about me: \t<input type = 'text' name = 'TXT_FurtherComments'></pre>";
echo "<br>";
echo "<pre>\t\t\t<input type='submit' value='Submit Details'<pre>";
echo "</form>";

/* Data Insertion */
$NewUserID= $_POST['TXT_UserID'];
$NewUserPassword= $_POST['TXT_UserPassword'];
$NewUserComments= $_POST['TXT_Comments'];
$NewUserFurtherComments=$_POST['TXT_FurtherComments'];
$sql = "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments)
VALUES('".$NewUserID."','".$NewUserPassword."','".$NewUserComments."','".$NewUserFurtherComments."')";
?>




-----------------------
[colorface] Michael Mason [colorface]
-----------------------
 
echo the query onto the page, to make sure you are getting something anything:

echo "<textarea rows=5 cols=50>$sql</textarea>;



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR

I seperated the code following discussions with others. Now I have two pages:

Register
Registered

The Register page POSTs to the Registered page and should commit the data. However, although I am echoing the results from the Register page succesfully for some reason I get a parse error because of an "=" symbol.

<?php
/* MySQL Connection Variables */
$host="localhost";
$user="arras_WebMaster";
$password="qwerty";
$dbase="arras_Members";

/* MySQL Connection String */
$Connection=mysql_connect ("$host", "$user", "$password")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$dbase");

echo "<br>";
echo "<h1>members area</h1>";

echo "<h3>User Created</h3>";
echo "<br><br><br>";

/* Data Insertion */
$NewUserID= $_POST['TXT_UserID'];
$NewUserPassword= $_POST['TXT_UserPassword'];
$NewUserComments= $_POST['TXT_Comments'];
$NewUserFurtherComments=$_POST['TXT_FurtherComments'];
sql == "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments)

VALUES('".$NewUserID."','".$NewUserPassword."','".$NewUserComments."','".$NewUserFurtherComments."')";
$query = mysql_query($sql);
echo "<pre>Username:\t\t$NewUserID</pre><br>";
echo "<pre>Password:\t\t$NewUserPassword</pre><br>";
echo "<pre>Comments:\t\t$NewUserComments</pre><br>";
echo "<pre>Further Comments:\t$NewUserFurtherComments</pre>";

mysql_close ($Connection);
?>


-----------------------
[colorface] Michael Mason [colorface]
-----------------------
 
Actually. Breaking it down, I know the problem lies with the data insertion portion of the code so here it is:

/* Data Insertion */
$NewUserID= $_POST['TXT_UserID'];
$NewUserPassword= $_POST['TXT_UserPassword'];
$NewUserComments= $_POST['TXT_Comments'];
$NewUserFurtherComments=$_POST['TXT_FurtherComments'];
$sql = "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments)
VALUES('".$NewUserID."','".$NewUserPassword."','".$NewUserComments."','".$NewUserFurtherComments."')";

$query = mysql_query($sql);

$mysql_result = mysql_query ($sql, $Connection) or die ("Invalid Query");

I get the message "Invalid Query". Not taht surprising or useful...!


-----------------------
[colorface] Michael Mason [colorface]
-----------------------
 
Code:
<?php
//make connection
$Connection=mysql_connect ("localhost","root","");

//select databases
mysql_select_db ("test");
/* Data Insertion */
  $NewUserID=$_POST['TXT_UserID'];
  $NewUserPassword=$_POST['TXT_UserPassword'];
  $NewUserComments=$_POST['TXT_Comments'];
  $NewUserFurtherComments=$_POST['TXT_FurtherComments'];
  $sql = "INSERT INTO RegisteredMembers (TXT_UserID,TXT_UserPassword,TXT_Comments,TXT_FurtherComments)
  VALUES('".$NewUserID."','".$NewUserPassword."','".$NewUserComments."','".$NewUserFurtherComments."')";

//dont need to see the query stuff as we know its now right
// echo "$sql";

//correctly send the query and check for errors
mysql_query("$sql") or die ("error". mysql_error());

// see what gets updated should ideally only ever be 1.
$rows_affected=mysql_affected_rows();

echo "added $rows_affected rows";

?>

works for me now... ;-)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks KarveR

I've now managed to get the subsequent page working fine which send a confirmation e-mail to the user.

Thanks very much for your patience.


-----------------------
[colorface] Michael Mason [colorface]
-----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top