Bob Mwenda Bob Mwenda - 5 months ago 24
SQL Question

Getting data from POST [id] form in PHP

On a table that displays data from a database, I have a form that has a text area on which a user can type a receipt number and submit to save in a database for a specif row. The PHP code below is what updates the database after the form is submitted.
I want to pick the rest of the details for the specific row so I used the

$_POST['id']
on which the receipt has been submitted. The
id
is the primary key. I'm however having a challenge since I can't fetch data from the database using
$id = $_POST['id'];
I created before outside the function The
update
statement works perfectly but the
SELECT STATEMENT
doesn't . How do I go about it? Any one?

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

$rec = $_POST['receipt'];
$id = $_POST['id'];
//reate connection

$sql = "UPDATE customer SET `receipt` = '".$_POST['receipt']."', `date_entered` = NOW(), `receipt_lock` = 1 WHERE `id` = '".$_POST['tid']."' AND receipt_lock = 0";
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
exit();
$conn->close();
}


function wall(){

global $recp;
global $id;

// Create con

$sql = "SELECT * FROM customer WHERE id ='$id'";

$result1 = mysqli_query($conn, $sql);
$resultarr = mysqli_fetch_assoc($result1); // fetch data


$name = $resultarr['name'];
echo "$name"; //Does not display

$amount = $resultarr['amount'];
$transaction_id = $resultarr['trans_id'];
$date = $resultarr['time_paid'];
}
else {

echo "this is not right!;
}
wall();

Answer

Ignoring all the (valid) questions about SQL security and just addressing your problem - how about passing the $id variable as a parameter to your wall function.?

wall($id);

function wall($id){

    $sql = "SELECT * FROM customer WHERE id ='$id'";
    // ... use prepared statements for security...
    ...
}