Solved

Combine 2 php files

Posted on 2006-10-26
5
193 Views
Last Modified: 2010-04-16
Is it possible to combine these 2 php files?

<?
mysql_connect("localhost","user","pass");
mysql_select_db("database");
$submit = $_POST['submit'];
$name = $_POST['name'];
$time = $_POST['time'];
$message = $_POST['message'];
if($submit){
$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,time,message)"."VALUES ('NULL','$name','$time','$message')");
} else {
}
?>

====================
and
====================

<?php
header("Content-type: text/xml");

$host = "localhost";
$user = "user";
$pass = "pass";
$database = "database";

$linkID = mysql_connect($host,$user,$pass) or die ("Could not connect to host.");
mysql_select_db($database,$linkID) or die ("Could not find database.");

$query = "SELECT * from shoutbox ORDER BY id DESC";
$resultID = mysql_query($query,$linkID) or die ("Data not found");

$xml_output = "<?xml version =\"1.0\"?>\n";
$xml_output = "<shouts>\n";
for ($x = 0; $x < mysql_num_rows($resultID); $x++){
      $row = mysql_fetch_assoc($resultID);
      $xml_output .= "\t<shout>\n";
      $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
      $xml_output .= "\t\t<time>" . $row['time'] . "</time>\n";
      $xml_output .= "\t\t<message>" . $row['message'] . "</message>\n";
      $xml_output .= "\t</shout>\n";
}
$xml_output.= "</shouts>";

echo $xml_output;
?>
============================

I'm trying to create a flash-based shoutbox dealy, but with the two files seperated, when the flash does the POST of the message, it re-directs to the post.php page, which is not the desired effect. I need for the informaton to get posted and then refresh the flash movie with the new post added.

Any help would be greatly appreciated.

Thanks.
0
Comment
Question by:warts
5 Comments
 
LVL 5

Expert Comment

by:daohailam
ID: 17813884
Like this:

<?php
header("Content-type: text/xml");

$host = "localhost";
$user = "user";
$pass = "pass";
$database = "database";

$linkID = mysql_connect($host,$user,$pass) or die ("Could not connect to host.");
mysql_select_db($database,$linkID) or die ("Could not find database.");

$query = "SELECT * from shoutbox ORDER BY id DESC";
$resultID = mysql_query($query,$linkID) or die ("Data not found");

$xml_output = "<?xml version =\"1.0\"?>\n";
$xml_output = "<shouts>\n";
for ($x = 0; $x < mysql_num_rows($resultID); $x++){
     $row = mysql_fetch_assoc($resultID);
     $xml_output .= "\t<shout>\n";
     $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
     $xml_output .= "\t\t<time>" . $row['time'] . "</time>\n";
     $xml_output .= "\t\t<message>" . $row['message'] . "</message>\n";
     $xml_output .= "\t</shout>\n";
}
$xml_output.= "</shouts>";

echo $xml_output;

$submit = $_POST['submit'];
$name = $_POST['name'];
$time = $_POST['time'];
$message = $_POST['message'];
if($submit){
$result=mysql_query("INSERT INTO shoutbox (id,name,time,message)"."VALUES ('NULL','$name','$time','$message')", $linkID);
} else {
}
?>
0
 
LVL 1

Author Comment

by:warts
ID: 17814031
Thanks,

Unfortunately, that's not going to work. It does the same thing as it was doing before. When I hit the submit button it does the post and redirects to the php page instead of staying within the flash movie. If I try to embed the flash into the php page with the combined scripts, it just hands out xml not well-formatted errors.

The flash movie is using 2 urls. 1 is for getting the posts that are already there, the other is for posting new messages to the databse.

I was hoping that there was a way to combine the 2 scripts in a way that allowed the movie to both post and view from within the same php file, and within the .swf file.

I'd ideally like to be able to go directly to the swf url and have it work, instead of a php or html page. This way I can just embed the swf into the site I'm working on.

Thanks again for helping.
0
 
LVL 1

Accepted Solution

by:
davidisaac earned 500 total points
ID: 17822626
Do you mean like this?

Then you would just have to change the flash movie url to append "?case=insert" for the insert function.

<?

$host = "localhost";
$user = "user";
$pass = "pass";
$database = "database";


$case = $_GET['case'];


switch( $case ) {
    case 'insert':
        insertIntoDB();
        break;
    default:
        showResults();
        break;
}

exit;


function insertIntoDB() {
  global $host, $user, $pass, $database;

  mysql_connect("localhost","user","pass");
  mysql_select_db("database");
  $submit = $_POST['submit'];
  $name = $_POST['name'];
  $time = $_POST['time'];
  $message = $_POST['message'];
  if($submit){
  $result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,time,message)"."VALUE('NULL','$name','$time','$message')");
  } else {
  }
}



function selectResults() {
  global $host, $user, $pass, $database;

  header("Content-type: text/xml");

  $linkID = mysql_connect($host,$user,$pass) or die ("Could not connect to host.");
  mysql_select_db($database,$linkID) or die ("Could not find database.");

  $query = "SELECT * from shoutbox ORDER BY id DESC";
  $resultID = mysql_query($query,$linkID) or die ("Data not found");

  $xml_output = "<?xml version =\"1.0\"?>\n";
  $xml_output = "<shouts>\n";
  for ($x = 0; $x < mysql_num_rows($resultID); $x++){
     $row = mysql_fetch_assoc($resultID);
     $xml_output .= "\t<shout>\n";
     $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
     $xml_output .= "\t\t<time>" . $row['time'] . "</time>\n";
     $xml_output .= "\t\t<message>" . $row['message'] . "</message>\n";
     $xml_output .= "\t</shout>\n";
  }
  $xml_output.= "</shouts>";

  echo $xml_output;
}
?>
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 76
countHi challenge 25 84
stringclean challenge 26 56
java  and programming certification ? 4 65
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now