-1

I've got a database with a Users table which I'm trying to update.

Currently I have customers.php, which displays form fields with the user information so it can be updated.

This form points to edit_customer_processor.php , which takes the new values, puts them into a MYSQL query... and then despite the query working correctly when I query the DB via the PHPMyAdmin command line, the record doesn't update.

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' ?>

And when I echo the MYSQL query, I get:

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

Which works when I put it in PHPMyAdmin.

I know it'll be some boneheaded little mistake, but I've been trying to get this work for ages now. Any ideas?

Hinchy
  • 101
  • 1
  • 10

3 Answers3

0

Maybe your program just can't connect to your MySQL database.

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

I can't see where you gave a value to the var $conn. If the problem is connection problem then we might need your database info like the name of your table in PhpMyAdmin.

Natalie Hedström
  • 2,607
  • 3
  • 25
  • 36
MrChelou
  • 36
  • 3
  • It can be a bonehead mistake like you said like forget a 's' at the end of your db_name it would explain why the javascript can return the error issue. – MrChelou Nov 27 '15 at 11:52
-1

your problem is...

$sql = mysql_query(..);

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

it should be

$sql = 'UPDATE ...';

$update = $mysqli->query($sql);
Murat Cem YALIN
  • 317
  • 1
  • 6
  • It's not the only issue here. The problem is because of the undefined `customer_id`. – Rajdeep Paul Nov 27 '15 at 10:27
  • @RajdeepPaul there is a code like "echo '';" and also he said And when I echo the MYSQL query, I get: UPDATE customer SET name = 'Kellyassdsa', address = 'ads', phone = '0260123123', email = 'asdasd' WHERE customer_id='1' .. that means THERE IS A customer_id.... – Murat Cem YALIN Nov 27 '15 at 10:29
  • Oh, my bad. I saw the other form there. – Rajdeep Paul Nov 27 '15 at 10:32
  • This is what fixed my problem. Thanks for that. I'm going to go through and stick consistently to mysqli as well... cheers for the feedback. – Hinchy Nov 27 '15 at 12:43
-2

i think problem occurs due to line break. pleas make a query in single line without line break.

$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'])."'");

Hope this helps..

Ajay Makwana
  • 2,330
  • 1
  • 17
  • 32