php - Database not updating with MYSQLI -
i'm beginner @ php , i'm using tutorial found on web work new piece of new cms i'm building. problem tutorial works fine when start alter use cannot connect database update. receive error "database error: unable update record." can add , delete not update.
index.php
<html> <head> <title>mysqli tutorial</title> </head> <body> <?php //include database connection include 'db_connect.php'; $action = isset($_get['action']) ? $_get['action'] : ""; if($action=='delete'){ //if user clicked ok, run our delete query $query = "delete family fid = ".$mysqli->real_escape_string($_get['fid']).""; if( $mysqli->query($query) ){ echo "user deleted."; }else{ echo "database error: unable delete record."; } } $query = "select * family"; $result = $mysqli->query( $query ); $num_results = $result->num_rows; echo "<div><a href='add.php'>create new record</a></div>"; if( $num_results ){ echo "<table border='1'>";//start table //creating our table heading echo "<tr>"; echo "<th>name</th>"; echo "<th>action</th>"; echo "</tr>"; //loop show each records while( $row = $result->fetch_assoc() ){ //extract row //this make $row['firstname'] //just $firstname extract($row); //creating new table row per record echo "<tr>"; echo "<td>{$name}</td>"; echo "<td>"; echo "<a href='edit.php?fid={$fid}'>edit</a>"; echo " / "; echo "<a href='#' onclick='delete_user( {$fid} );'>delete</a>"; echo "</td>"; echo "</tr>"; } echo "</table>";//end table }else{ //if table empty echo "no records found."; } //disconnect database $result->free(); $mysqli->close(); ?> <script type='text/javascript'> function delete_user( fid ){ //this script helps var answer = confirm('are sure?'); if ( answer ){ //if user clicked ok //redirect url action delete , fid record deleted window.location = 'index.php?action=delete&fid=' + fid; } } </script> </body> </html>
add.php
<html> <head> <title>mysqli create record</title> </head> <body> <!--we have our html form here user information entered--> <form action='#' method='post' border='0'> <table> <tr> <td>firstname</td> <td><input type='text' name='name' /></td> </tr> <td></td> <td> <input type='hidden' name='action' value='create' /> <input type='submit' value='save' /> <a href='index.php'>back index</a> </td> </tr> </table> </form> <?php $action = isset($_post['action']) ? $_post['action'] : ""; if($action=='create'){ //include database connection include 'db_connect.php'; //write query $query = "insert family set name = '" . $mysqli->real_escape_string($_post['name']) ."' "; if( $mysqli->query($query) ) { echo "user created."; }else{ echo "database error: unable create record."; } $mysqli->close(); } ?> </body> </html>
edit.php
<?php //include database connection include 'db_connect.php'; $action = isset( $_post['action'] ) ? $_post['action'] : ""; if($action == "update"){ //write query $query = "update users set name = '".$mysqli->real_escape_string($_post['name'])."', fid='".$mysqli->real_escape_string($_request['fid'])."'"; if( $mysqli->query($query) ) { echo "user updated."; }else{ echo "database error: unable update record."; } } $query = "select fid, name family fid='".$mysqli->real_escape_string($_request['fid'])."' limit 0,1"; $result = $mysqli->query( $query ); $row = $result->fetch_assoc(); $fid = $row['fid']; $name = $row['name']; ?> <!--we have our html form here new user information entered--> <form action='#' method='post' border='0'> <table> <tr> <td>firstname</td> <td><input type='text' name='name' value='<?php echo $name; ?>' /></td> </tr> <tr> <td></td> <td> <!-- identify record updated --> <input type='hidden' name='fid' value='<?php echo $fid ?>' /> <!-- set action edit --> <input type='hidden' name='action' value='update' /> <input type='submit' value='edit' /> <a href='index.php'>back index</a> </td> </tr> </table> </form>
>
i have been unable find proper language fix believe it's because i"m using form of multiples not familiar mysqli/php languages fix it.
thank
you need remove comma @ end of:
name = '".$mysqli->real_escape_string($_post['name'])."',
the query should be:
$query = "update users set name = '".$mysqli->real_escape_string($_post['name'])."' fid='".$mysqli->real_escape_string($_request['fid'])."'";
Comments
Post a Comment