USERRR5
USERRR5

Reputation: 430

php post method in update query is not working

my database insert has one table table1. table1 has 3 columns user,id,time i want to update time column of table1 by posting a value from a php file but my update query is showing error:

Error: UPDATE table1 set time=1:40 WHERE id=12 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':40 WHERE id=12' at line 1

i have already inserted id column value in table1 ..what should i do?here is my code

update.php

<html>
<body>

<form action="newadd.php" method="post">
Name: <input type="text" name='name'><br>
<input type="submit" value="submit">
</form>

</body>
</html> 

insertupdate.php

 <?php
include('insertjoincon.php');
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$tim = $_POST['name'];
$sql = "UPDATE table1 set time=$tim WHERE id=12";

if ($conn->query($sql) == TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?> 

but when i use $tim = 4 then table updates but when use post then it shows error.

Upvotes: 1

Views: 1514

Answers (3)

Manoj Kushwaha
Manoj Kushwaha

Reputation: 114

Not Proper Syntax So Try this Code:

$tim = $_POST['name'];
$sql = "UPDATE table1 set time='".$tim."' WHERE id=12";

or

$sql = "UPDATE table1 set time='$tim' WHERE id=12";

Upvotes: 3

pavel
pavel

Reputation: 27092

Missing quotes around time (and don't forget to sanitize data from users).

$sql = "UPDATE table1 set time='" . $conn->real_escape_string($tim) . "' WHERE id = 12";

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31749

The error is coming because of : in the value. Wrap it with 's. It would be -

set time='1:40' 

$sql = "UPDATE table1 set time='$tim' WHERE id=12";

Upvotes: 1

Related Questions