数据库在PHP / MySQL中无法正确更新

时间:2015-11-27 09:39:04

标签: php mysql

我有一个包含用户表的数据库,我正在尝试更新。

目前我有customers.php,它显示带有用户信息的表单字段,以便可以更新。

这个表单指向edit_customer_processor.php,它接受新值,将它们放入MYSQL查询......然后,当我通过PHPMyAdmin命令行查询数据库时查询工作正常,记录没有'更新。

customers.php

<?php
  session_start();
  if(!$_SESSION["logged_in"]){
    header("location:home.php");
    die;
  }
?>
<?php include 'header.html'; ?>
<div id='maincontent'>

<?php

  if (isset($_GET["id"])){
    $customer_id = $_GET["id"];

    require_once('config.php');

    $customer_query = "SELECT * FROM customer WHERE customer_id = $customer_id";
    $customer_results = mysql_query($customer_query, $conn);
    if (!$customer_results) {
      die ("Error selecting car data: " .mysql_error());
    }
    else {
      while ($row = mysql_fetch_array($customer_results)) {
        echo "<h3>Edit Customer</h3>";
        echo "<FORM method='post' action='edit_customer_processor.php'>";
          echo '<p> Name: <input type="text" name="name" size = "40" value=' . $row[name] . '></p>';
          echo '<p> Address: <input type="text" name ="address" size="40" value=' . $row[address] . '></p>';
          echo '<p> Email: <input type="text" name="email" value=' . $row[email] . '></p>';
          echo '<p> Phone: <input type ="text" name="phone" size="20" value=' . $row[phone] . '></p>';
          echo '<input type ="hidden" name="customer_id" value="' . $row[customer_id] . '">';
          echo '<input type ="hidden" name="formtype" value="edit_customer">';
          echo '<input type="submit" name="submit" value= "Update">';
        echo '</form>';
      }
    }
  } else {
    // If there isn't an ID, display the New Customer form and all customers below, with links
    // to their edit pages.

    echo "<h3>Enter new customer information and submit.</h3>";

    echo "<FORM method='post' action='new_customer_processor.php'>";

      echo '<p> Name: <input type="text" name="name" size = "40"></p>';
      echo '<p> Address: <input type="text" name ="address" size="40"></p>';
      echo '<p> Email: <input type="text" name="email"></p>';
      echo '<p> Phone: <input type ="text" name="phone" size="20"></p>';
      echo '<input type ="hidden" name="formtype" value="new_customer">';
      echo '<input type="submit" name="submit" value= "Submit">';
      echo '<input type ="reset" name="reset" value ="Reset">';

    echo '</form>';

    require_once('config.php');
    echo "<h3>Current Customers</h3>";
    $query = "SELECT * FROM customer";
    $results = mysql_query($query, $conn);
    if (!$results) {
      die ("Error selecting customer data: " .mysql_error());
  }
  else {
    // In the absence of an ID, all customers will be displayed down
    // the bottom of the page
    while ($row = mysql_fetch_array($results)) {
      echo "<a href=customers.php?id=";
      echo $row[customer_id];
      echo "><p> $row[name] </p></a>";
      echo "<p> $row[address] </p>";
      echo "<p> $row[phone] </p>";
      echo "<p> $row[email] </p>";
    }
  }
      }
?>
<a href="customers.php">Back to Customers Page</a>

</div>
<?php include 'footer.html' ?>

edit_customer_processor.php

<?php include 'header.html' ?>
  <div id="maincontent">
