mysql - php insert statement has correct arguments but insert is returning false -


ok insert should fine returning false, $quote holds value have checked. , database here hope pair of eyes can spot this. have been using same insert style other php files, works fine. 1 tricky...issit because of foreign keys?

    <?php  $quote = $_post["quote"]; require "init.php";  $query = "insert `quote`(`quote_description`) values('".$quote."');"; $result = mysqli_query($con,$query);   //"insert `quote`(`quote_description`) values ('".$quote."');";  if($result) {     $response = array();     $code = "submit_true";     $message = "quote success, click ok continue";     array_push($response,array("code"=>$code,"message"=>$message));     echo json_encode(array("server_response"=>$response));   }  else{     $response = array();     $code = "submit_false";     $message = "sorry, server error occurred, please try again";     array_push($response,array("code"=>$code,"message"=>$message));     echo json_encode(array("server_response"=>$response));   } mysqli_close($con);  ?> 

i getting following error after checking errors:

cannot add or update child row: foreign key constraint fails (mydb.quote, constraint fk_quote_user foreign key (user_id) references user (user_id) on delete no action on update no action)

when using insert in mysql, have include columns , values inserted. try this;

$query = "insert quote(quote_id, quote_description, quote_points, user_id, quote_of_day_id) values(null, '".$quote."', '".$points."', '".$user_id."', '".$qod_id."');"; 

if don't have values inserted @ time query, set them nothing. add values later use update:

$query = "update quote set user_id = 8 quote_id = 6"; 

though solution inserting, insecure , vulnerable sql injection. here better object-oriented solution more secure, , suggest using on other insecure inserts:

$db = new mysqli($host, $user, $pass, $database); $stmt = $db->prepare('insert quote(quote_id, quote_description, quote_points, user_id, quote_of_day_id) values(?, ?, ?, ?, ?);'); $stmt->bind_param('isiii', $quote_id, $quote, $points, $user_id, $qod_id);  $stmt->execute(); 

also idea add handle post data:

if(!isset($_post["quote"])) {     echo "data not received correctly";     exit(); } 

hope these examples you. wish best.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -