Get the solution ↓↓↓
I have a table that I select from my database and want the user to update just 1 of the columns. I echo the rows into the table with a user input box that I have added at the end of each row. I have tried foreach loops and end up with an error "Fatal error: Uncaught Error: Object of class mysqli_result could not be converted to string in..." When I print_r the array of the user inputs, it displays, but I'm struggling to use it within prepared statements.
<?php
if (isset($_POST['save'])){
if(!empty($_POST['newbid'])) {
$biduserID = $_SESSION['id'];
$itemID = $_GET['ItemId'];
$bidprice = ($_POST['newbid']);
$getcurrentround = "SELECT `Round` FROM `RoundCounter` WHERE `ItemID` =".$_GET['ItemId']."";
$currentroundresult= $db->query($getcurrentround);
$currentround = $currentroundresult->fetch_assoc();
$currentround1 = $currentround['Round'];
$biddername = $_SESSION["id"];
$count = $_POST['count'];
$newbid = $_POST['newbid']; // check empty and check if interger
print_r($newbid);
$getusername = "SELECT `Username` FROM `User` WHERE `UserID` = `$biddername`";
$username1= $db->query($getusername);
$getbandname = "SELECT `BandName` FROM `BidTables` WHERE `ItemID` =" .$_GET['ItemId']."";
$bandname= $db->query($getbandname);
$getnumberlots = "SELECT numberlots FROM `Item` WHERE `ItemID` =".$_GET['ItemId']."";
$numberlots= $db->query($getnumberlots);
$bid = 1;
foreach($_POST as $bid => $value) {
$sql4 = "INSERT INTO BidTables (`BandName`,`BidderID`, `ItemID`, `BidPrice`, `Round`, `Username`) VALUES
(?,?,?,?,?,?)";
$stmt = $db->prepare($sql4);
echo $db->error;
$stmt->bind_param("siiiis", $bandname, $biduserID, $itemID, $bid ,$currentround1, $username1 );
$stmt->execute();
}
}
?>
and here is the submit button with the table data:
<form action="" method="POST">
<table class="table table-hover">
<thead class="thead">
<tr class="header">
<th>ID</th>
<th>Band</th>
<th>Current Price</th>
</tr>
<?php
$sql = "SELECT * FROM BidTables WHERE ItemID = ".$_GET['ItemId']." ORDER BY `Round` DESC";
$resultSQL= mysqli_query($db, $sql);
if(mysqli_num_rows($resultSQL) > 0){
}
?>
</thead>
<tbody>
<!-- PHP CODE TO FETCH DATA FROM ROWS -->
<tr>
<?php
// LOOP TILL END OF DATA
while($row = $resultSQL->fetch_assoc()) {
?>
<tr>
<td><?=$row['bidtable']?></td>
<td><?=$row['BandName']?></td>
<td><?=$row['BidPrice']?></td>
<td><input type="number" name="newbid[]" size="10" /></td>
</tr>
<?php } ?>
</table>
<input type="hidden" name="count" value="<?=$resultSQL->num_rows?>" />
<button class="btn btn-primary btn-lg" name="save">Submit</button>
</form>
I appreciate any help/pointers
EDIT 1: I have used prepared statements for each SELECT, and I am getting the error for each statement in my INSERT function: "Warning: Array to string conversion in..."
This is the code for the prepared statements:
<?php
if (isset($_POST['save'])){
if(!empty($_POST['newbid'])) {
$biduserID = $_SESSION['id'];
$itemID = $_GET['ItemId'];
$bidprice = ($_POST['newbid']);
$count = $_POST['count'];
$newbid = $_POST['newbid']; // check empty and check if interger
print_r($newbid);
$sql6 = "SELECT `Round` FROM `RoundCounter` WHERE `ItemID` =?"; // SQL with parameters
$stmt6 = $db->prepare($sql6);
$stmt6->bind_param("i", $itemID);
$stmt6->execute();
$result6 = $stmt6->get_result(); // get the mysqli result
$round = $result6->fetch_assoc(); // fetch data
$sql7 = "SELECT `Username` FROM `User` WHERE `UserID` = ?"; // SQL with parameters
$stmt7 = $db->prepare($sql7);
$stmt7->bind_param("i", $biduserID);
$stmt7->execute();
$result7 = $stmt7->get_result(); // get the mysqli result
$username = $result7->fetch_assoc(); // fetch data
$sql8 = "SELECT `BandName` FROM `BidTables` WHERE `ItemID` =?"; // SQL with parameters
$stmt8 = $db->prepare($sql8);
$stmt8->bind_param("i", $itemID);
$stmt8->execute();
$result8 = $stmt8->get_result(); // get the mysqli result
$bandname = $result8->fetch_assoc(); // fetch data
$sql9 = "SELECT numberlots FROM `Item` WHERE `ItemID` =?"; // SQL with parameters
$stmt9 = $db->prepare($sql9);
$stmt9->bind_param("i", $itemID);
$stmt9->execute();
$result9 = $stmt9->get_result(); // get the mysqli result
$numberlots = $result9->fetch_assoc(); // fetch data
foreach($_POST as $bid => $value) {
$sql4 = "INSERT INTO BidTables (`BandName`,`BidderID`, `ItemID`, `BidPrice`, `Round`, `Username`) VALUES
(?,?,?,?,?,?)";
$stmt = $db->prepare($sql4);
echo $db->error;
$stmt->bind_param("siiiis", $bandname, $biduserID, $itemID, $bid ,$round, $username );
$stmt->execute();
}
}
}
?>
Someone kindly helped me find the problem, I was preparing my tables wrongly. The final loop looked like this:
foreach ($newBidIds as $id){
$insertStmt = $db->prepare("INSERT INTO BidTables (`BandName`,`BidderID`, `ItemID`, `BidPrice`, `Round`, `Username`) VALUES (?,?,?,?,?,?) ;");
$insertStmt->bind_param("siiiis", $bandname1, $bidUserID, $itemID, $id,$round1, $username);
$insertStmt->execute();
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.