1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

some html php and mysql mixed question

Discussion in 'Programming & Webmastering' started by r9, Aug 30, 2009.

  1. r9

    r9

    Joined:
    Jul 28, 2008
    Messages:
    2,144 (0.96/day)
    Thanks Received:
    284
    I have a FORM in HTML file that has CHECKBOX:

    <body>
    <form id="form1" name="lastname" method="post" action="delete1.php">
    <label>Label
    <select name="lastname">
    <option value="Ristovski">Ristovski</option>
    <option value="Petacka">Petacka</option>
    </select>
    </label>
    <input type="submit" />
    </form>

    that I want to send data to PHP file delete.php

    <?php
    $con = mysql_connect("localhost","root","00000000");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("proba", $con);

    $sql="DELETE FROM Persons WHERE LastName='$_POST[lastname]' AND FirstName='$_POST[firstname]'";

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record DELETED";
    ?>

    The problem is in my html file. I`m new to this and I don`t know how to assign multiple names and values to one radio button so when I submit it would send that in to the PHP file LastName='$_POST[lastname]' AND FirstName='$_POST[firstname]'";
    I just know how to set checkbox how to send just last name or first name. The question is how to send both with just one checkbox selected. I hope this make sense :D.
  2. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,413 (1.32/day)
    Thanks Received:
    636
    Location:
    Geneva, FL, USA
    Your Persons table has a primary key? Use that instead of a first/last name combination.

    HTML:
    <select name="person_id">
    	<option value="1">Ristovski</option>
    	<option value="2">Petacka</option>
    </select>
    PHP:
    <?php
    $con 
    mysql_connect("localhost","root","00000000");

    if (!
    $con) {
        die(
    'Could not connect: ' mysql_error());
    }

    mysql_select_db("proba"$con);

    $sql "DELETE FROM Persons WHERE person_id = ".$_POST['person_id'];

    if (!
    mysql_query($sql,$con)) {
        die(
    'Error: ' mysql_error());
    }

    echo 
    "1 record DELETED";
    ?>
    There's no protection from SQL injection, nor verification of deletion (anyone could call delete.php?person_id=x, with x = 1 to 2^64 to clear your table out), so keep that in mind if these files are accessible by the outside world.
    r9 says thanks.
  3. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,475 (6.26/day)
    Thanks Received:
    3,430
    Location:
    IA, USA
    FYI:
    HTML:
    <select name="person_id">
    	<option value="1" selected>Ristovski</option>
    	<option value="2">Petacka</option>
    </select>
    Ristovski would be selected by default.

    I usually do names like "checkbox{ID}" or "firstname{ID}" or "lastname{ID}" That way every single named object in the form has a unique name like checkbox5, firstname100, lastname9. You have everything you need now to work with it (name, id, and value).
    r9 says thanks.
    Crunching for Team TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page