Jump to content

[SOLVED] unable to insert user input into ms sql database. help is needed!!


Recommended Posts

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);

}

?>

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??

<?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 . "') ";

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>");

  }

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>");

  }

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?

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?

 

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>

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! :)

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.