Reputation: 47
I had a query which ran perfectly smooth, updating the database with a new image title, that the user gave. Which can be seen below:
<?php
$query = "SELECT * FROM `tblImage`";
$result = $conn -> query($query);
while($row = $result -> fetch_assoc())
{
?>
<form method="post" action="">
<a href="<?php echo $row['fldFilePath']; ?>" data-lightbox="gallery" data-title="<?php echo $row['fldName']; ?>"><img src="<?php echo $row['fldFilePath']; ?>" class="ImgRound"></a>
<label>Image Name: <?php echo $row['fldName']; ?></label>
<input name ="img-title" type ="text" placeholder="Enter New Image Title...">
<button type="submit" value ="<?php echo $row['fldName'] ?>" name="update_title" class="ImgRound">Update Title</button>
<button type="submit" value ="<?php echo $row['fldName'] ?>" name="delete" class="ImgRound">Delete</button>
</form>
<?php
if(isset($_POST['update_title']))
{
$imgTitle = $_POST['img-title'];
$stmt= "UPDATE `tblImage` SET `fldName` = '$imgTitle' WHERE `fldName` = '$_POST[update_title]' ";
$result = $conn -> query($stmt);
if($conn -> query($stmt))
{
header("Refresh:0");
}
}
}
?>
However, when I went back into the code to bind params for $stmt
, I've made a mistake somewhere in the code, the error message is telling me:
Array ( )
Warning: mysqli::query() expects parameter 1 to be string, object given
.
The update query that I tried which gave this message was:
$stmt = $conn->prepare("UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?");
$stmt->bind_param("ss", $imgTitle, $_POST['update-title']);
$stmt->execute();
if($conn -> query($stmt))
{
header("Refresh:0");
}
I've looked on here under this error message and I'm struggling to still make sense of what I've done.
Upvotes: 0
Views: 92
Reputation: 33813
Assign a variable to the prepare
method so that you can test to see that the statement object has been created successfully and then assign a variable to the return value of execute
to test that it too was successful.
$stmt = $conn->prepare( "UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?" );
if( $stmt ){
$stmt->bind_param("ss", $imgTitle, $_POST['update_title']);
$result = $stmt->execute();
if( $result )exit( header("Refresh:0") );
}
-- updated
<?php
$query = "SELECT * FROM `tblImage`";
$result = $conn->query($query);
while( $row = $result -> fetch_assoc() ) {
?>
<form method="post">
<a href="<?php echo $row['fldFilePath']; ?>" data-lightbox="gallery" data-title="<?php echo $row['fldName']; ?>">
<img src="<?php echo $row['fldFilePath']; ?>" class="ImgRound" alt='' />
</a>
<label>Image Name: <?php echo $row['fldName']; ?></label>
<input name="img-title" type="text" placeholder="Enter New Image Title..." />
<button type="submit" value="<?php echo $row['fldName'] ?>" name="update_title" class="ImgRound">Update Title</button>
<button type="submit" value="<?php echo $row['fldName'] ?>" name="delete" class="ImgRound">Delete</button>
</form>
<?php
} #close loop here ???
?>
<?php
if( isset( $_POST['update_title'], $_POST['img-title'] ) ) {
$imgTitle = filter_input( INPUT_POST, 'img-title', FILTER_SANITIZE_STRING );
$updateTitle = filter_input( INPUT_POST, 'update_title', FILTER_SANITIZE_STRING );
if( $imgTitle & $updateTitle ){
$stmt = $conn->prepare( 'UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?' );
if( $stmt ){
$stmt->bind_param( 'ss', $imgTitle, $updateTitle );
$result = $stmt->execute();
if( $result )exit( header("Refresh:0") );
}
$stmt->close();
}
}
?>
Upvotes: 1
Reputation: 147166
You shouldn't be trying to pass your $stmt
variable to query()
. To check the success of the UPDATE
, simply check the result of execute()
instead:
$stmt = $conn->prepare("UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?") or die($conn->error);
$stmt->bind_param("ss", $imgTitle, $_POST['update_title']) or die($stmt->error);
if ($stmt->execute())
{
header("Refresh:0");
}
else {
die($stmt->error);
}
Upvotes: 1
Reputation: 364
$stmt->bind_param("ss", $imgTitle, $_POST['update-title']);
You are giving 3 parameters here.
Shouldn't it be like this ..
$stmt = $conn->prepare("UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?");
$stmt->bind_param($_POST['update-title'], $imgTitle);
Assumed,
$_POST['update-title']
- the updated value.
$imgTitle
- to be updated value
Upvotes: 0