Jump to content

PHP script to process UPDATE via MySQL


JsusSalv

Recommended Posts

Hello:

 

I wrote the script below.  It works fine in IE6 but amazingly fails in Firefox and Opera.  It doesn't make sense why Firefox and Opera wouldn't save my info after submitting the data to this script.  Does anyone have any clue as to what I may be doing wrong?

 

Some more info that might help:

1) I am using PDO constructors

2) I have set up a persistent connection to MySQL database.

3) This script is for an Ajax / PHP inline editing feature.

4) My comments are within the code in case there were any questions regarding a particular area.

5) I made this script work with ALL browsers using REPLACE.  While REPLACE is fine, I would like to get 

    UPDATE working as well.

 

Am I writing the UPDATE statement incorrectly or is there a better way to do all of this?

 

Thank you in advance.

 

Hugo

 

 

 

Persistent Connection script:

<?php

// This script uses the PHP Data Object (PDO) connection style (for use with PHP5.1 / MySQL 4.1 or later).

// PDO (PHP Data Object or Portable Data Object) is the new object-oriented data access abstraction.

function dbConnect() {

 

// Include the USERS DATABASE INFORMATION file here.

require('db_info.inc.php');

 

// Try/Catch Block.

// When PDO can't connect to the database, it displays the Username and Passcode onscreen.

// This is because PDO uses a type of error handling called 'exceptions.'

// Unless you catch the exception, PHP displays debugging information onscreen (e.g. Username, Passcode).

// So, use the following try/catch to catch any exceptions.

  try {

 

// Encapsulate the connection code and return it within the variable ($conn).

// This variable now becomes a reference to the database connection object (in this case, PDO).

// String containing hostname & database name must be presented in the following format: 'mysql:host=HOSTNAME;dbname=DATABASE_NAME'.

// The hostname, database_name, username, and passcode can be set within a variable (in this case $hostname, $dbname, $username & $passcode). Just make sure to sure double-quotes around the DSN, hostname, and dbname.

// A Persistent Connection to the database will be establish by appending the PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor.

// Set error handling options for the PDO connection in the fourth argument to the PDO constructor. Example: PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION.

    $conn = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $passcode, array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

    return $conn;

}

 

  // Type of exception (PDOException) and variable to catch the exception ($e). The variable can be anything desired.

  catch (PDOException $e) {

 

// Some error handling; Provide useful error message.

    echo 'Error: Database facilities cannot be accessed at this time.<br />

    Please try again later or contact <a href=\"mailto:db_help@mydomain.com?Database_Connection_Error\"></a><br /><br />';

// Display technical database base error.

    echo($e->getMessage());

exit;

}

  }

?>

 

 

MySQL $_POST Processing Script:

<?php

include('persistentconnection_pdo.inc.php');

include('corefuncs.php');

// Call the function [ nukeMagicQuotes() ] in order to remove backslashes from the $_POST array.

nukeMagicQuotes();

 

// Grab submitted data and assign it to variables.

$title = $_POST['title'];

$subtitle = $_POST['subtitle'];

$forename = $_POST['forename'];

$surname = $_POST['surname'];

$article = $_POST['article'];

 

 

// BEGIN BUILDING SQL QUERY IN PREPARATION FOR PDO PREPARED STATEMENT.

 

  // Create database connection by calling function [ dbConnect() ]

  $conn = dbConnect();

 

  // "UPDATE table_name SET column_name=value, column_name2=value2";

  $sql="UPDATE articles SET title=$title, subtitle=$subtitle, forename=$forename, surname=$surname, article=$article WHERE article_id=0";

 

  // After binding the parameters, execute the Prepared Statement

  // The execute() method runs the SQL query.

  $stmt->execute($_POST);

 

// Generate successful update message and display it within the alert box that pops up after hitting the save button on the toolbar.

$updatedMessage = 'Website Updated!';

?>

Link to comment
Share on other sites

While that is correct...and I have added single-quotes around my variables, Firefox and Opera are NOT updating the information.  IE6 continues to work, however. REPLACE works fine in all browsers but I'd like to use UPDATE.

 

Any other ideas or suggestions?

 

Thanks!

 

Hugo

Link to comment
Share on other sites

Awesome!  I am glad comments were posted to help me out.  I knew it wasn't my scripts.  I have spent three days going over the PHP code am can't see anything wrong. 

 

I'm still stumped, however, as to why my script works with REPLACE on all browsers but when I change it to UPDATE Firefox and Opera stop working.  I didn't change the javascript file at all when I made the change.  Weird eh?

 

 

Hugo

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.