I would like to add data to a database when user clicks button in table.
I am creating a portfolio for stocks in Wordpress and a user should be able to add stocks to their portfolio.
This is my insert_to_db.php file where the query is. get_current_user_id() gets the User_ID which is used to identify the user.
global $wpdb;
<?php
if(isset($_POST['id'])){
echo "You clicked button one!" ;
$sql = $wpdb->prepare("INSERT INTO user_portfolio (Name, Price, Date, User_ID)
VALUES ('$name','$price','$date',get_current_user_id())");
$results = $wpdb->get_results( $sql );
$results -> execute();
}
else {
echo" dhur";
}
?>
Below is my function that insert data to a front-end table, including the button user presses to add Name
and Price
to a database
function trendy2($Name, $Price, $user_id){
$link = "https://signal-invest.com/tick/?ticker=";
echo "<tr>
<td>$Name</td>
<td>$Price</td>
<td><form method='POST' action='insert_to_db.php'>
<input type='submit' name='id' value='$user_id'/>$user_id
</form>
</td></tr>";}
Html:
<table >
<tr>
<th>NAME</th>
<th>PRICE TODAY</th>
<th>ADD TO PORTFOLIO</th>
</tr>
<?php
foreach ($results_query_uptrend as $r){
$name = $r["Name"];
$price = $r['Price'];
echo trendy2($name, $price, $user_id);
}
?>
</table>
Right now, when button is clicked I get redirected to front-page and data is not added to MySQL Database.