473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Use PHP ONLY to write to database

Akatz712
Is there a way to use PHP to ONLY write to the database?

I am calling a PHP using POST to send data to the server to be stored in the database. The logic to save it is in the PHP.

What I want to happen is that the program calling the PHP (with a SAVE button) pops up a modal window indicating that the database operation was a success or not a success. What is happening now is that I am getting a blank page in the browser which is popping up these messages. And to get back to my page I need to use the browser back button. And for other reasons this is not good.
Apr 12 '07 #1
6 2904
tolkienarda
316 100+
i would love to help but i strugle without the code

eric
Apr 12 '07 #2
This is not a question about any piece of code. It is a question to explain how to do something in PHP.

I am new to PHP (this week).

In my experience, each time a PHP file is "run" it sends an entire page back to the browser. So, then the user needs to press the back button to get back to where they were before. And in this case, I do not want that to happen.
Apr 13 '07 #3
exoskeleton
104 100+
This is not a question about any piece of code. It is a question to explain how to do something in PHP.

I am new to PHP (this week).

In my experience, each time a PHP file is "run" it sends an entire page back to the browser. So, then the user needs to press the back button to get back to where they were before. And in this case, I do not want that to happen.
Hi good sir...if you want not to load the entire page upon writing to the database...you may use AJAX for that ... there in no plain PHP code that can do that!

Hope this tip helps...
Apr 13 '07 #4
tolkienarda
316 100+
exoskelton is right about needing AJAX for a popup or .js either one and to do that in php you just need to echo out the code
example
[PHP]
echo 'alert("whatever", "attributes you want")';
[/PHP]

and that is a basic popup window if to write stuff to a databse you just use the normal database commands inside of a mysql_query function
example
[PHP]
mysql_query("UPDATE thefull SET ftext = '$text' WHERE location = '$location'");
[/PHP]
this will update a table called thefull and set ftext to equla the varable $text in the table row where location is equal to the var $location

and finally if you want to get a value from the database
[PHP]
$result = mysql_query("SELECT user, pass FROM users WHERE pass = '$pass' AND user='$user'");
[/PHP]

if you need any more general information you can check out php.net or the countless tutorals across the web
if you have programing experiance then php.net will probably have all the answers you need but if the programing thought process in knew to you then try some of the tutorials that start with defining vars and building basic logic structures

the best of luck to you

eric
Apr 13 '07 #5
exoskeleton
104 100+
thank you for your support sir eric... :)

@ Akatz712 ... AJAX is the best way of adding record to the database without reloading the whole page sir. as well as retrieving and updating!

more power
Apr 14 '07 #6
The question should have been:
How does the client keep control, and use the server as a servant and not just package something for the server and the server spits out a new page, leaving the original client out of the loop?

The answer is AJAX. Since I am learning PHP, I still will write the server side code in PHP. But if my PHP does generate HTML, it will send it back to the client who called it, and the client decides where to place it in its world.

I found some code online which I modified into a general purpose AJAX POST. My only thought is that this is the wrong forum. But here it is:

Expand|Select|Wrap|Line Numbers
  1.    // This AJAX library is taken from http://www.captain.at/howto-ajax-form-post-request.php
  2.    // I modified it to return the contents of the one text written by the PHP function in a passed handler.
  3.    var http_request = false;
  4.    var js_handler_pointer = null;
  5.    function makePOSTRequest(url, parameters, js_handler) {
  6.       http_request = false;
  7.       js_handler_pointer = js_handler;
  8.       if (window.XMLHttpRequest) { // Mozilla, Safari,...
  9.          http_request = new XMLHttpRequest();
  10.          if (http_request.overrideMimeType) {
  11.              // set type accordingly to anticipated content type
  12.             //http_request.overrideMimeType('text/xml');
  13.             http_request.overrideMimeType('text/html');
  14.          }
  15.       } else if (window.ActiveXObject) { // IE
  16.          try {
  17.             http_request = new ActiveXObject("Msxml2.XMLHTTP");
  18.          } catch (e) {
  19.             try {
  20.                http_request = new ActiveXObject("Microsoft.XMLHTTP");
  21.             } catch (e) {}
  22.          }
  23.       }
  24.       if (!http_request) {
  25.          alert('Cannot create XMLHTTP instance');
  26.          return false;
  27.       }
  28.  
  29.       http_request.onreadystatechange = alertContents;
  30.       http_request.open('POST', url, true);
  31.       http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  32.       http_request.setRequestHeader("Content-length", parameters.length);
  33.       http_request.setRequestHeader("Connection", "close");
  34.       http_request.send(parameters);
  35.    }
  36.  
  37.    function alertContents() {
  38.       if (http_request.readyState == 4) {
  39.          if (http_request.status == 200) {
  40.             //alert(http_request.responseText);
  41.             js_handler_pointer(http_request.responseText);
  42.          } else {
  43.             alert('There was a problem with the request.');
  44.          }
  45.       }
  46.    }
  47. // How to call this is done by simply giving string to the PHP in the form: 
  48. // makePOSTRequest('post.php',"Name1=encodeURI(value1)&Name2=encodeURI(value2)...", js_handler);
  49.  
  50.  
