使用MYSQLi更新数据库

时间:2014-02-15 10:46:24

标签: php syntax-error

我一直在更新我的会员网站,以便它可以与mysqli一起使用。我是php和mysql的新手。 我有一个页面,用户可以通过发布到send_post.php的表单编辑他们的信息。 谁能告诉我我的代码有什么问题?我只是在第7行的send_post.php中出现了白屏和语法错误,'意外','。

这是我的表单页面。

<?php
// See if they are a logged in member by checking Session data
include_once("php_includes/check_login_status.php");
if (isset($_SESSION['username'])) {
// Put stored session variables into local php variable
$username = $_SESSION['username'];
}
//Connect to the database through our include 
include_once "php_includes/db_conx.php";
// Query member data from the database and ready it for display
$sql = "SELECT * FROM members WHERE username='$username' AND activated='1' LIMIT 1";
$user_query = mysqli_query($db_conx, $sql);
// Now make sure that user exists in the table
$numrows = mysqli_num_rows($user_query);
if($numrows < 1){
echo "That user does not exist or is not yet activated, press back";
exit();

}
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$state = $row["state"];
$city = $row["city"];
$name = $row["name"];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.png">

<title>Edit</title>
</head>
<body>
<br>
<div class = "container">
<div align="center">
   <h3><br />
     Edit your account info here<br />  
   <br />
   </h3>
 <table align="center" cellpadding="8" cellspacing="8">
  <form action="send_post.php" method="post" enctype="multipart/form-data" name="form" 
   id="form">

  <tr>
      <td><div align="right">Name:</div></td>
      <td><input name="city" type="text" id="city" value="<?php echo "$name"; ?>"    
  size="30" maxlength="24" /></td>
    </tr>
    <tr>
      <td><div align="right">State:</div></td>
      <td><input name="state" type="text" id="state" value="<?php echo "$state"; ?>"  

    size="30" maxlength="64" /></td>
    </tr>  
    <tr>
      <td><div align="right">City:</div></td>
      <td><input name="city" type="text" id="city" value="<?php echo "$city"; ?>"  
     size="30" maxlength="24" /></td>
    </tr>               
    <tr>
      <td>&nbsp;</td>
      <td><input name="Submit" type="submit" value="Submit Changes" /></td>
    </tr>
  </form>
  </table>
 </div>
</div>
 </body>
 </html>

这是表单处理页面。 send_post.php

 <?php 
  if ($_POST['state']) {
 $city = $_POST['city'];
 $name = $_POST['name'];
//Connecting to sql db.
$connect = mysqli_connect("localhost","username","password","database");
$mysqli_query=($connect,"UPDATE members (`state`, `city`, `name` WHERE      
username='$username'");
VALUES ('$state', '$city', '$name')";
mysqli_query($connect, $query);
mysqli_close($connect);
echo "Your information has been successfully added to the database."; 
?>

2 个答案:

答案 0 :(得分:0)

更改

$mysqli_query=($connect,"UPDATE members (`state`, `city`, `name` WHERE      
username='$username'");
VALUES ('$state', '$city', '$name')";

$mysqli_query=($connect,"UPDATE members (`state`, `city`, `name`) VALUES ('$state', '$city', '$name') WHERE username='$username' ");

答案 1 :(得分:0)

在表单中添加隐藏字段

<input type="hidden" name="username" value="<?php echo $username; ?>">

更改send_post.php

<?php
    //checking that all the fields have been entered
    if( isset( $_POST['state'] ) && !empty( $_POST['state'] ) )
    {
        $state = $_POST['state'];
    }

    if( isset( $_POST['city'] ) && !empty( $_POST['city'] ) )
    {
        $city = $_POST['city'];
    }

    if( isset( $_POST['name'] ) && !empty( $_POST['name'] ) )
    {
        $name = $_POST['name'];
    }

    if( isset( $_POST['username'] ) && !empty( $_POST['username'] ) )
    {
        $username = $_POST['username'];
    }

    //Connecting to sql db.
    $mysqli = new mysqli("localhost","username","password","database");

    //updating database
    $query = $mysqli->query( "UPDATE members SET `state` = '$state', `city` = '$city', `name` = '$name'  WHERE `username` = '$username'" );

    //closing mysqli connection
    $mysqli->close;

    //echoing that the information has been added
    echo "Your information has been successfully added to the database."; 
?>