Reputation:
I have table called book
with this structure and data:
id_book = 1
name = dragon
type =
and I have form in other.php with url already other.php?id=1
and here I want insert a value into "type" where id = 1
if( isset($_POST['submit']) ) {
$id_book = $_POST['id_book'];
$type = $_POST['type'];
if(!empty(trim($typei))) {
if(add2($id_book, $type)) {
echo "success";
}else{
echo "fail";
}
}else{
echo "check data";
}
}
$result = show_per_id($_GET['id']);
while($row = mysqli_fetch_assoc($result)) {
?>
<form class="" action="" method="post>
<div class="form-group">
<input type="text" class="form-control" placeholder="" name="id_book" value="<?php echo $row['id_book']?>" Required autofocus>
</div>
<div class="form-group">
<input type="text" class="form-control" name="type" value="" readonly>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-info btn-block" value="confirm">
</div>
</form>
<?php
}
and I have function add2()
:
function add2($id_book, $type) {
global $connect;
$query= "UPDATE book SET type='$type' WHERE id='$id_book'";
if( mysqli_query($connect, $query) ){
return true;
}else{
return false;
}
}
My question here, how do I insert data type? Why do my attempts to INSERT always fail?
Upvotes: 0
Views: 72
Reputation: 47894
TYPE
is a mysql keyword, you should use backticks around your column name.
`type`
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
If this doesn't fix your issue, go to your phpMyAdmin and run your query directly on your database. If there is anything wrong with the syntax, it will tell you. (of course, you could also run error checks and check for affected rows in your php -- as coders normally do)
After all that, if you are still stuck, do an EXPORT of your table structure & data, and post that information into your question.
Upvotes: 2