I have been attempting to switch over to prepared statements, however I cant figure out why my new code no longer functions. I am new to using these and still learning but i understand it is the best practice for security. any help would be appreciated. Thank You.
<?php
$servername = "11.11.11.11";
$username = "root";
$password = "root";
$dbname = "sit";
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$result = mysqli_query($conn, "SELECT * FROM `ourstory` ");
$values = mysqli_fetch_array($result);
if(isset($_POST['ourstory_title'])){
$ourstory_title = $_POST['ourstory_title'];
$ourstory_testimonial = $_POST['ourstory_testimonial'];
$ourstory_content = $_POST['ourstory_content'];
$ourstory->execute();
$ourstory = $conn->prepare("UPDATE ourstory SET
ourstory_title='$ourstory_title' ,
ourstory_content='$ourstory_content' ,
ourstory_testimonial='$ourstory_testimonial'
WHERE ourstory_id='1'");
$ourstory->bind_param("sss", $ourstory_title, $ourstory_content, $ourstory_testimonial);
if (mysqli_query($conn, $ourstory)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
$ourstory->close();
$conn->close();
}
?>
<form id="comment_form" method="post"
action="<?php echo $ourstory?>"
onsubmit="setTimeout(function () {
window.location.reload();
}, 10), location.reload(true);">
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="85%">About Us Title</td>
</tr>
<tr>
<td>
<input class="commentarea"
name="ourstory_title" type="text"
id="ourstory_title" value="<?php echo $values['ourstory_title']?>">
</td>
</tr>
<tr>
<td width="85%" >Testimonial</td>
</tr>
<tr>
<td>
<pre>
<textarea class="commentarea"
name="ourstory_testimonial" type="text"
id="ourstory_testimonial" rows= "10" ><?php echo $values['ourstory_testimonial']?>
</textarea>
</pre>
</td>
</tr>
<tr>
<td width="85%" >About Us Content</td>
</tr>
<tr>
<td>
<pre>
<textarea class="commentarea" name="ourstory_content"
type="text" id="ourstory_content"
rows= "10" ><?php echo $values['ourstory_content']?>
</textarea>
</pre>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
In conjunction with Mark's answer, am submitting the following as a complimentary answer and using some of my comments left under the OP's question.
Firstly, <textarea>
does not have a type. type="text"
remove all of those.
Then, $ourstory->execute();
is misplaced, it needs to go after $ourstory->bind_param("sss",...
once you've used Mark's answer and using placeholders as stated in the answer and from the manual http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php
You shouldn't have if (mysqli_query($conn, $ourstory)) {
what you're looking to use is affected_rows
http://php.net/manual/en/mysqli.affected-rows.php in a conditional statement to check if the query was indeed successful.
From your edit: https://stackoverflow.com/revisions/31003865/4
printf("Affected rows (UPDATE): %d
", $ourstory->affected_rows);
$ourstory->execute();
this needs to go after executing:
$ourstory->execute();
printf("Affected rows (UPDATE): %d
", $ourstory->affected_rows);
but I would use a conditional if
for that and it should be the connection's variable, i.e. and from the manual:
int $mysqli->affected_rows;
so do:
printf("Affected rows (UPDATE): %d
", $conn->affected_rows);
Example from the manual:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
/* Insert rows */
$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %d
", $mysqli->affected_rows);
$ourstory = $conn->prepare("UPDATE ourstory SET ourstory_title='$ourstory_title' ,ourstory_content='$ourstory_content' ,ourstory_testimonial='$ourstory_testimonial' WHERE ourstory_id='1'");
$ourstory->bind_param("sss", $ourstory_title, $ourstory_content, $ourstory_testimonial);
If you're binding values to your prepared statement, you need to set placeholders in that query.... not inject the values themselves and then try binding them
$ourstory = $conn->prepare("UPDATE ourstory SET ourstory_title=? ,ourstory_content=? ,ourstory_testimonial=? WHERE ourstory_id='1'");
$ourstory->bind_param("sss", $ourstory_title, $ourstory_content, $ourstory_testimonial);
And you might as well bind the value for ourstory_id
as well