Forum Moderators: coopster

Message Too Old, No Replies

PHP-Mysqli Problem

         

jimmyb29

8:46 am on Oct 25, 2014 (gmt 0)

10+ Year Member



Hi,
I'm an Old Newbie" trying to build a flip guestbook with some "Store Bought" code.
The guest.php doesn't work. Would some kind soule be kind enough to take a look and see what the problem is?

<?php
// CONNECT TO THE SERVER AND SELECT DATABASE
$server = "/home/users/web/b2713/ipg.example";
$user = "example.com"; $password = "*********"; $dataBase = "guestbook";

$conx = mysqli_connect($ipage.com,$jimmyb29,$********);?>
resource mysql_connect ([ string $server = ini_get("ipage.com") [, string $"example.com" = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_********") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )
{
die('Could not connect: ' . mysqli_error());
}

$db_selected = mysqli_select_db("test_db", $con);

if (!$db_selected)
{
die ("Can't use test_db : " . mysqli_error());
}

mysqli_close($con);
?>
$conx = mysqli_connect($ipage,$example.com,$*******);
$db_selected = mysqli_select_db($dataBase,$conx);

if($conx && $db_selected){
// IF CONNECTION IS ESTABLISHED
$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=mysqil_escape_string(trim($_POST['email']));
$message=mysqli_escape_string(trim($_POST['message']));

// ADD DATA TO THE TABLE guestbook WHEN THE USER PRESS THE send_btn in FLASH
$sql="INSERTINTOguestbook(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'])){
// GET LIST OF MESSAGES AND PUT THEM AS XML FILE
$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{
// IF CONNECTION == false OR DATABASE DOESN'T EXISTE
die (mysqli_error());
}
?>

[edited by: Ocean10000 at 5:44 pm (utc) on Oct 25, 2014]
[edit reason] Examplified [/edit]

phranque

10:49 am on Oct 25, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, jimmyb29!


it looks like you have several ?> closing tags in your code.

jimmyb29

7:31 pm on Oct 25, 2014 (gmt 0)

10+ Year Member



Hi, phranque! (LOVE that! Are you "Phrench?")

As I said, I really don't know much about code, but, at the ripe old age of 85, am still trying to learn.
I know that there is supposed to be an opening and closing tag for every PHP statement. Should I remove any extras, which I must confess I hadn't noticed? Jimmyb

jimmyb29

7:46 pm on Oct 25, 2014 (gmt 0)

10+ Year Member



Hi, Phranque,

I removed the php closing tags, but still get the error, "Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request."

Any ideas?

Jimmyb

phranque

9:28 pm on Oct 25, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



whenever you get an internal server error there should be a corresponding entry in the web server error log file.

jimmyb29

10:08 pm on Oct 25, 2014 (gmt 0)

10+ Year Member



I think I have it narrowed down even more - but it still involves a server error.

Here's the current php code:

<?php
// CONNECT TO THE SERVER AND SELECT DATABASE
$server = " <?php
$link = mysql_connect('jimmybryantnet.ipagemysql.com', 'jimmyb29', '*******');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(guestbook);
?> ";
The mysql code seems to be failing.

Jimmyb29

Dammy

7:09 pm on Oct 26, 2014 (gmt 0)

10+ Year Member



a) Please do check you .htaccess very well
b) mysqli_query($sql,$conx) Should be mysqli_query($conx,$sql)

penders

9:54 pm on Oct 26, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I know that there is supposed to be an opening and closing tag for every PHP statement.


Not exactly. If you have a file that contains only PHP code (which you seem to have in your examples) then you should only have a PHP opening tag
<?php
at the very start of the file (not even a space should precede it). You can omit (considered best practise) the closing PHP tag
?>
at the end of the file. The PHP block is automatically closed at the end of the file and omitting it ensures there is no white-space following it.

The only time you should use "an opening and closing tag for every PHP statement" is when you are embedding snippets of PHP code in a predominantly HTML document (by jumping in and out of PHP mode). Whilst this type of PHP coding is (historically) very common, it is very "hacky", messy, harder to maintain and generally considered bad practise (beyond simply echo'ing variables). Try and separate the PHP code from the HTML output as much as possible.

phranque

6:38 am on Oct 29, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



<?php
// CONNECT TO THE SERVER AND SELECT DATABASE
$server = " <?php

that's two opening tags - haven't tried that so not sure what results you would get there.