poqoz-87 Posted May 7, 2007 Share Posted May 7, 2007 i have tried to insert user input into ms sql database but still unable to do so. my code has no errors in it. its just that the user input cannot be submitted into the ms sql database. can someone help me out with this?? these are the codes that i m using: <?php <?php if (isset($_POST['submit'])) { //Collect form data and assign to scalar variables $Name = $_POST['Name']; $Email = $_POST['Email']; $Connection = $_POST['Connection']; $Residence = $_POST['Residence']; $Age = $_POST['Age']; $Gender = $_POST['Gender']; $Comments = $_POST['Comments']; //Establish a connection to the Database // create connection $connection = mssql_connect("localhost","sa","sa"); // select database $db = mssql_select_db("Hello", $connection); //SQL Statement $sql = "INSERT INTO Survey ". "(Name,Email,Connection,Residence,Age,Gender,Comments) VALUES ('$Name', '$Email', '$Connection', '$Residence', '$Age', '$Gender', '$Comments')"; //Execute SQL Statement and store results as a recordset $rs = mssql_query($db,$sql); //mssql_free_result($rs); mssql_close($connection); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/ Share on other sites More sharing options...
jitesh Posted May 7, 2007 Share Posted May 7, 2007 $connection = mssql_connect("localhost","sa","sa"); echo $connection; // Should give o/p like this : Resource ID #1 Check connection is held or not. Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247125 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 i have tried another method. i found this code at this website:>> http://www.samag.com/documents/s=9370/sam0210c/0210c.htm the codes that i m working by following the website example is working. but when i try to add new label called "residence", i was given an error. this is the error: PHP Warning: mssql_query() [function.mssql-query]: message: There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement. (severity 15) in C:\Inetpub\wwwroot\insert-1.php on line 20 PHP Warning: mssql_query() [function.mssql-query]: Query failed in C:\Inetpub\wwwroot\insert-1.php on line 20 why is it so?n how can i solve it? this are the codes i used when i try to add new label called "residence" <?php if (!empty($_POST["submit"])) { $conn = mssql_connect("<my server ip address>", "mei", "mei"); mssql_select_db("lalalala", $conn); $sql = "INSERT INTO me (name, email, residence) VALUES ("; $sql .= "'" . $_POST["name"] . "', "; $sql .= "'" . $_POST["email"] . "')"; $sql .= "'" . $_POST["residence"] . "')"; mssql_query($sql, $conn); mssql_close($conn); echo("<p>Entry added to database!</p>"); } ?> can someone help me with this error?? Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247763 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 <?php if (!empty($_POST["submit"])) { $conn = mssql_connect("<my server ip address>", "mei", "mei"); mssql_select_db("lalalala", $conn); $sql = "INSERT INTO me (name, email, residence) VALUES ("; $sql .= "'" . $_POST["name"] . "', "; $sql .= "'" . $_POST["email"] . "')"; $sql .= "'" . $_POST["residence"] . "')"; mssql_query($sql, $conn); mssql_close($conn); echo("<p>Entry added to database!</p>"); } ?> try this code: $username=htmlentities($_POST['name']); $email=htmlentities($_POST['email']); $resid=htmlentities($_POST['residence']); $sql = "INSERT into `me` SET "; $sql .= "`name` = ('" . $username . "'), "; $sql .= "`email` = ('" . $email . "'), "; $sql .= "`residence` = ('" . $resid . "') "; Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247770 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 i am inserting user input into ms sql database, by the way:) i have tried using ur code. but there is still an error. the error is PHP Fatal error: Call to undefined function mysql_real_escape_string() in C:\Inetpub\wwwroot\insert-1.php on line 17 what should i do ? this are the codes that i m talking abt: <?php if (!empty($_POST["submit"])) { $conn = mssql_connect("<my server ip address>", "mei", "mei"); mssql_select_db("lalalala", $conn); $username=mysql_real_escape_string($_POST['name']); $email=mysql_real_escape_string($_POST['email']); $resid=mysql_real_escape_string($_POST['residence']); $sql = "INSERT into `me` SET "; $sql .= "`name` = ('" . $username . "'), "; $sql .= "`email` = ('" . $email . "'), "; $sql .= "`residence` = ('" . $resid . "') "; mssql_query($sql, $conn); mssql_close($conn); echo("<p>Entry added to database!</p>"); } Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247778 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 Sorry for that plz again look at my post above i remove the mysql_real_escape_string with htmlentities. Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247779 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 ok. i have tried using ur code. but there is still an error. the error is: PHP Warning: mssql_query() [function.mssql-query]: message: Line 1: Incorrect syntax near '`'. (severity 15) in C:\Inetpub\wwwroot\insert-1.php on line 27 PHP Warning: mssql_query() [function.mssql-query]: Query failed in C:\Inetpub\wwwroot\insert-1.php on line 27 what should i do? how can i solve this error?? this is driving me nuts! this is the codes that i use together with the codes that u told me: <?php if (!empty($_POST["submit"])) { $conn = mssql_connect("<my ip address>", "mei", "mei"); mssql_select_db("lalalala", $conn); $username=htmlentities($_POST['name']); $email=htmlentities($_POST['email']); $resid=htmlentities($_POST['residence']); $sql = "INSERT into `me` SET "; $sql .= "`name` = ('" . $username . "'), "; $sql .= "`email` = ('" . $email . "'), "; $sql .= "`residence` = ('" . $resid . "') "; //line 27 mssql_query($sql, $conn); mssql_close($conn); echo("<p>Entry added to database!</p>"); } Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247785 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 //line 27 mssql_query($sql, $conn); mssql_close($conn); echo("<p>Entry added to database!</p>"); } Chane that line to this: $sql_result = mssql_query($sql, $conn); Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247786 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 there is still an error. the error is >>> PHP Warning: mssql_query() [function.mssql-query]: message: Line 1: Incorrect syntax near '`'. (severity 15) in C:\Inetpub\wwwroot\insert-1.php on line 27 PHP Warning: mssql_query() [function.mssql-query]: Query failed in C:\Inetpub\wwwroot\insert-1.php on line 27 //line 27 are the line which has cause the error $sql_result = mssql_query($sql, $conn); how can i solve this error? Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247793 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 change this : $conn = mssql_connect("<my ip address>", "mei", "mei"); to this and try: $conn = mssql_pconnect("<my ip address>", "mei", "mei"); Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247798 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 it still resulted error. the error is this >> PHP Warning: mssql_query() [function.mssql-query]: message: Line 1: Incorrect syntax near '`'. (severity 15) in C:\Inetpub\wwwroot\insert-1.php on line 27 PHP Warning: mssql_query() [function.mssql-query]: Query failed in C:\Inetpub\wwwroot\insert-1.php on line 27 . //the line 27 which has caused the error $sql_result = mssql_query($sql, $conn); what should i do? Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247860 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 try $sql_result = mssql_query($sql, $conn); if($sql_result){ echo "<p>Entry added to database!</p>"; } Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247867 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 it still resulted in an error. the error is >> PHP Parse error: syntax error, unexpected $end in C:\Inetpub\wwwroot\insert-1.php on line 40 the weird thing is that on line 40, it is </html>. what is wrong? <?php if (!empty($_POST["submit"])) { $conn = mssql_pconnect("<my ip address>", "mei", "mei"); mssql_select_db("lalalala", $conn); $username=htmlentities($_POST['name']); $email=htmlentities($_POST['email']); $resid=htmlentities($_POST['residence']); $sql = "INSERT into `users` SET "; $sql .= "`name` = ('" . $username . "'), "; $sql .= "`email` = ('" . $email . "'), "; $sql .= "`residence` = ('" . $resid . "') "; $sql_result = mssql_query($sql, $conn); mssql_close($conn); if($sql_result){ echo "<p>Entry added to database!</p>"; } ?> <form method="post"> Name: <input type="text" name="name" /><br /> Email: <input type="text" name="email" /><br /> Residence: <input type="text" name="residence" /><br /> <input type="submit" name="submit" value="Add to database" /> </form> </body> //this is line 40 </html> Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247873 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 Please post ur complete code in ur code blocks. Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247876 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 i have solve the error:) i change this line n it works: $sql = "INSERT INTO users (name, email,residence) VALUES ("; $sql .= "'" . $_POST["name"] . "', "; $sql .= "'" . $_POST["email"] . "', "; $sql .= "'" . $_POST["residence"] . "')"; i can now insert the data into ms sql database. thanks mmarif4u! you have been helping me a lot of time! i really treasure that. thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247879 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 Glad to hear that sorted it out, please mark th thread as solved. Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247883 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 haven yet finish. why issit when i want to add label called "gender", the data is not being inserted to the ms sql database? there is no error. its just that the data is not being inserted into the ms sql database. this is the codes that i m using now: <?php if (!empty($_POST["submit"])) { $conn = mssql_connect("<my ip address>", "mei", "mei"); mssql_select_db("lalalala", $conn); $sql = "INSERT INTO users (name, email,residence,Gender) VALUES ("; $sql .= "'" . $_POST["name"] . "', "; $sql .= "'" . $_POST["email"] . "', "; $sql .= "'" . $_POST["residence"] . "', "; $sql .= "'" . $_POST["Gender"] . "')"; mssql_query($sql, $conn); mssql_close($conn); echo("<p>Entry added to database!</p>"); } ?> <form method="post"> Name: <input type="text" name="name" /><br /> Email: <input type="text" name="email" /><br /> Residence: <input type="text" name="residence" /><br /> Gender: <input name="Gender" type="radio" value="" /><label>Male</label> <input name="Gender" type="radio" value="" /><label>FeMale</label><br /> <input type="submit" name="submit" value="Add to database" /> </form> i have tried using the codes, that u-mmarif4u send it to me, but to no avail. can someone help out? Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247888 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 try this. <input name="Gender" type="radio" value="Male" /><label>Male</label> <input name="Gender" type="radio" value="Female" /><label>FeMale</label> Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247893 Share on other sites More sharing options...
poqoz-87 Posted May 8, 2007 Author Share Posted May 8, 2007 it works! thanks you many many:D Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247906 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 u r welcom. Quote Link to comment https://forums.phpfreaks.com/topic/50334-solved-unable-to-insert-user-input-into-ms-sql-database-help-is-needed/#findComment-247907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.