<?php
  // Pulling in hidden customer ID from post value
  $mysqli = new mysqli( 'localhost', 'root', 'root', 'w_c_a' );

    // Check our connection
    if ( $mysqli->connect_error ) {
      die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
    }
  // Insert our data
  $sql = mysql_query("UPDATE customer
     SET name = '".mysql_real_escape_string($_POST['name'])."',
     address = '".mysql_real_escape_string($_POST['address'])."',
     phone = '".mysql_real_escape_string($_POST['phone'])."',
     email = '".mysql_real_escape_string($_POST['email'])."'
     WHERE customer_id='".mysql_real_escape_string($_POST['customer_id'])."'");
  $update = $mysqli->query($sql);

  echo "Customer updated: ";
  echo "<a href=customers.php?id=" . $_POST['customer_id'] . ">";
  echo "Back to Edit Customer</a>";
  ?>
</div>
<?php include 'footer.html' ?>

当我回复MYSQL查询时,我得到:

UPDATE customer SET name = 'Kellyassdsa', address = 'ads', phone = '0260123123', email = 'asdasd' WHERE customer_id='1'

当我把它放在PHPMyAdmin中时,它会起作用。

我知道这将是一个愚蠢的小错误,但我现在已经尝试了这项工作多年。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

也许您的程序无法连接到您的MySQL数据库。

$customer_results = mysql_query($customer_query, $conn);

我看不到你给var $conn赋值的位置。 如果问题是连接问题,那么我们可能需要您的数据库信息,如PhpMyAdmin中表的名称。

答案 1 :(得分:-1)

你的问题是......

cmake_minimum_required(VERSION 2.8.3)

### Try to add external project 
include(ExternalProject)
ExternalProject_Add(project_appl
    PREFIX project_appl 
    SVN_REPOSITORY "mySVN.server"
    SVN_REVISION ""         
    SVN_USERNAME "user" 
    SVN_PASSWORD "pwd"
    SVN_TRUST_CERT 1  
    UPDATE_COMMAND ""
    SOURCE_DIR "project_appl/src"
    CONFIGURE_COMMAND ""
    BINARY_DIR "project_appl/src"
    BUILD_COMMAND  make
    BUILD_IN_SOURCE 
    INSTALL_COMMAND ""
)
ExternalProject_Get_Property(project_appl binary_dir)

project(pomdp_planner)    
SET(APPL_LIB_DIR ${binary_dir})
add_custom_command(TARGET project_appl
    POST_BUILD
    COMMAND cmake -E remove_directory ${binary_dir}/Utils/boost
    COMMAND cp ${binary_dir}/libappl.a ./CMakeFiles/pomdp_planner.dir/src/libappl.a
    COMMAND cp ${binary_dir}/OnlineOPSolver/OnlineOPsolver/solverOP_lib.hpp ../../devel/include/pomdp_planner/solverOP_lib.hpp
    )    
find_package(catkin REQUIRED COMPONENTS 
  roscpp
  rospy
  std_msgs
  message_generation
)    
 add_service_files(
   FILES
   PlannerAction.srv 
 )    
 generate_messages(
   DEPENDENCIES
   std_msgs
 )    
SET(SOURCES ${PROJECT_SOURCE_DIR}/src/main.cpp)
SET(HEADERS ${APPL_LIB_DIR}/OnlineOPSolver/OnlineOPsolver/solverOP_lib.hpp 
    ${APPL_LIB_DIR}/OfflineSolver/GlobalResource.h
    ${APPL_LIB_DIR}/OfflineSolver/solverUtils.h
    ${APPL_LIB_DIR}/Parser/Cassandra/Parser.h
    ${APPL_LIB_DIR}/Parser/Cassandra/POMDP.h
    ${APPL_LIB_DIR}/Parser/ParserSelector.h 
    ${APPL_LIB_DIR}/Models/MOMDP/MOMDP.h
    ${APPL_LIB_DIR}/Algorithms/SARSOP/SARSOP.h 
    ${APPL_LIB_DIR}/Algorithms/OPMDP/OPMDP.h
    ${APPL_LIB_DIR}/Bounds/BackupAlphaPlaneMOMDP.h
    ${APPL_LIB_DIR}/Bounds/BackupBeliefValuePairMOMDP.h 
    ${APPL_LIB_DIR}/OnlineOPSolver/OnlineOPsolver/dataLogger.h
    ${APPL_LIB_DIR}/Bounds/FastInfUBInitializer.h
    ${APPL_LIB_DIR}/Bounds/FullObsUBInitializer.h
    ${APPL_LIB_DIR}/Utils/CPTimer.h 
    ${APPL_LIB_DIR}/Core/BeliefCache.h
 )    
SET(APPL_DIR 
    ${APPL_LIB_DIR}/OnlineOPSolver/OnlineOPsolver/
    ${APPL_LIB_DIR}/OfflineSolver/
    ${APPL_LIB_DIR}/Parser/Cassandra/include
    ${APPL_LIB_DIR}/Parser/Cassandra/
    ${APPL_LIB_DIR}/Parser/
    ${APPL_LIB_DIR}/Parser/POMDPX/
    ${APPL_LIB_DIR}/Models/MOMDP
    ${APPL_LIB_DIR}/Algorithms/SARSOP
    ${APPL_LIB_DIR}/Algorithms/OPMDP/
    ${APPL_LIB_DIR}/Bounds/
    ${APPL_LIB_DIR}/Utils/
    #${APPL_LIB_DIR}/Utils/boost
    ${APPL_LIB_DIR}/Core/
    ${APPL_LIB_DIR}/MathLib
    ${APPL_LIB_DIR}/Algorithms
    ${APPL_LIB_DIR}/Parser/POMDPX
)    
SET(LINKER_FLAG "-std=c++11 -fpermissive")    
catkin_package(
  INCLUDE_DIRS ${APPL_LIB_DIR}  
  LIBRARIES pomdp_planner  project_appl
  CATKIN_DEPENDS roscpp rospy std_msgs message_generation
  DEPENDS system_lib 
)    
add_definitions(${LINKER_FLAG})
${APPL_DIR}/libappl.a)    
include_directories(
    include
    ${catkin_INCLUDE_DIRS}
    ${APPL_LIB_DIR}
    ${APPL_DIR} 
)     
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS} ${APPL_LIB_DIR})     
add_dependencies(${PROJECT_NAME} project_appl) 
add_dependencies(${PROJECT_NAME} ${catkin_EXPORTED_TARGETS} )
target_link_libraries(${PROJECT_NAME} ${ROS_LIBRARIES} ${catkin_LIBRARIES} ${APPL_LIB_DIR}/libappl.a)

应该是

$sql = mysql_query(..);

$update = $mysqli->query($sql);

答案 2 :(得分:-2)

我认为问题是由于换行而发生的。请求在单行中进行查询而不换行。

$sql = mysql_query("UPDATE customer SET name = '".mysql_real_escape_string($_POST['name'])."',address = '".mysql_real_escape_string($_POST['address'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', email = '".mysql_real_escape_string($_POST['email'])."' WHERE customer_id='".mysql_real_escape_string($_POST['customer_id'])."'");

希望这会有所帮助..