Since this is a PHP forum, I feel obligated to include my PHP code:

[PHP]<?php
function insertsql($item, $key)
{
if (substr($key, 0, 6)=='PMAGIC')
{
global $records, $name;
$query = 'INSERT INTO DesignSession VALUES("'.$name.'",'.substr($item,1,strlen($item)-2).')';
$result = mysql_query($query) or die('Insert main failed: ' . mysql_error());
$records++;
}
}
$name=$_POST['Savename'];
$records = 0;
if ($name=="")
$responseText="Save name not entered.";
else
{
$link = mysql_connect('localhost','root','') or die('Could not connect: ' . mysql_error());
mysql_select_db('akatz712_mgs') or die('Could not select database');

$query = 'SELECT * FROM DesignSession_Background WHERE Name="'.$name.'"';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
if (mysql_num_rows($result)!=0)
{
$responseText="Save name ".$name." already used. Pick another.";
}
else
{
$backcolor = substr($_POST['backgroundcolor'],1,strlen($_POST['backgroundcolor'])-2);
$query = 'INSERT INTO DesignSession_Background VALUES("'.$name.'",'.$backcolor.',NULL)';
$result = mysql_query($query) or die('Insert background failed: ' . mysql_error());
array_walk($_POST, 'insertsql');
$responseText="Design Session ".$name." successfully saved with ".$records." design(s).";
}
mysql_close($link);
}
echo $responseText;
?>
[/PHP]
Apr 14 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: JDJones | last post by:
I'm trying to write a script that will read from a text list of songs that I burned onto my CD (Albums011.txt), then write to the database text the new text made ready for inserting into a...
9
by: Chad Smith | last post by:
Hello, I'm relatively new to the database world so please forgive me in advance for my ignorance. I have recently been tasked at my job with finding a tool that will perform the following tasks:...
8
by: Jack | last post by:
Hi, Here is my problem: I am logging in to a page, where the page retrieves a record from a database. The text boxes are used in the display formto let the users update the fields, if they...
6
by: sambuela | last post by:
How can I write message to the file located in the wwwroot directory? It seems that IIS protect these files. Let make me cannot do the I/O writing sucessfully. I try to open file's write...
1
by: hulinning | last post by:
Hi I use webservice to read/write data to Access database using System.Data.OleDb with a connection string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source;User Id=admin;Password=" I can...
8
by: asenthil | last post by:
Hai, i'm having a string in a specific field of a database... now i want to retrive that string from the database and i have to write that string into a xml file.... retriving is not a...
0
by: musosmiffy | last post by:
Hi, I have been trying to get the following working for days - I wonder if anyone could help me? I am trying to list a set of database entries as a newspaper column. I am using classic ASP (not...
0
by: michael.spoden | last post by:
Hi, I'm working with DB2 UDB V8.2 Fixpak 14 on Linux. Since two days the 'set write suspend on database' command takes multiple hours in our backup script in the night. Theres more or less no...
36
by: CK | last post by:
How do I write a set based query? I have a groupSets table with fields setId, idField, datasource, nameField, prefix, active Data: 1,someIDfield, someTable, someField, pre1, 1 2,someotherIDfield,...
3
by: sriram347 | last post by:
Hi I am a newbie to ASP.NET. I developed a web page (project type is web application) and I keep getting this error. B]Error message : "System.AccessViolation Exception attempted to read or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.