-4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="admin page.php">
   <p>
     <label for="Name">Name</label>
     <input type="text" name="Name" id="Name" />
   </p>
   <p>
     <label for="Age">Age </label>
     <input type="text" name="Age" id="Age" />
   </p>
   <p>
     <label for="Email">Email</label>
     <input type="text" name="Email" id="Email" />
   </p>
   <p>
     <label for="DOB">DOB</label>
     <input type="text" name="DOB" id="DOB" />
   </p>
   <p>
     <input type="button" name="add" id="add" value="Add" onclick="func();" />
   </p>
</form>

<?php 

 function func()
 {
  $con = mysql_connect("127.0.0.1","root","") or die(mysql_error());
  $db = mysql_select_db("lr") or die(mysql_error());
  $sql=mysqli_query("INSERT INTO Persons (Name,age,Email,DOB)
                                       VALUES ('&Name', '&Age','&Email','&DOB')");
  if($sql){echo "Welcome ".$Name.",you may Login now! ";}

   mysql_close($con);    
 }

?>
</body>

This is my code i want to run my query on this very same page! I cannot do it on other page because i want to add users recursively in this page .I called function func on click to do that but its not working.

Asd
  • 103
  • 2
  • 12
  • 1
    why do you always want to insert `'&Name', '&Age','&Email','&DOB'` it will be same for each user .. .and there is difference between & and $ i think you want to use $ – NullPoiиteя Jun 05 '13 at 05:18
  • What is the error you getting? – Yogus Jun 05 '13 at 05:18
  • Have you looked at your code. You are mixing all things altogether. You can't call `php` function onclick of button. For this you have to use ajax. – Yogesh Suthar Jun 05 '13 at 05:18
  • yes im new in php can you please help a bit ? – Asd Jun 05 '13 at 05:22
  • guide me with ajax please – Asd Jun 05 '13 at 05:22
  • @Asd try [php chat](http://chat.stackoverflow.com/rooms/11/php) , and you can also check this to know how to make ajax call with jquery http://stackoverflow.com/questions/5004233/jquery-ajax-post-example/14217926#14217926 – NullPoiиteя Jun 05 '13 at 05:25
  • You mixing up mysql and mysqli – Yogus Jun 05 '13 at 05:33
  • i am afraid but you really need to learn stuff from starting .. you are calling server side language function directly on client side without ajax something like that ... – NullPoiиteя Jun 05 '13 at 05:44

3 Answers3

2

Try this corrections/changes in your code:

  • action in form: <form id="form1" name="form1" method="post" action="admin page.php"> - no white-space between admin and page.php!

  • func(). You cannot call php with onclick. Just remove that line onclick="func();". The form submit action will do what you want. And remove also the func() {} and leave just the php

  • INSERT: change & to $ and concatenate your variables. For example I STRONGLY suggest you add also some validation for your form inputs to avoid sql injections.

  • Getting the variables from the input fields: use $Age = $_POST["Age"] to get the variable from the form post.

  • Your code is vulnerable to sql injection if you just replace & to $ in your query you need to properly escape all request in mysql_* you can use mysql_real_escape_string()

  • Mysql_* api is deprecated so either use PDO or MySQLi and imo use PDO

  • you are also mixing tow different api mysqli_* (mysqli_query) and mysql_* api which wont work

Community
  • 1
  • 1
Sergio
  • 28,539
  • 11
  • 85
  • 132
2

Try something like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

   $name= $_POST['Name'];
   $age= $_POST['Age'];
   $email= $_POST['Email'];
   $dob= $_POST['DOB'];

$sql = "INSERT INTO employee ".
       "(name,age, email, dob) ".
       "VALUES('$name','$age','$email','$dob')";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form id="form1" name="form1" method="post" action="<?php $_PHP_SELF ?>">
   <p>
     <label for="Name">Name</label>
     <input type="text" name="Name" id="Name" />
   </p>
   <p>
     <label for="Age">Age </label>
     <input type="text" name="Age" id="Age" />
   </p>
   <p>
     <label for="Email">Email</label>
     <input type="text" name="Email" id="Email" />
   </p>
   <p>
     <label for="DOB">DOB</label>
     <input type="text" name="DOB" id="DOB" />
   </p>
   <p>
     <input type="submit" name="add" id="add" value="Add" />
   </p>
</form>
<?php
}
?>
</body>
</html>
Yogus
  • 2,307
  • 5
  • 20
  • 38
  • 1
    Your code is vulnerable to **sql injection** in `mysql_*` you can use `mysql_real_escape_string()` – NullPoiиteя Jun 05 '13 at 05:46
  • @NullPoiиteя Yes I know . its just an example for the user . He can modify and can use mysqli or PDO if its available on server – Yogus Jun 05 '13 at 05:52
0

You can do this using ajax like this :

//Submit form without refreshing page
 $.ajax({
        type: "POST",
        url: "update.php",
        data: dataString,
        success: function(){
            $('.success').fadeIn(200).show();
            $('.error').fadeOut(200).hide();
        }
    });

Please refer HERE and jQuery.ajax()

DEMO

rusly
  • 1,504
  • 6
  • 28
  • 62