Advertisement
Guest User

insert user

a guest
Sep 26th, 2012
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.50 KB | None | 0 0
  1. <?php
  2.    
  3.     function connect_db($db_name) {
  4.        
  5.         $con = mysql_connect('localhost', 'db_user', 'db_pass');
  6.         $seldb = mysql_select_db($db_name);
  7.        
  8.         if($con && $seldb) {
  9.        
  10.             return true;
  11.        
  12.         }
  13.    
  14.     }
  15.    
  16.     function insert_user($name,$company,$address,$city,$zip,$state,$phone,$web_site) {
  17.        
  18.         $insert_user_q = "INSERT into USERS (name,company,address,city,zip,state,phone,web_site) VALUES ('" . $name . "','" . $company . "','" . $address . "','" . $city . "','" . $zip . "','" . $state . "','" . $phone . "','" . $web_site . "')";
  19.        
  20.         $insert_user_m = mysql_query($insert_user_q);
  21.        
  22.         if ($insert_user_m) {
  23.        
  24.             return true;
  25.        
  26.         }
  27.         else {
  28.        
  29.             return false;
  30.        
  31.         }
  32.        
  33.     }
  34.    
  35.     if (isset($_REQUEST['name'])) {
  36.  
  37.         $conn_to_db = connect_db("db_name");
  38.        
  39.         if ($conn_to_db) {
  40.            
  41.             $name = $_REQUEST['name'];
  42.             $company = $_REQUEST['company'];
  43.             $address = $_REQUEST['address'];
  44.             $city = $_REQUEST['city'];
  45.             $zip = $_REQUEST['zip'];
  46.             $state = $_REQUEST['state'];
  47.             $phone = $_REQUEST['phone'];
  48.             $web_site = $_REQUEST['web_site'];
  49.            
  50.             $insert_user = insert_user($name,$company,$address,$city,$zip,$state,$phone,$web_site);
  51.            
  52.             if ($insert_user) {
  53.                
  54.                 $inserted = "done!";
  55.            
  56.             }
  57.             else {
  58.                
  59.                 $inserted = "error";
  60.                
  61.             }
  62.            
  63.         }
  64.         else {
  65.        
  66.             $inserted = "error";
  67.        
  68.         }
  69.  
  70.     }
  71.  
  72. ?>
  73. <html>
  74. <head>
  75. <title>Insert new user</title>
  76.  
  77. <script>
  78.  
  79.     function allow_form_submit(form_id) {
  80.    
  81.         if(confirm("Are you sure that all new user data is ok?")) {
  82.        
  83.             document.forms[form_id].submit();
  84.        
  85.         }
  86.         else {
  87.        
  88.             alert('You canceled registration')
  89.        
  90.         }
  91.        
  92.     }
  93.    
  94. </script>
  95.  
  96. </head>
  97. <body>
  98.    
  99.     <h1><?php print $inserted; ?></h1>
  100.    
  101.     <form action="" method="post" id="new_user_form">
  102.  
  103.         <p>
  104.             <label>Name:</label> <input type="text" name="name">
  105.         </p>
  106.        
  107.         <p>
  108.             <label>Company:</label> <input type="text" name="company">
  109.         </p>
  110.        
  111.         <p>
  112.             <label>Address:</label> <input type="text" name="address">
  113.         </p>
  114.        
  115.         <p>
  116.             <label>City:</label> <input type="text" name="city">
  117.         </p>
  118.        
  119.         <p>
  120.             <label>Zip:</label> <input type="text" name="zip">
  121.         </p>
  122.        
  123.         <p>
  124.             <label>State:</label> <input type="text" name="state">
  125.         </p>
  126.        
  127.         <p>
  128.             <label>Phone:</label> <input type="text" name="phone">
  129.         </p>
  130.        
  131.         <p>
  132.             <label>Website:</label> <input type="text" name="web_site">
  133.         </p>
  134.  
  135.     </form>
  136.    
  137.     <input type="button" name="chk_form" value="SAVE" onclick="allow_form_submit('new_user_form');" />
  138.  
  139. </body>
  140. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement