Results 1 to 8 of 8

Thread: Need Help Creating PHP UPDATE & Delete Statements

  1. #1
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Exclamation Need Help Creating PHP UPDATE & Delete Statements

    Hey guys I need help creating a PHP function/statement to update a record in my database and also one to delete a record from the database.

    I already got the add record to the database working.


    HTML FORM CODE

    HTML Code:
    <head>
    <title>Change Person</title>
    </head>
    <body>
    
    <form action="change_person.php">
    ID: <input type="text" name="id" />
    Name: <input type="text" name="name" />
    Gender: <input type="text" name="gender" />
    Age: <input type="text" name="age" />
    <input type="submit" />
    </form>
    
    </body>
    HTML Code:
    <head>
    <title>Add Person</title>
    </head>
    <body>
    
    <form action="add_person.php">
    ID: <input type="text" name="id" />
    Name: <input type="text" name="name" />
    Gender: <input type="text" name="gender" />
    Age: <input type="text" name="age" />
    <input type="submit" />
    </form>
    
    </body>

    PHP CODE ADD PERSON

    PHP Code:
    <HTML>
    <HEAD>
        <TITLE> Add Person</TITLE>
    </HEAD>
    <BODY>

    <?php
    include('connect.php');    
    $id=$_GET['id'];
    $name=$_GET['name'];
    $gender=$_GET['gender'];
    $age=$_GET['age'];

    $query="INSERT INTO person (id,Name,Gender,Age) VALUES ('$id','$name','$gender','$age')";

     if(
    pg_query($query)) {
            echo 
    "User successfully added";
       
         }
        else {
             echo 
    " ERROR: Duplicate Person";
             
        }

    ?>

    </BODY>
    </HTML>
    I messed around trying to get the delete and update function working but not getting anywhere. Can anyone provide some help.
    Last edited by King_Jay16; Mar 12, 2012 at 11:09 PM.

  2. #2
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    any takers on this on?

  3. #3
    Yung_Jah Guest

    Default

    Your way of coding may be the shorter version I guess....but learning it at school now....for the last coding that you posted....in mine I have something like this...I didnt copy all the coding


    <?php

    $fname = $_POST["fname"];
    $lname = $_POST["lname"];
    $email = $_POST["email"];
    $address = $_POST["address"];
    $parish = $_POST["parish"];
    $homephone = $_POST["homephone"];
    $mobilephone = $_POST["mobilephone"];

    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Sorry but i could not connect to your server: ' . mysql_error());
    }
    mysql_select_db("applicants");



    mysql_query("INSERT INTO students (Fname, Lname, Email, Address, Parish, Home, Mobile)
    VALUES ('$fname','$lname','$email','$address','$parish',' $homephone','$mobilephone')");
    If you notice where [mysql_select_db("applicants");] is.....applicants is the name of the database....while students is the name of the table. Dont see the name of the database in yours.

    Where I have [ mysql_query("INSERT INTO ] that ends with another bracket at the end if you notice...dont know if it makes a difference...though you only have [ $query="INSERT ]. Maybe thats where you are having the issue.

    But for your coding, maybe this video may help you....http://www.youtube.com/watch?v=mn0ucCuNOTI. At first I started to use this....but for my assignment, I got lost along the way.

    Hope this will help.
    Last edited by Yung_Jah; Mar 13, 2012 at 07:45 PM.

  4. #4
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    Quote Originally Posted by Yung_Jah View Post
    Your way of coding may be the shorter version I guess....but learning it at school now....for the last coding that you posted....in mine I have something like this...I didnt copy all the coding



    If you notice where [mysql_select_db("applicants");] is.....applicants is the name of the database....while students is the name of the table. Dont see the name of the database in yours.

    Where I have [ mysql_query("INSERT INTO ] that ends with another bracket at the end if you notice...dont know if it makes a difference...though you only have [ $query="INSERT ]. Maybe thats where you are having the issue.

    But for your coding, maybe this video may help you....http://www.youtube.com/watch?v=mn0ucCuNOTI. At first I started to use this....but for my assignment, I got lost along the way.

    Hope this will help.
    thanks will test the later and keep it as a reference... I however met with my Professor and got it sorted out. See the code below


    Change/Update Record

    HTML Code:
    <HTML>
    <HEAD>
        <TITLE> Change Person</TITLE>
    </HEAD>
    <BODY>
    
    <?php
    
    	
    include("connect.php");
    	$id=$_GET["id"];
    	$name=$_GET["name"];
    	$age=$_GET["age"];
    	$gender=$_GET["gender"];
    	
    	$query="UPDATE person SET name= $1, gender= $2, age=$3  WHERE id=$4";
    	pg_query_params($query,array($name,$gender,$age,$id));
    	
    ?>
    
    </BODY>
    </HTML>
    As for the name of the database and connection items, those are included in the connect.php file, that way you only update your credentials etc in one place

  5. #5
    Yung_Jah Guest

    Default

    Cool....good to know you got through. There is always a shorter version to do things.

  6. #6
    Yung_Jah Guest

    Default

    But I am still puzzles...I dont see the name of the database....I am only seeing the name of the table which is "person"

  7. #7
    Join Date
    Feb 2008
    Posts
    725
    Rep Power
    17

    Default

    Quote Originally Posted by Yung_Jah View Post
    But I am still puzzles...I dont see the name of the database....I am only seeing the name of the table which is "person"
    The database name would be located in the connect.php file. like King_Jay16 previously stated.

    @King_Jay16
    Code:
    ......
    //Delete from database
    	$query_delete="DELETE FROM person WHERE id='$ID' ";
    	mysql_query($query_delete);......
    
    or
    $query_delete="DELETE FROM person WHERE id=$4 ";
    	pg_query_params($query_delete(array($name,$gender,$age,$id));
    and that would be your delete based on the last data shown.

    if both operations where happening on a form then you would set it to a specific button using IF conditions

    if ( isset($_POST['Delete_Details']))
    {

    }

    'Delete_Details' would be the name of the button the user needs to press.
    Last edited by Dre'; Mar 21, 2012 at 05:21 AM. Reason: code tags

  8. #8
    Yung_Jah Guest

    Default

    Oh zeen...respect still.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •