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

some html php and mysql mixed question

r9

Joined
Jul 28, 2008
Messages
3,300 (0.53/day)
System Name Primary|Secondary|Poweredge r410|Dell XPS|SteamDeck
Processor i7 11700k|i7 9700k|2 x E5620 |i5 5500U|Zen 2 4c/8t
Memory 32GB DDR4|16GB DDR4|16GB DDR4|32GB ECC DDR3|8GB DDR4|16GB LPDDR5
Video Card(s) RX 7800xt|RX 6700xt |On-Board|On-Board|8 RDNA 2 CUs
Storage 2TB m.2|512GB SSD+1TB SSD|2x256GBSSD 2x2TBGB|256GB sata|512GB nvme
Display(s) 50" 4k TV | Dell 27" |22" |3.3"|7"
VR HMD Samsung Odyssey+ | Oculus Quest 2
Software Windows 11 Pro|Windows 10 Pro|Windows 10 Home| Server 2012 r2|Windows 10 Pro
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.
 
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.
 
  • Like
Reactions: r9
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).
 
  • Like
Reactions: r9
Back
Top