Jump to content

insert form values into database


DimitriDV

Recommended Posts

execute this once..

 

create_database.php

<?php

$sqla="create database test";
$resa=mysql_query($sqla)or die(mysql_error());

$sqlb="use test";
$resb=mysql_query($sqlb);


$sqlc="create table users(
id int auto_increment primary key,
name varchar(50) not null,
surname varchar(50) not null,
email varchar(50) not null
);";

$resc="mysql_query($sqlc)or die(mysql_error())";

?>

 

 

 

Gotto this add a user...

 

add_user.php


<!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=iso-8859-1" />
</head>
<body>
<div class="header"><h1>User Insert Test</h1></div>

<?php


if(is_file('create_database.php')){

echo " PLEASE DELETE THE FILE NAME CREATE_DATABASE.PHP <BR>
       BEFORE WE GO ANY FUTHER THANK YOU!";
}


//database
$db=mysql_connect("localhost","username","password");
$res=mysql_select_db("test",$db);


// submit button
if(isset($_POST['submit'])){


//post varables.

   $name = mysql_real_escape_string($_POST['name']);
   $surname = mysql_real_escape_string($_POST['surname']);
   $email = mysql_real_escape_string($_POST['email']);

   
   //insert database..
   
   $query="INSERT INTO users(id,name, surname, email) VALUES ('$id','$name','$surname','$email')";
   $result = mysql_query($query) or die(mysql_error());   

}

?>

<form action="<?php $_SERVER['PHP_SELF'];?>" method="POST">

<table cellspacing="0" cellpadding="3" class="tbl_survey">
<tr>
<td>Name:</td>
<td><input type="text" name="name"  /></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname"  /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="ADD User" /></td>
</tr>
</table>
</form>
</body>
</html>

 

 

 

Goto this to delete a selected  user....

 

delete_user.php


<!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=iso-8859-1" />
</head>
<body>
<div class="header"><h1>User Delete Test</h1></div>
<?php session_start();

include('connect_db.php');

    $name =mysql_real_escape_string($_POST['name']);
   
   $surname = mysql_real_escape_string($_POST['surname']);
   
   $email = mysql_real_escape_string($_POST['email']);

   
   $query="SELECT * FROM users";
   
   $result = mysql_query($query, $DBC) or die("Cannot connect: " . mysql_error());
   
   while($data=mysql_fetch_assoc($result)){
      
      
      $_SESSION['email_address']=$data['email'];
   
   
   echo "$name <br> $surname <br> $email <br><br>
   
   
   <a href='".$_SERVER['PHP_SELF']."?cmd=del&email=".md5($_SESSION['email_address'])."'>DELETE ENTRY</a> <br><br>"; 
   
}

if($_GET['cmd']=="del"){
   
   
   $query="DELETE FROM users WHERE email = '".md5($_GET['email'])."'"; 
   $res=mysql_query($query)or die(mysql_error());
   
   if(mysql_affected_rows($res)){
      
   echo " Entry deleted!";
      
   }else{
   
      echo "Entry not deleted!";
   
   }
}
   
?>
</body>
</html>

Link to comment
Share on other sites

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.