php - how to link mysql 'id' to html element -


i have database rows of data display on webpage. now, looking way link each 'id' field in mysql button when button clicked, php script run deletes row of mysql information associated id.

i know incorrect think close. don't know php portion inside id tag. help?

<form action="remove.php" method="post">    <input type="submit" value="remove entry" id="<?php $row['id'] ?>" /> </form> 

am on right path? remove.php like...

<?php  $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if($conn === false){    die("error: not connect. " . mysqli_connect_error()); }  $sql = "delete newcars (stock, year, make, model, trim)         ('$_post[id] = $row[id]');  if(mysqli_query($conn, $sql)){     echo "records deleted successfully."; }   else {     echo "error: not execute $sql. " . mysqli_error($link); }  mysql_close($conn) ?> 

any appreciated. thank you!

html:

<form action="remove.php" method="post">    <input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>">    <input type="submit" value="remove entry" /> </form> 

you want pass id in form element, not submit button.

the php - , more secure original code uses prepared statements.

<?php  $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if($conn === false) {     die("error: not connect. " . mysqli_connect_error()); }  $stmt = $conn->prepare("delete newcars id = ?"); // prepare() can fail because of syntax errors, missing privileges, .... if(false === $stmt) {     // , since following operations need valid/ready statement object     // doesn't make sense go on     // might want use more sophisticated mechanism die()     // but's it's example     die('prepare() failed: ' . htmlspecialchars($mysqli->error)); }  $rc = $stmt->bind_param('i', $_post['id']); // bind_param() can fail because number of parameter doesn't match placeholders in statement // or there's type conflict(?), or .... if(false === $rc) {     // again execute() useless if can't bind parameters. bail out somehow.     die('bind_param() failed: ' . htmlspecialchars($stmt->error)); }  $rc = $stmt->execute(); // execute() can fail various reasons. , may stupid tripping on network cable // 2006 "server gone away" option if(false === $rc) {     die('execute() failed: ' . htmlspecialchars($stmt->error)); }  $stmt->close();  //redirect page view page ?> 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -