redarrow Posted December 17, 2008 Share Posted December 17, 2008 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 https://forums.phpfreaks.com/topic/137332-insert-form-values-into-database/page/2/#findComment-717666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.