PHP Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jimmyb29
Forum Newbie
Posts: 7
Joined: Fri Apr 04, 2014 2:05 pm

PHP Problem

Post by jimmyb29 »

Hi, I'm back with a similar problem. This is the php code to query and add names to the guestbook. It ALMOST works, but-?

<img_back>imgback</img_back>

Code: Select all

<?php 
$link = mysql_connect('jimmybryantnet.ipagemysql.com', '*****', '*****'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error());
 
} 
echo 'Connected successfully';


 mysqli_select_db(guestbook); 
?> 
} 
echo 'Connected successfully';
 
(mysqli_select_db(guestbook); 
?>";
$server = mysql_connect('jimmybryantnet.ipagemysql.com', '****', '****'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error());
 
} 
echo 'Connected successfully';$user = "*****";
$password = *****";

$dataBase = "guestbook";


$conx = mysqli_connect(<?php 
$link = mysql_connect('jimmybryantnet.ipagemysql.com', '*****', '****'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error());
 
} 
echo 'Connected successfully';
 
 
mysql_select_db(guestbook);
 
?>$server,$user,
$password);

$db_selected = mysqli_select_db($dataBase,$conx);


if($conx && $db_selected){
	$xml = "<xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
	$xml .= "<data>\n";
	
	if(isset($_POST['name'])){
		$result = 0;

		$name=mysqli_escape_string(trim($_POST['name']));

		$email=mysqli_escape_string(trim($_POST['email']));

		$message=mysqli_escape_string(trim($_POST['message']));

		
		$sqli="INSERT INTO guestbook(name,email,message,dateAdded)values('$name','$email','$message',now())";


		$query = mysqli_query($sql,$conx);

		if ($query){
			$result= 1;

			$sql2 = "SELECT * FROM guestbook ORDER BY id DESC";

			$query2 = mysqli_query($sql2,$conx);

			//WHEN query == true , GET LIST OF MESSAGES  AND PUT THEM AS XML FILE
			while($data = mysqli_fetch_array($query2)){
				$xml .= "<guest>\n";

				$xml .= "<name>".$data['name']."</name>\n";


				$xml .= "<msg><![CDATA[".$data['message']."]]></msg>\n";

				$xml .= "</guest>\n";
				}
			}
		else{
			$result=0;

			}
		$xml .= "<inserted>".$result."</inserted>\n";

		
	}	
	if(isset($_POST['getMessage'])){
			$sql = "SELECT * FROM guestbook ORDER BY id DESC";


			$query = mysql_query($sql,$conx);

	
		while($data = mysql_fetch_array($query)){
$xml .= "<guest>\n";

				$xml .= "<name>".$data['name']."</name>\n";

				$xml .= "<msg><![CDATA[".$data['message']."]]></msg>\n";
				$xml .= "<sdate>".$data['dateAdded']."</sdate>\n";
				$xml .= "</guest>\n";
			}
		}
	$xml .= "</data>\n";
	echo $xml;
}
else{
	die (mysqli_error());
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Problem

Post by requinix »

That code is all sorts of messed up. Is that really what you have?
jimmyb29
Forum Newbie
Posts: 7
Joined: Fri Apr 04, 2014 2:05 pm

Re: PHP Problem

Post by jimmyb29 »

Unfortunately, yes! I believe part of it is right, but the rest -? Is there any way I could pay you to straighten it out so that it works properly? I would DESPERATELY like to get this guestbook finished!

Thanks,

Jimmyb29
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Problem

Post by requinix »

I can't straighten it up for you because I don't know what it's supposed to look like. That is what you mean, right? Because what you've posted, literally, won't work. Certainly won't do what you're saying it does. So I'm asking whether the code you posted, what you have right there in your post, is exactly the code you're working with.
jimmyb29
Forum Newbie
Posts: 7
Joined: Fri Apr 04, 2014 2:05 pm

Re: PHP Problem

Post by jimmyb29 »

requinix wrote:I can't straighten it up for you because I don't know what it's supposed to look like. That is what you mean, right? Because what you've posted, literally, won't work. Certainly won't do what you're saying it does. So I'm asking whether the code you posted, what you have right there in your post, is exactly the code you're working with.
I have another version of this that might be better. Let me see if I can find it.

Jimmyb
jimmyb29
Forum Newbie
Posts: 7
Joined: Fri Apr 04, 2014 2:05 pm

Re: PHP Problem

Post by jimmyb29 »

jimmyb29 wrote:
requinix wrote:I can't straighten it up for you because I don't know what it's supposed to look like. That is what you mean, right? Because what you've posted, literally, won't work. Certainly won't do what you're saying it does. So I'm asking whether the code you posted, what you have right there in your post, is exactly the code you're working with.
I have another version of this that might be better. Let me see if I can find it.

Jimmyb
Here it is - but it didn't work!

Code: Select all

<?php
<?xml version="1.0" encoding="UTF-8"?>
$server = " <?php 
$link = mysqli_connect('jimmybryantnet.ipagemysql.com', '*****', '*****'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully'; 
mysqli_select_db(guestbook); 
?> ";
$user = "*****";
$password = "*****";
$dataBase = "guestbook";

$conx = mysqli_connect($server,$user,$password);
$db_selected = mysqli_select_db($dataBase,$conx);

if($conx && $db_selected){
	$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
	$xml .= "<data>\n";
	
	if(isset($_POST['name'])){
		$result = 0;
		$name=mysqli_escape_string(trim($_POST['name']));
		$email=mysqli_escape_string(trim($_POST['email']));
		$message=mysqli_escape_string(trim($_POST['message']));
		
		$sql="INSERT INTO guestbook(name,email,message,dateAdded)values('$name','$email','$message',now())";
		$query = mysqli_query($sql,$conx);
		if ($query){
			$result= 1;
			$sql2 = "SELECT * FROM guestbook ORDER BY id DESC";
			$query2 = mysqli_query($sql2,$conx);
			//WHEN query == true , GET LIST OF MESSAGES  AND PUT THEM AS XML FILE
			while($data = mysqli_fetch_array($query2)){
				$xml .= "<guest>\n";
				$xml .= "<name>".$data['name']."</name>\n";
				$xml .= "<msg><![CDATA[".$data['message']."]]></msg>\n";
				$xml .= "<sdate>".$data['dateAdded']."</sdate>\n";
				$xml .= "</guest>\n";
				}
			}
		else{
			$result=0;
			}
		$xml .= "<inserted>".$result."</inserted>\n";
		
	}	
	if(isset($_POST['getMessage'])){
			$sql = "SELECT * FROM guestbook ORDER BY id DESC";
			$query = mysqli_query($sql,$conx);
			while($data = mysqli_fetch_array($query)){
				$xml .= "<guest>\n";
				$xml .= "<name>".$data['name']."</name>\n";
				$xml .= "<msg><![CDATA[".$data['message']."]]></msg>\n";
				$xml .= "<sdate>".$data['dateAdded']."</sdate>\n";
				$xml .= "</guest>\n";
			}
		}
	$xml .= "</data>\n";
	echo $xml;
}
else{
	die (mysqli_error());
}
?>
See if you can make sense of it.

Thanks, Jimmyb
Last edited by requinix on Tue Dec 23, 2014 2:19 am, edited 1 time in total.
Reason: [syntax=php] tags
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Problem

Post by requinix »

That's not your code either.

To make sure we both understand what the problem is: it's nothing to do with the "It ALMOST works, but-" you said in your first post. The code you have running right now, that works partially but not quite, is somewhere on your computer. Or on a server somewhere, I don't know. But what you've been posting is not that code: the first one was valid, coincidentally, but does not even come close to working, while this latest code is not even valid PHP code in the first place.

In an effort to get the right code posted here, I have some questions:
1. Where is the code you're reading from right now? A file on your computer? Somewhere else?
2. How are you getting that code into your posts in this thread?
3. Can you try using just an attachment instead?

One more thing. If you do put the code directly into your post, please copy/paste, then select all the code and hit the "PHP Code" button above the text box. That will wrap the code in

Code: Select all

 tags that make it much easier to read than if it was just straight text.
Post Reply