You need to put the URL in the action attribute that processes the form, not the function:
action="<?php updater($_POST['name'],1); ?>" // not this action="<?php echo $_SERVER['PHP_SELF']; ?>" // path to this page
If it's on the same page, you can simply omit it or use $_SERVER['PHP_SELF'] and then intercept the form submission. Inside this process, your custom function is called.
if($_SERVER['REQUEST_METHOD'] === 'POST') { $value = $_POST['name']; $id = 1; updater($value, $id); }
A simple fix would just quote the line inside it:
$sql = "UPDATE table_name SET name='$value' WHERE id=$id";
But this is open to SQL injection, another way to make safer queries is to prepare them:
function updater($value,$id) { // Create connection $conn = new mysqli( 'localhost' , 'user_name' , '' , 'data_base_name' ); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "UPDATE table_name SET name = ? WHERE id= ?"; $update = $conn->prepare($sql); $update->bind_param('si', $value, $id); $update->execute(); if ($update->affected_rows > 0) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } }
Ghost
source share