Specifying ID in Mysqli query in php -
i'm working on cms site, i've got blog posts store in database. can create, edit , delete them. there's issue though when want edit them. can't specify clause in update query match id of blog post i'm trying edit!
suppose i've got blog post id of '5'. if write code it, works way should.
$sqledit = "update paginas set message='$_post[message]' id= $_post[id]";
but don't want edit blog post #5, want edit blog post i'm updating. seems me should work,
where id= $_post[id]";
... doesn't.
that throws me undefined id error. shouldn't because can delete blog posts exact same way particular code:
$sqldel = "delete `paginas` id= $_post[id]";
this allow me to.
the code below on blog page, edit query in own edit.php page
if (isset($_post['edit'])) // if pressed, execute { echo '<br><br> <div class="blogscript"> <form action="edit.php" method="post">edit stuff<br> <input type="text" placeholder='. $pagetitle . ' ><br><br> <textarea id="message2" name="message"><p>' . $message . '</p></textarea><br> <input type="submit" name="editsubmit" value="confirm" /> <input type="hidden" name="id" value="' . $id . '">. </form></div>'; }
i forward tips should try out.
edit:
this edit.php page
<?php $db_host = "localhost"; $db_user = "root"; $db_pass = ""; $db_name = "cmsbase"; $mysqli_con = new mysqli($db_host,$db_user,$db_pass,$db_name); if($mysqli_con->connect_errno) { die("error : -> ".$mysqli_con->connect_error); } $sql = "update paginas set message='$_post[message]' id= $_post[id]"; if ($mysqli_con->query($sql) === true) { echo ""; } else { echo "error: " . $sql . "<br>" . $mysqli_con->error; } $mysqli_con->close(); //close connection echo "<script>alert('edited');location.href='index.php';</script>"; ?>
in order values present in $_post
, need have element (e.g. <input>
, <select>
, <textarea>
) inside form name
attribute set $_post
key want.
you can add hidden input form id.
<input type='hidden' name='id' value='" . $id . "'>
assuming getting $message
variable shown in form code selecting database, should able id there well, or potentially $_get
if how determine post being displayed.
Comments
Post a Comment