php - Can't insert and update at the same time -
i have code below , can't figure why it's not working. problem it's can insert post it, when try update post it's create new page instead update.
i have tried remove isset
if(isset($_post['id']) != 'null')
, update work, insert doesn't work anymore.
any idea it's wrong code? thanks.
<?php if(isset($_post['submitted']) == 1) { $title = mysqli_real_escape_string($dbc, $_post['title']); $header = mysqli_real_escape_string($dbc, $_post['header']); $body = mysqli_real_escape_string($dbc, $_post['body']); if(isset($_post['id']) != 'null') { $q = "update pages set user = $_post[user], title = '$title', header = '$header', body = '$body' id = $_get[id]"; } else { $q = "insert pages (user, title, header, body) values ($_post[user], '$title', '$header', '$body')"; } $r = mysqli_query($dbc, $q); if($r) { $message = '<p>page added!</p>'; } else { $message = '<p>page not added because:</p>'.mysqli_error($dbc); $message .= '<p>'.$q.'</p>'; } } ?>
you using post , @ same time. first check whethet post or get. isset() check
<?php if(isset($_post['submitted']) == 1) { $title = mysqli_real_escape_string($dbc, $_post['title']); $header = mysqli_real_escape_string($dbc, $_post['header']); $body = mysqli_real_escape_string($dbc, $_post['body']); if(isset($_get['id']) && $_get['id']!="") { $q = "update pages set user = $_post[user], title = '$title', header = '$header', body = '$body' id = $_get[id]"; } else { $q = "insert pages (user, title, header, body) values ($_post[user], '$title', '$header', '$body')"; } $r = mysqli_query($dbc, $q); if($r) { $message = '<p>page added!</p>'; } else { $message = '<p>page not added because:</p>'.mysqli_error($dbc); $message .= '<p>'.$q.'</p>'; } } ?>
Comments
Post a Comment